8086 Assembly Program to Divide Two 16 bit Numbers

When working with assembly language, one of the fundamental operations is division. In this blog post, we will explore an 8086 assembly program that divides two 16-bit numbers using the DIV instruction.

The following program takes two 16-bit numbers stored in memory, performs division, and stores the result.

data segment
a dw 4444h
b dw 0002h
c dw ?
data ends

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

Step-by-Step Explanation:

  1. Data Segment:
    • a is initialized with 4444h (17476 in decimal).
    • b is initialized with 0002h (2 in decimal).
    • c is left uninitialized to store the result.
  2. Code Segment:
    • The MOV AX, DATA and MOV DS, AX instructions set up the data segment.
    • The dividend is moved to the AX register, and the divisor is placed in the BX register.
    • The DIV BX instruction divides AX by BX and stores the quotient in AX and the remainder in DX.
    • The quotient is then stored in memory location c.
    • INT 3 is used to pause execution for debugging.

Flowchart:


Output

C:\TASM>masm an16div.asm
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987.  All rights reserved.
 
Object filename [an16div.OBJ]:
Source listing  [NUL.LST]:
Cross-reference [NUL.CRF]:
 
  50402 + 450254 Bytes symbol space free
 
      0 Warning Errors
      0 Severe  Errors
 
C:\TASM>link an16div.obj
 
Microsoft (R) Overlay Linker  Version 3.60
Copyright (C) Microsoft Corp 1983-1987.  All rights reserved.
 
Run File [AN16DIV.EXE]:
List File [NUL.MAP]:
Libraries [.LIB]:
LINK : warning L4021: no stack segment
 
C:\TASM>debug an16div.exe
-g
 
AX=2222  BX=0002  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  44 44 02 00 22 22 00 00-00 00 00 00 00 00 00 00   DD..""..........
0B97:0010  B8 97 0B 8E D8 A1 00 00-8B 1E 02 00 F7 F3 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 displayed in the DEBUG session shows the contents of memory after running the program. Let’s analyze the key parts.

Memory Dump Output:

0B97:0000  44 44 02 00 22 22 00 00-00 00 00 00 00 00 00 00   DD..""..........

Summary of the Memory Dump Analysis:

AddressHex ValuesInterpretation
0B97:000044 44Dividend (a) = 4444h (Hex) = 17476 (Decimal)
0B97:000202 00Divisor (b) = 0002h (Hex) = 2 (Decimal)
0B97:000422 22Quotient (c) = 2222h (Hex) = 8738 (Decimal)
0B97:0006 - 0B97:000F00 00 00 00 00 00 00 00Unused or uninitialized memory

4 thoughts on “8086 Assembly Program to Divide Two 16 bit Numbers”

        1. but how does the assembler know that it is to take ax as dividend? What if I used another register place of ax? Or what if the ax assignment statement was some line before where it is right now?

Leave a Reply

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