-2

So I have this declaration in .bss

answer resb 1

In answer, I store the results of the sum of 2 digit integers ranging from -99 to +99. When I try adding +99 and +99, the answer becomes negative. Any answer within the -128 to 127 range are inputted correctly. How do I resolve this overflow/underflow issue?

I am just beginning in Assembly coding so help me understand this issue. I've tried making resb as resw but my code just produced an "floating point error" when ran so I reverted to using resb.

How i print the answer

;ansh,anso,anst are all declared as resb 1 in .bss
print_answer: ;tens1 holds the answer always
;getting each digit of tens2 to be converted as char    
mov al,[answer]
mov bl,100
div bl

mov [ansh],al ; holds tens digit of average
mov [anst],ah

mov ah,0
mov al,[anst]
mov bl,10
div bl

mov [anst],al
mov [anso],ah

add byte[ansh],30h
add byte[anst],30h
add byte[anso],30h

;printing routine follows here
ejandra
  • 336
  • 2
  • 4
  • 15

1 Answers1

0

Adding -99 and +99 should work just fine in 8 bit registers. If it doesn't, either the addition is not being done correctly, or the display of the result is mishandled.

A common display error is calling a library function with the 8 bit result but passing it to a function expecting a 16-bit (or wider) value. Be sure to mask off or zero the bits you did not generate when creating the value to pass to the library function.

When doubling +99, of course the result is greater than 127, so you would either have to handle the result as an unsigned 8-bit value, or extend it to a wider value. How are you viewing the result?

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Adding +99 and -99 works fine, but +99 and +99 doesn't as I stated in my question. – ejandra Sep 17 '14 at 21:14
  • @nowrn29: Oh sorry! I missed the subtly and have amended my answer. – wallyk Sep 17 '14 at 21:16
  • First i divide answer by 100, then i store the answer as the hundreds digit, then i divide the remainder from that operation with 10 and store the answer as the tens digit and the remainder as the ones digit. As for the symbol of the answer, i have a conditional statement that checks it. I'll edit my post and show it. – ejandra Sep 17 '14 at 21:21
  • how do I extend it to a wider value? – ejandra Sep 17 '14 at 21:33