In this blog post, we will discuss how to multiply two 32-bit numbers using 8086 assembly language. Since the 8086 microprocessor is 16-bit, handling 32-bit multiplication requires breaking it into multiple steps and managing carries properly. Let’s dive into the implementation.
data segment abc dd 12345678H def dd 12345678H ghi dq ? data ends code segment assume cs:code, ds:data start: mov ax, data mov ds, ax mov ax, word ptr abc mul word ptr def mov word ptr ghi, ax mov cx, dx mov ax, word ptr abc+2 mul word ptr def add cx, ax mov bx, dx jnc move add bx,0001H move: mov ax,word ptr abc mul word ptr def+2 add cx, ax mov word ptr ghi+2, cx mov cx,dx jnc ma add bx, 0001H ma: mov ax, word ptr abc+2 mul word ptr def+2 add cx, ax jnc mb add dx, 0001H mb: add cx, bx mov word ptr ghi+4, cx jnc mc add dx, 0001H mc: mov word ptr ghi+6, dx int 3 code ends end start
Step-by-Step Explanation:
1. Data Segment:
- abc dd 12345678H → Defines a double-word (32-bit) variable
abc
initialized with12345678H
. - def dd 12345678H → Defines another double-word variable
def
with the same value. - ghi dq ? → Reserves a quad-word (64-bit) memory location
ghi
to store the result.
2. Code Segment:
Initialization:
MOV AX, DATA
→ Loads the address of the data segment intoAX
.MOV DS, AX
→ SetsDS
register to point to the data segment, enabling access to variables.
Multiplication of Lower 16 bits of abc
and def
:
MOV AX, WORD PTR abc
→ Loads the lower 16 bits ofabc
intoAX
.MUL WORD PTR def
→ MultipliesAX
with the lower 16 bits ofdef
.- The result is stored in
DX:AX
(DX
contains the upper 16 bits,AX
the lower 16 bits).
- The result is stored in
MOV WORD PTR ghi, AX
→ Stores the lower 16 bits of the result intoghi
.MOV CX, DX
→ Stores the upper 16 bits of the result intoCX
.
Multiplication of Higher 16 bits of abc
with Lower 16 bits of def
:
MOV AX, WORD PTR abc+2
→ Loads the upper 16 bits ofabc
intoAX
.MUL WORD PTR def
→ MultipliesAX
with the lower 16 bits ofdef
.- Again, the result is stored in
DX:AX
.
- Again, the result is stored in
ADD CX, AX
→ AddsAX
toCX
to accumulate the result.MOV BX, DX
→ Stores the upper 16 bits of this multiplication inBX
.
Handling Carry:
JNC move
→ If there is no carry, jump tomove
.ADD BX, 0001H
→ If carry is set, incrementBX
by 1.
Multiplication of Lower 16 bits of abc
with Higher 16 bits of def
:
move: MOV AX, WORD PTR abc
→ Reloads the lower 16 bits ofabc
intoAX
.MUL WORD PTR def+2
→ MultipliesAX
with the upper 16 bits ofdef
.ADD CX, AX
→ Adds the result toCX
.MOV WORD PTR ghi+2, CX
→ Stores the updated value inghi+2
.MOV CX, DX
→ UpdatesCX
withDX
(upper 16 bits of the result).
Handling Carry Again:
JNC ma
→ If no carry, jump toma
.ADD BX, 0001H
→ If carry exists, incrementBX
by 1.
Multiplication of Upper 16 bits of abc
and def
:
ma: MOV AX, WORD PTR abc+2
→ Loads the upper 16 bits ofabc
intoAX
.MUL WORD PTR def+2
→ Multiplies it with the upper 16 bits ofdef
.ADD CX, AX
→ Adds the result toCX
.
Final Carry Handling:
JNC mb
→ If no carry, jump tomb
.ADD DX, 0001H
→ If carry exists, incrementDX
by 1.
Storing Final Results:
mb: ADD CX, BX
→ AddsBX
(carry from previous steps) toCX
.MOV WORD PTR ghi+4, CX
→ Stores this result inghi+4
.JNC mc
→ If no carry, jump tomc
.ADD DX, 0001H
→ If carry exists, incrementDX
by 1.mc: MOV WORD PTR ghi+6, DX
→ Stores the final value inghi+6
.
Program Termination:
INT 3
→ Triggers an interrupt, signaling the end of execution.
Overall Process:
- The program initializes the data and code segments, setting up registers to access variables.
- It performs a 32-bit multiplication of two double-word values (
abc
anddef
) to produce a 64-bit result, which is stored inghi
. - The multiplication is broken into four partial multiplications involving the lower and upper 16-bit segments of
abc
anddef
. - After each multiplication, the results are accumulated carefully while handling carry propagation to ensure correctness.
- The computed 64-bit result is stored across
ghi
,ghi+2
,ghi+4
, andghi+6
. - The program terminates by triggering an interrupt (
INT 3
).
Output
C:\TASM>masm an_32mul.asm Microsoft (R) Macro Assembler Version 5.00 Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved. Object filename [an_32mul.OBJ]: Source listing [NUL.LST]: Cross-reference [NUL.CRF]: 50278 + 450378 Bytes symbol space free 0 Warning Errors 0 Severe Errors C:\TASM>link an_32mul.obj Microsoft (R) Overlay Linker Version 3.60 Copyright (C) Microsoft Corp 1983-1987. All rights reserved. Run File [AN_32MUL.EXE]: List File [NUL.MAP]: Libraries [.LIB]: LINK : warning L4021: no stack segment C:\TASM>debug an_32mul.exe -g AX=5A90 BX=0626 CX=66DC DX=014B SP=0000 BP=0000 SI=0000 DI=0000 DS=0B97 ES=0B87 SS=0B97 CS=0B98 IP=0052 NV UP EI PL NZ NA PO NC 0B98:0052 CC INT 3 -d 0B97:0000 0B97:0000 78 56 34 12 78 56 34 12-40 D8 F4 1D DC 66 4B 01 [email protected]. 0B97:0010 B8 97 0B 8E D8 A1 00 00-F7 26 04 00 A3 08 00 8B .........&...... 0B97:0020 CA A1 02 00 F7 26 04 00-03 C8 8B DA 73 03 83 C3 .....&......s... 0B97:0030 01 A1 00 00 F7 26 06 00-03 C8 89 0E 0A 00 8B CA .....&.......... 0B97:0040 73 03 83 C3 01 A1 02 00-F7 26 06 00 03 C8 73 03 s........&....s. 0B97:0050 83 C2 01 03 CB 89 0E 0C-00 73 03 83 C2 01 89 16 .........s...... 0B97:0060 0E 00 CC 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC .....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
The memory dump displayed in the DEBUG session shows the contents of memory after running the program. Let’s analyze the key parts.
0B97:0000 78 56 34 12 78 56 34 12 40 D8 F4 1D DC 66 4B 01
Address Range | Content | Description |
---|---|---|
0B97:0000 – 0B97:0007 | 78 56 34 12 78 56 34 12 | These are the values of abc and def , both set to 12345678H . |
0B97:0008 – 0B97:000F | 40 D8 F4 1D DC 66 4B 01 | This represents the 64-bit result stored in ghi . |
SHOKAM
i am still getting some interrupts
yeah me too
why do you use word ptr? and there is no way to use eax, ebx register to do more easy?