为什么要设置进位标志?
我写了一个简单的汇编函数 sum64,它添加了 ecx:ebx+edx:eax,它对于正数可以正确工作,但对于负数则不行。
sum64:
add ebx,eax
adc ecx,edx
ret
示例:
edx = 1d1h
eax = a94a2003
ebx = FFFFFFFF
ecx = 00000000
正确的结果是 1D1A94A2002h,但我的函数返回 ecx:1d2h ebx:a94a2002,这是不正确的,因为第一个“add”设置了进位,为什么? 怎么解决这个问题呢?
谢谢各位的解答。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在测试的数字是:
两者都不是负数(以 64 位二进制补码表示形式),因此将其与您得到的结果相加。
-1
作为 64 位负数,在ecx
和ebx
中都是ffffffff
,这将给出结果你原本所期待的。The numbers you are testing are:
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 beffffffff
in bothecx
andebx
which would give the result that you were originally expecting.您仍在添加正数,因为 ecx:ebx (00000000FFFFFFFF) 是正数。您需要对高位双字进行符号扩展才能使其工作。如果您使用 edx:eax 作为一个数字,则可以使用 cdq 指令来实现这一目的。
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.