8086 Assembly Program to Add Two 16-bit Numbers with Carry Handling

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

Overall Functionality

The code performs the following steps:

  1. Initialization:
    • Sets up the data segment and initializes variables a and b.
  2. Addition:
    • Loads the values of a and b 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.
  3. Result Storage:
    • Stores the result from AX into the variable c.
    • Checks if a carry occurred. If so, sets carry_flag to 1; otherwise, sets it to 0.
  4. Program Termination:
    • Halts the program execution using the int 3 instruction.

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 variable a initialized to 0FFFFh (65535 in decimal).
  • b dw 0001h: Declares a 16-bit word variable b initialized to 0001h (1 in decimal).
  • c dw ?: Declares a 16-bit word variable c 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, datamov ds, ax: Initializes the data segment.
  • mov ax, amov bx, b: Loads values of a and b 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.
  • mov c, ax: Stores the sum in c.
  • jc carry_occurred: If the carry flag is set, jumps to carry_occurred.
  • mov carry_flag, 1: Stores 1 in carry_flag if there was a carry.
  • mov carry_flag, 0: Stores 0 in carry_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:

AddressValueExplanation
0B97:0000FF FFValue of a (FFFFh in little-endian format)
0B97:000201 00Value of b (0001h in little-endian format)
0B97:000400 00Value of c (result of addition, stored as 0000h due to overflow)
0B97:000601Value of carry_flag (set to 1 since carry occurred)
0B97:000700Unused/garbage memory (may contain random data)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.