Jacob Macallan
Jacob Macallan

Reputation: 979

Adding Bytes and Overflow

So I'm new to assembly programming and I'm undertaking a class right now at my university. I'm curious about adding bytes and words...

If you're given the following addresses ...


Address     Before     
1000         255
1001         255
1002         255
1003         255

And the prompt is "what effect do the following instructions have (independently) on this area of memory ..."

What exactly would happen once I exceed 256 bytes? I believe overflow will catch the significant number, but what will happen in each address because of it?

I know I'm adding

0xFF + 0x01 for a byte, and 0xFF + 0x16 for a word

Upvotes: 0

Views: 1127

Answers (1)

Brendan
Brendan

Reputation: 37214

Different CPUs (and in some cases, different instructions on the same CPU) will do addition and overflows differently. However; there are 4 common cases:

1) The instruction only does unsigned addition, and overflows cause an error condition (e.g. "overflow exception"). In this case 0x7F + 0x01 = 0x80 and 0xFF + 0x01 = error.

2) The instruction only does signed addition, and overflows cause an error condition (e.g. "overflow exception"). In this case 0x7F + 0x01 = error and 0xFF + 0x01 = -1 + 1 = 0x00.

3) The same instruction is used for both signed and unsigned addition, where there's 2 different flags (an "overflow" flag and a "carry" flag) that are set by the instruction, and the instruction doesn't generate an error on its own. In this case (if you care about overflow), after the addition you'd have a second instruction (e.g. a conditional branch) that either tests the "overflow" flag (if the addition was signed) or tests the "carry" (if the addition was unsigned).

4) The instruction does both signed and unsigned addition, and there's no error condition and no flags are set. In this case 0x7F + 0x01 = 0x80 and 0xFF + 0x01 = 0x00.

Note: All examples above assume 8-bit integers with 2's compliment signed numbers. For larger integers it's similar but larger (e.g. 0x7FFF + 0x0001 = 0x8000 and 0xFFFF + 0x0001 = 0x0000).

Upvotes: 1

Related Questions