assemblyx86integer-overflowtwos-complementsigned-integer

What are the specifics of STORING a 32 bit signed integer into a register?


My number type is a signed two’s complement integer.

In memory registers %rdi/edi/di, I have 0xFFFFFFFF. In %rsi/esi/si, I have 0x80000000.

My instruction is addl %edi, %esi.

How do I properly add these?

I think the answer is:

Since I'm adding the full 32-bit register, I have the benefit of adding the full amounts 0xFFFFFFFF and 0x80000000.

So, I have a signed integer added to a signed, and they are both 32-bit. I am effectively adding -2147483648 and 0x80000000. Since 0x80000000 is 2147483648 in hexadecimal, I add these two and I get zero.

The Zero Flag is activated The Carry Flag is activated (since I am adding the leading value of the 32 bit register)


Solution

  • In 32-bit two’s complement, 0xFFFFFFFF represents −1, and 0x80000000 represents −2,147,483,648. The sum of these is −2,147,483,649. That does not fit in 32 bits, so it overflows. The computed result will be 0x7FFFFFFF, which represents 2,147,483,647.

    At the bit-level: FFFFFFFF plus 80000000 produces 17FFFFFFF, which means 7FFFFFFF is stored in the 32-bit destination, and the 1 is a carry out.