Skip to content

Instantly share code, notes, and snippets.

@maestroviktorin
Last active February 25, 2024 10:49
Show Gist options
  • Save maestroviktorin/c13baf09902d20929c51d79501bcbb4a to your computer and use it in GitHub Desktop.
Save maestroviktorin/c13baf09902d20929c51d79501bcbb4a to your computer and use it in GitHub Desktop.
Computer Organization Laboratory Work #1.
#include <iostream>
#include <string>
static int calculate(int a, int b, int c, int d)
{
int result = 0;
// (4*a + 52 - 9*b) / (c - 84/(d*a))
__asm {
; a * 4
mov eax, a
shl eax, 2
; a * 4 + 52
add eax, 52
; 9 * b
mov ebx, b
imul ebx, 9
; a * 4 + 52 - 9 * b
sub eax, ebx
push eax
; 84 / (d * a)
mov ebx, d
imul ebx, a
mov eax, 84
cdq
idiv ebx
mov ebx, eax
; c - 84 / (d * a)
mov ecx, c
sub ecx, ebx
; Finally.
pop eax
cdq
idiv ecx
; Write result.
mov result, eax
}
return result;
}
static int read(std::string message)
{
int result = 0;
bool err;
do
{
std::cout << message;
try
{
std::cin >> result;
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
throw std::invalid_argument("Integer needed. Try again.");
}
err = false;
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
err = true;
}
} while (err);
return result;
}
int main() // (4*a + 52 - 9*b) / (c - 84/d*a)
{
int a = read("Enter `a`: ");
int b = read("Enter `b`: ");
int c = read("Enter `c`: ");
int d = read("Enter `d`: ");
int result = calculate(a, b, c, d);
// int result = calculate(207, -23, -92, 6);
std::cout << result << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment