8086 Assembly Program to Add Two 16-bit Numbers

In this blog post, we’ll delve into the world of 8086 assembly language to explore a fundamental arithmetic operation: adding two 16-bit numbers.

; Data Segment

data segment
    a dw 0202h      ; 16-bit variable 'a' initialized with 0202h
    b dw 0408h      ; 16-bit variable 'b' initialized with 0408h
    c dw ?          ; 16-bit variable 'c' to store the result
data ends

; Code Segment

code segment
assume cs:code, ds:data
start:
    mov ax, data    ; Load the address of the data segment into AX
    mov ds, ax      ; Initialize the data segment register

    mov ax, a       ; Load value of 'a' into AX
    mov bx, b       ; Load value of 'b' into BX
    add ax, bx      ; Perform addition (AX = AX + BX)

    mov c, ax       ; Store the result in 'c'

    int 3           ; Halt program execution for debugging

code ends
end start           ; Mark the end of the program

16-bit addition in 8086: It’s like learning a new language, except the language is binary and the grammar is brutal ๐Ÿ˜‰



Understanding the code:

Data Segment

  • a dw 0202h: This line declares a 16-bit word variable named a and initializes it with the hexadecimal value 0202.
  • b dw 0408h: Similarly, this declares a 16-bit word variable b and initializes it with the hexadecimal value 0408.
  • c dw ?: This declares a 16-bit word variable c but does not initialize it. It will store the result of the addition operation.

Code Segment

  • mov ax,data: This instruction moves the address of the data segment into the AX register.
  • mov ds,ax: This sets the Data Segment (DS) register to the value in AX, effectively making the data segment accessible for data operations.
  • mov ax,a: This moves the value of a (0202h) into the AX register.
  • mov bx,b: This moves the value of b (0408h) into the BX register.
  • add ax,bx: This adds the contents of BX to the contents of AX. The result (060Ah) is stored in AX.
  • mov c,ax: This moves the result from AX to the variable c.
  • int 3: This is a breakpoint instruction that causes the program to halt, allowing for debugging and inspection of registers and memory.

Flowchart

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.
  3. Result Storage: Stores the result from AX into the variable c.
  4. Program Termination: Halts the program execution using the int 3 instruction.

In essence, this code adds two 16-bit hexadecimal numbers (0202h and 0408h) and stores the result (060Ah) in the variable c.


Output

C:\TASM>masm an16add.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.
 
Object filename [an16add.OBJ]:
Source listing  [NUL.LST]:
Cross-reference [NUL.CRF]:
 
  50402 + 450254 Bytes symbol space free
 
      0 Warning Errors
      0 Severe  Errors
 
C:\TASM>link an16add.obj
 
Microsoft (R) Overlay Linker  Version 3.60
Copyright (C) Microsoft Corp 1983-1987.  All rights reserved.
 
Run File [AN16ADD.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment
 
C:\TASM>debug an16add.exe
-g
 
AX=060A  BX=0408  CX=0022  DX=0000  SP=0000  BP=0000  SI=0000  DI=0000
DS=0B97  ES=0B87  SS=0B97  CS=0B98  IP=0011   NV UP EI PL NZ NA PE NC
0B98:0011 CC            INT     3
-d 0B97:0000
0B97:0000  02 02 08 04 0A 06 00 00-00 00 00 00 00 00 00 00   ................
0B97:0010  B8 97 0B 8E D8 A1 00 00-8B 1E 02 00 03 C3 A3 04   ................
0B97:0020  00 CC 86 72 FF 77 15 8A-86 70 FF 2A E4 50 B8 FD   ...r.w...p.*.P..
0B97:0030  05 50 FF 36 24 21 E8 77-63 83 C4 06 FF 36 24 21   .P.6$!.wc....6$!
0B97:0040  B8 0A 00 50 E8 47 5E 83-C4 04 5E 8B E5 5D C3 90   ...P.G^...^..]..
0B97:0050  55 8B EC 81 EC 84 00 C4-5E 04 26 80 7F 0A 00 74   U.......^.&....t
0B97:0060  3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC   >.F..V..F..V..^.
0B97:0070  26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8   &.G.*[email protected].
-q
 
C:\TASM>


Understanding the Memory Dump

The memory dump provided by the -d 0B97:0000 command displays the contents of memory starting from the DS (data segment) base address, which is 0B97:0000 in this case.

Here is the relevant portion of the memory:

0B97:0000  02 02 08 04 0A 06 00 00-00 00 00 00 00 00 00 00

Here, 1A 06 is the result of a + b = 0202h + 0408h = 060Ah, stored in c. In the memory dump, values are stored in little-endian format, meaning the least significant byte appears first. Hence the result 060A is stored as 0A 06 in memory.

25 thoughts on “8086 Assembly Program to Add Two 16-bit Numbers”

    1. DW stands for define word. When we require 16 bit date, we use DW.
      DB stands for define byte. When 8bit data is required, we use DW.

    2. These are assembler directives
      dw = define word
      db = define byte

      used to specify the size of the data variable

    1. int 3 and int 21h both works fine, after all int 3 is used to terminate statements above it and int 21h is used to terminate whole program

Leave a Reply

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