8086 Assembly Program to Subtract Two 16 bit Numbers

Assembly language offers a hands-on approach to understanding how computers perform basic arithmetic operations at a low level. In this blog post, we will explore an 8086 assembly program that subtracts two 16-bit numbers.

data segment
a dw 9A88h
b dw 8765h
c dw ?
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,a
mov bx,b
sub ax,bx
mov c,ax
int 3
code ends
end start


Understanding the Code:

Data Segment

  • data segment: Marks the beginning oon highf the data segment where variables are stored.
  • a dw 9A88h: Declares a 16-bit variable a initialized to 9A88h.
  • b dw 8765h: Declares a 16-bit variable b initialized to 8765h.
  • c dw ?: Declares a 16-bit variable c, uninitialized (value is undefined initially).

Flowchart

Code Segment

  • code segment: Marks the beginning of the code segment.
  • assume cs:code,ds:data: Informs the assembler that cs points to the code segment and ds points to the data segment.
  • start: This label marks the program’s entry point.
  • mov ax, data: Loads the starting address of the data segment into the ax register.
  • mov ds, ax: Copies the address in ax to the ds register, setting up ds to point to the data segment.
  • mov ax, a: Loads the value of a (9A88h) into the ax register.
  • mov bx, b: Loads the value of b (8765h) into the bx register.
  • sub ax, bx: Subtracts the value in bx (8765h) from the value in ax (9A88h), storing the result back in ax.
  • The result is: 9A88h – 8765h = 12C3h.
  • mov c, ax: Stores the result of the subtraction (12C3h) into the variable c in the data segment.
  • int 3: Generates a software interrupt, typically used to stop the program or for debugging purposes.
  • code ends: Marks the end of the code segment.
  • end start: Specifies the program’s entry point (start) for the assembler.

On High Level:

  1. Data Initialization: The program defines two 16-bit variables, a and b, with predefined values (9A88h and 8765h, respectively) and reserves space for a third variable, c, to store the subtraction result.
  2. Segment Setup: The program initializes the data segment register (ds) to ensure the processor can access variables stored in the data segment.
  3. Load Values: The values of a and b are loaded into the ax and bx registers, respectively, to perform arithmetic operations.
  4. Perform Subtraction: The value in bx (representing b) is subtracted from the value in ax (representing a), and the result is stored back in ax.
  5. Store Result: The subtraction result is saved into the variable c, making it accessible in memory, and the program halts with an interrupt for debugging or termination.

Output

C:\TASM>masm an16sub.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.

Object filename [an16sub.OBJ]:
Source listing  [NUL.LST]:
Cross-reference [NUL.CRF]:

  50402 + 450254 Bytes symbol space free

      0 Warning Errors
      0 Severe  Errors

C:\TASM>link an16sub.obj

Microsoft (R) Overlay Linker  Version 3.60
Copyright (C) Microsoft Corp 1983-1987.  All rights reserved.

Run File [AN16SUB.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment

C:\TASM>debug an16sub.exe
-g

AX=1323  BX=8765  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 PO NC
0B98:0011 CC            INT     3
-d 0B97:0000
0B97:0000  88 9A 65 87 23 13 00 00-00 00 00 00 00 00 00 00   ..e.#...........
0B97:0010  B8 97 0B 8E D8 A1 00 00-8B 1E 02 00 2B 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


Understanding the Memory Dump

This is the memory dump starting from address 0B97:0000, showing the contents of memory. Here is the breakdown:

0B97:0000 88 9A 65 87 23 13 00 00-00 00 00 00 00 00 00 00 ..e.#………..

  • 88 9A: The value a = 9A88h in little-endian format.
  • 65 87: The value b = 8765h in little-endian format.
  • 23 13: The result of the subtraction 1323h in little-endian format, which is stored in c.

    Leave a Reply

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