Kyle Bendickson
Kyle Bendickson

Reputation: 318

Dealing with the overflow of a 9bit result to an ADD op that is supposed to store in a byte ptr in x86

I am working on a problem for my introductory computer organization course at UCI and we use x86 assembly language in the course (using Visual Studio and the MS syntax). While the problem is for a homework assignment, we are graded on attempts and not the correctness of our answers.

My question is w.r.t indirect memory addressing. The entire question is stated below:

Suppose EBX = 1000 (in decimal) and the byte values stored in the memory addresses 1000 through 1003 are 255, 255, 255, and 255 (in decimal). What effect do the following instructions have (independently) on this area of memory? Assume for multi-byte data, least significant byte is stored in smallest address.

a. ADD byte ptr [EBX], 1

   ADDRESS | Before | After
     1000  |  255   |          ; The After column is where we place our answers.
     1001  |  255   |          ; All of these values are unsigned
     1002  |  255   |
     1003  |  255   |

b. ADD word ptr [EBX], 1

   ADDRESS | Before | After
     1000  |  255   |   
     1001  |  255   |    
     1002  |  255   |
     1003  |  255   |

c. ADD dword ptr [EBX], 1

   ADDRESS | Before | After
     1000  |  255   |   
     1001  |  255   |    
     1002  |  255   |
     1003  |  255   |

I understand that byte ptr [EBX] will load the value 0xFF and that question (A) essentially boils down to ADD 0xFF, 0x01 using a memory address of one byte. However, what I do not understand is how to deal with the overflow caused by '0x100' which would require a minimum of 9 bits to represent.

The two possible solutions I can determine for question (A) ADD byte ptr [EBX], 1 are as follows:

   ADDRESS | Before | After
     1000  |  255   |  000    ; Overflow does not carry out to address 1001 and the most significant bit is lost
     1001  |  255   |  255
     1002  |  255   |  255
     1003  |  255   |  255

   ADDRESS | Before | After
     1000  |  255   |  000    ; Overflow overwrites the value in the next memory address
     1001  |  255   |  001    ; [base 10]
     1002  |  255   |  255
     1003  |  255   |  255

QUESTION: what effect does the overflow of representing a 9 bit result of an ADD operation in a single byte of memory have on any subsequent memory regions, if any

Any insight that anyone could shed on this topic would be greatly appreciated as I was not able to find any x86 specific information that dealt with the overflow of storing a 9 bit number in a single byte of memory.

Upvotes: 2

Views: 709

Answers (1)

Magoo
Magoo

Reputation: 80023

In each case, the value retrieved from memory (be it byte, word or doubleword) +1 will yield zero and set the overflow flag because the actual result can't be stored in the space available. That zero will be returned to memory and the CPU will have the overflow bit set in its status register. The fact that the overflow bit is set will allow actions to be executed as a result - it does not affect what is stored to memory.

Think hundreds-tens-units (if that's still taught). If you have 9 and add 1, you get 0 with an "overflow". 99+1 gets 00 +overflow; 999+1 gets 000 +overflow

Upvotes: 1

Related Questions