[英]subtracting negative numbers in assembly
I know in order to turn a number into a negative in assembly, I need to turn it into two's complement by xor'ing it then adding one. 我知道为了将数字转换为负数,我需要先对它进行异或然后加一个,从而将它转换为二进制补码。
I've done this, but my output is still incorrect, can someone please explain to me where I have made a mistake? 我已经做到了,但是我的输出仍然不正确,有人可以向我解释我哪里出错了吗?
The object is to subtract -val1 - val2 - val3 对象是减去-val1-val2-val3
Here is the bulk of my code 这是我的大部分代码
.data val1 dword 10000h val2 dword 40000h val3 dword 20000h finalVal dword ?
.code main PROC .code主程序
mov eax,val1 ; start with 10000h
xor eax,val1 ; xor eax to make it almost negative
add eax,1h ; now add one to make it negative
sub eax,val2 ; subtract 40000h
sub eax,val3 ; subtract 20000h
mov finalVal,eax ; store the result (30000h)
call DumpRegs ; display the registers
exit
https://ideone.com/hulsdU https://ideone.com/hulsdU
The final result should be EAX=FFF90000 but currently my output is EAX=FFFA0001 最终结果应该是EAX = FFF90000,但目前我的输出是EAX = FFFA0001
note that I am using 32 bit numbers, and running this on an x86 processor. 请注意,我使用的是32位数字,并在x86处理器上运行它。
Thank you for any help 感谢您的任何帮助
This won't work: 这行不通:
mov eax,val1
xor eax,val1
; (a XOR a) is always 0. Using the line above you XOR
; val1 with val1 so EAX will always be 0 here!
add eax,1h
; (Ok. EAX will be 1 here...)
sub eax,val2
...
If you want to negate a number you can use the "NEG" instruction: 如果要取反数字,可以使用“ NEG”指令:
neg eax
If you want to do this using the ones complement you use the "NOT" instruction: 如果要使用补码来执行此操作,请使用“ NOT”指令:
not eax
add eax, 1h
Or: 要么:
not eax
inc eax
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.