We help IT Professionals succeed at work.

# How to check overflow

on
Hi
I have a question about assembly language. How do I check overflow in arithmetic operations ? Assume that all operands and registers have word size.

A, B, c are memory locations and ax, and dx are registers
(ax can't hold more than 2 ^ 15 - 1 = 32676)

Assembly codes:
mov  A, 4681
mov  B, 7
mov  C, 1
mov  ax, A   ;ax set to 4681
imul B       ;ax = 4681 * 7 = 32767. Still OK
add  ax, C   ;ax = 32678. Overflow occurs
;ax = -32767. Value of ax is wrapped around
To check for overflow, I used:
cmp ax, 0
jge NoOverFlow    ;if ax >= 0, there is no overflow
cmp dx, 0
jne NoOverFlow    ;if ax < 0, dx = 0, there is overflow
jmp OverFlow

It worked, however it doesn't work with:

mov  A, 15
mov  B, 0
mov  C, -100
mov  ax, A
imul B

Any ideas, suggestions ?
Thanks

Comment
Watch Question

## View Solution Only

Senior Software Engineer

Commented:
You might want to try posting this in the Programming Software & Languages(General) Topic area.

http://www.experts-exchange.com/jsp/qList.jsp?ta=progsoftgen

Commented:
It's been a long while, but I believe--

JO, should jump if overflow
JNO, if not.

Commented:
See the following for a quick reference on the flag registers and conditional branching:

http://userpages.wittenberg.edu/bshelburne/Comp255/Intel02.htm

Commented:
>> JO, should jump if overflow
>> JNO, if not.
It depends on the aritmatic in question, but in this case that is correct.

Note that if there is an overflow the result will not be 0, it will be the portion of the result that "fits" in the destiantion (AX), so the

cmp ax, 0

is definitely not correct.