Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a positive number to negative in assembly

Tags:

x86

assembly

I want to convert a positive number to negative in assembly, I need a code sample

The input is in hex sdword

Thanks

like image 299
alonp Avatar asked Dec 26 '10 16:12

alonp


People also ask

How do you do negative numbers in assembly?

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.

How do you convert a negative number to a positive assembly language?

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.

How do you change positive to negative in Java?

To convert positive int to negative and vice-versa, use the Bitwise Complement Operator.

How do you change a negative number to a positive in MIPS?

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.


1 Answers

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

like image 180
Necrolis Avatar answered Oct 06 '22 23:10

Necrolis