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
Here is the bulk of my code
.data val1 dword 10000h val2 dword 40000h val3 dword 20000h finalVal dword ?
.code main PROC
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
The final result should be EAX=FFF90000 but currently my output is EAX=FFFA0001
note that I am using 32 bit numbers, and running this on an x86 processor.
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 eax
If you want to do this using the ones complement you use the "NOT" instruction:
not eax
add eax, 1h
Or:
not eax
inc eax
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.