'Assembly 64 bits function a*b+c/d-(e-f)%(g+h)
I need to solve this function in assembly but I have a problem with modulo. I have no idea why but it gives very strange results.
C++ code
#include <iostream>
extern "C" __int64 suma(int a, int b, int c, int d, int e, int f, int g, int h);
int main()
{
int a = 1, b = 1, c = 1, d = 1, e = 1, f = 1, g = 1, h = 1;
int wynik = suma(a, b, c, d, e, f, g, h);
std::cout << wynik;
}
assembly code
.CODE
_DATA SEGMENT
_DATA ENDS
_TEXT SEGMENT
PUBLIC suma
suma PROC
push rbp;
mov rbp, rsp;
imul rcx, rdx; // a * b
mov rax, r8; // c
xor rdx, rdx;
div r9; // c/d
mov r10, [rbp + 6 * 8]; //e
sub r10, [rbp + 7 * 8]; // e-f
mov r11, [rbp + 8 * 8]; // g
add r11, [rbp + 9 * 8]; //g + h
add rcx, rax; // a * b + c / d
sub rcx, r10; // a * b + c / d - e - f
mov rdx, 0;
mov rax, rcx;
idiv r11;
mov rax, rdx;
pop rbp;
ret
suma ENDP
_TEXT ENDS
END
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
