I want to convert a positive number to negative in assembly, I need a code sample
The input is in hex sdword
Thanks
Negative numbers are represented in Two's Complement in assembly. In order to obtain Two's Complement of a number you have two options: to complement all it's bits and add one. to complement all it's bits until the last 1.
With only one exception, starting with any number in two's-complement representation, if all the bits are flipped and 1 added, the two's-complement representation of the negative of that number is obtained. Positive 12 becomes negative 12, positive 5 becomes negative 5, zero becomes zero(+overflow), etc.
To convert positive int to negative and vice-versa, use the Bitwise Complement Operator.
You are processing floating point numbers by using integer instructions and want to invert the sign: In this case you have to change the sign bit. This is done using x xor 0x80000000 . On MIPS CPUs, you can generate the constant 0x80000000 using one lui instruction.
Well, there are two ways to do this, best would be to load the number into a register, then use the NEG
instruction as Hans mention, ie: NEG EAX
would negate eax. The other way would be XOR EAX,EAX SUB EAX,EDX
where edx contains the number you want to negate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With