Reputation: 1081
I write a simple assembly function sum64, which add ecx:ebx+edx:eax, its works correct with positive number, but not in negative.
sum64:
add ebx,eax
adc ecx,edx
ret
Example:
edx = 1d1h
eax = a94a2003
ebx = FFFFFFFF
ecx = 00000000
The correct result is 1D1A94A2002h but my function return ecx:1d2h ebx:a94a2002, it is incorrect because the first "add" set the carry, why? How to solve this?
Thanks the answers.
Upvotes: 1
Views: 216
Reputation: 5648
You are still adding positive numbers because ecx:ebx (00000000FFFFFFFF) is a positive number. You need to sign-extend the high dword for this to work. If you use edx:eax for one number, you can use the cdq instruction for this.
Upvotes: 2
Reputation: 793199
The numbers you are testing are:
00000000ffffffff (ecx:ebx)
000001d1a94a2003 (edx:eax) +
----------------
000001d2a94a2002
Neither are negative (in 64-bit two's complement representation) so sum to the result that you are getting. -1
as a 64-bit negative number would be ffffffff
in both ecx
and ebx
which would give the result that you were originally expecting.
ffffffffffffffff (ecx:ebx)
000001d1a94a2003 (edx:eax) +
----------------
000001d1a94a2002
Upvotes: 2