In this blog post, we’ll explore an 8086 assembly language program that adds two 16-bit numbers while also checking for a carry flag.
; Data Segment data segment a dw 0FFFFh ; Example value that will cause a carry b dw 0001h ; Example value c dw ? ; To store result carry_flag db 0 ; To store carry (0 or 1) data ends ; Code Segment code segment assume cs:code, ds:data start: mov ax, data mov ds, ax ; Initialize data segment mov ax, a ; Load first number mov bx, b ; Load second number add ax, bx ; Perform addition mov c, ax ; Store result in 'c' jc carry_occurred ; Jump if carry flag is set mov carry_flag, 0 ; No carry, store 0 jmp end_program carry_occurred: mov carry_flag, 1 ; Store carry flag as 1 end_program: int 3 ; Halt program code ends end start
Related Links:
Overall Functionality
The code performs the following steps:
- Initialization:
- Sets up the data segment and initializes variables
a
andb
.
- Sets up the data segment and initializes variables
- Addition:
- Loads the values of
a
andb
into registers AX and BX, respectively. - Adds the two values and stores the result in AX.
- If the sum exceeds
FFFFh
, the carry flag (CF) is set.
- Loads the values of
- Result Storage:
- Stores the result from AX into the variable
c
. - Checks if a carry occurred. If so, sets
carry_flag
to1
; otherwise, sets it to0
.
- Stores the result from AX into the variable
- Program Termination:
- Halts the program execution using the
int 3
instruction.
- Halts the program execution using the
In essence, this code adds two 16-bit hexadecimal numbers (FFFFh
and 0001h
). Since the result exceeds FFFFh
, it wraps to 0000h
and sets the carry flag, indicating an overflow. 🚀
Understanding the Code:
Data Segment:
a dw 0FFFFh
: Declares a 16-bit word variablea
initialized to0FFFFh
(65535 in decimal).b dw 0001h
: Declares a 16-bit word variableb
initialized to0001h
(1 in decimal).c dw ?
: Declares a 16-bit word variablec
to store the result.carry_flag db 0
: A byte variable to store whether a carry occurred (0 = no carry, 1 = carry occurred).
Code Segment:
mov ax, data
→mov ds, ax
: Initializes the data segment.mov ax, a
→mov bx, b
: Loads values ofa
andb
into registers AX and BX.add ax, bx
: Adds BX to AX.- If the sum exceeds
FFFFh
, the carry flag (CF) is set. - The lower 16 bits of the result are stored in AX.
- If the sum exceeds
mov c, ax
: Stores the sum inc
.jc carry_occurred
: If the carry flag is set, jumps tocarry_occurred
.mov carry_flag, 1
: Stores 1 incarry_flag
if there was a carry.mov carry_flag, 0
: Stores 0 incarry_flag
if no carry occurred.int 3
: Stops execution.
Flowchart

Output
C:\TASM>masm addcarry.asm
C:\TASM>link addcarry.obj
C:\TASM>debug addcarry.exe
-g
AX=0000 BX=0001 CX=0022 DX=0000 CF=1
....
0B97:0000 FF FF 01 00 00 00 01 00
Let’s take a closer look at memory dump:
Address | Value | Explanation |
---|---|---|
0B97:0000 | FF FF | Value of a (FFFFh in little-endian format) |
0B97:0002 | 01 00 | Value of b (0001h in little-endian format) |
0B97:0004 | 00 00 | Value of c (result of addition, stored as 0000h due to overflow) |
0B97:0006 | 01 | Value of carry_flag (set to 1 since carry occurred) |
0B97:0007 | 00 | Unused/garbage memory (may contain random data) |