How to Negate an Integer in x86 Assembly Language

x86 Assembly programming language

In this article, we show how to negate an integer in x86 assembly language.

Negating a number performs the two's complement on a number, making a positive number negative.

With the two's complement method, we convert the hexadecimal number into binary. Each 1, we turn into a 0. Each 0 we turn into a 1. Lastly, we add 1 to the binary number. We can then convert the number back to hexadecimal.

For example, let's take the hexadecimal number 2 and find the negated integer value.

So the AX register is a 16-bit register.

So 2 would be represented as, 0000 0000 0000 0010

Inverting all the 1s to 0s and all the 0s to 1s, gets us the following, 1111 1111 1111 1101

Lastly, we add 1, which gets us, 1111 1111 1111 1110

Converting this into hexadecimal gets us, FF FE

Let's demonstrate this now in code.

So the first thing we do is move 2 into the AX register.

We then negate the integer by using the neg operation.

After running the code, we see that the AX register now holds, FF FE

So it worked just as we calculated above.

This value now represents -2

So this is how to negate an integer in the x86 assembly language.

Related Resources

HTML Comment Box is loading comments...