Hi,
Here is a set of examples. Hope this makes some kind of sense.
It is in the form of an 8086 assembly program in DEBUG.
Regards,
Steve N.
PAGE ,132
TITLE Carry and Overflow Question.
CODE SEGMENT
ASSUME CS:CODE,DS:CODE
ORG 100H ; COM file opening
START:
COMMENT |
For the X86 architecture a carry is set when an unsigned number
operation exceeds the allowed size of the number. Unsigned 16-bit
numbers range from zero to 65,535. For a 16-bit number a carry will
be generated when two numbers added exceed 65,535. Or when subtracting
a larger number from a smaller number to generate a result that would
be smaller than zero. In school terms, adding 6 to 5 would generate a
1 and a carry into the tens column. Subtracting 6 from 5 would make a
9 and a borrow from the next larger column. Example; add 40,000 and
30,000. |
MOV AX,40000
MOV BX,30000
ADD AX,BX ; Add 30,000 to 40,000 and place result into AX.
NOP
COMMENT |
Show result in DEBUG.EXE.
D:\MASM\TEMP>debug
carry.com
-u
1AF9:0100 B8409C MOV AX,9C40 ; 09C40H = 40,000
1AF9:0103 BB3075 MOV BX,7530 ; 07530H = 30,000
1AF9:0106 03C3 ADD AX,BX
-g 106 ; Go to ADD instruction.
AX=9C40 BX=7530 CX=0008 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000
DS=1AF9 ES=1AF9 SS=1AF9 CS=1AF9 IP=0106 NV UP EI PL NZ NA PO NC
1AF9:0106 03C3 ADD AX,BX
-t ; Execute ADD instruction.
AX=1170 BX=7530 CX=0008 DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000
DS=1AF9 ES=1AF9 SS=1AF9 CS=1AF9 IP=0108 NV UP EI PL NZ NA PO CY
Note that the result is 01170H (= 4,464), which is 70,000 MOD 65,536.
And the carry bit is set, indicating the result should be 011170H
(= 70,000), which does not fit into 16 bits.
On the 8086, an overflow is generated when a signed number operation
exceeds the allowed values of the register. Signed 16-bit numbers
range from -32,768, though zero, to +32,767. So if adding two positive
numbers exceed 32,767, an overflow happens. And if adding two negative
numbers makes a result less than -32,768, an overflow (underflow?) is
generated. Example; add 10,000 and 30,000.
|
MOV AX,10000
MOV BX,30000
ADD AX,BX ; Add 30,000 to 10,000 and place result into AX.
COMMENT |
1AF9:0109 B81027 MOV AX,2710 ; 02710H = 10,000
1AF9:010C BB3075 MOV BX,7530 ; 07530H = 30,000
1AF9:010F 03C3 ADD AX,BX
-g10f ; Go to ADD instruction.
AX=2710 BX=7530 CX=001A DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000
DS=1AF9 ES=1AF9 SS=1AF9 CS=1AF9 IP=010F NV UP EI PL NZ NA PO CY
1AF9:010F 03C3 ADD AX,BX
-t ; Execute ADD instruction.
AX=9C40 BX=7530 CX=001A DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000
DS=1AF9 ES=1AF9 SS=1AF9 CS=1AF9 IP=0111 OV UP EI NG NZ NA PO NC
The result is 09C40H which is -25,535, so an overflow occured.
No carry though as 40,000 is still a valid unsigned number.
|
MOV AX,10000
MOV BX,-30000
ADD AX,BX ; Add -30,000 to 10,000 and place result into AX.
COMMENT |
-g117 ; Go to ADD instruction.
AX=2710 BX=8AD0 CX=001A DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000
DS=1AF9 ES=1AF9 SS=1AF9 CS=1AF9 IP=0117 OV UP EI NG NZ NA PO NC
1AF9:0117 03C3 ADD AX,BX
-t ; Execute ADD instruction.
AX=B1E0 BX=8AD0 CX=001A DX=0000 SP=FFFE BP=0000 SI=0000 DI=0000
DS=1AF9 ES=1AF9 SS=1AF9 CS=1AF9 IP=0119 NV UP EI NG NZ NA PO NC
COMMENT |
Adding -30,000 to 10,000 (Signed) to get -20,000, or adding 35,536
to 10,000 (unsigned) to get 45,536 is valid so no carry or overflow
occurs.
|
NOP
CODE ENDS
END START