Negate a number in assembly
Hi,
I'd like to know how to negate a number in assembly:
a = -a; //a is a 32bit integer
I tried
mov exa,a;
and eax,0xFFFFFFFF;
mov a,eax;
but this always changes the sign to negative.
if a is already a negative number then the result should be positive.
I searched the net but can't find anything on that. Please help.
Thanks!
Quote:Original post by Antheus
xor eax, 0x80000000;
this won't work for two's complement representation (used on x86).
Quote:Original post by echoheadQuote:Original post by Antheus
xor eax, 0x80000000;
this won't work for two's complement representation (used on x86).
Yes, it occured to me right after posting.
Quote:Original post by JoeZ
and eax,0xFFFFFFFF;
but this always changes the sign to negative.
That's impossible. and eax, 0xffffffff does not change the value at all.
I am not familiar with x86, but in MIPS you could just do something like:
add $1,$0,-$1
Or you could do a bitwise negation and add 1 for twos-complement numbers.
add $1,$0,-$1
Or you could do a bitwise negation and add 1 for twos-complement numbers.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement