Jump to content
  • Advertisement
Sign in to follow this  
JoeZ

Negate a number in assembly

This topic is 4438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Antheus
xor eax, 0x80000000;


this won't work for two's complement representation (used on x86).

Share this post


Link to post
Share on other sites
Quote:
Original post by echohead
Quote:
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!