8086 Assembly Program to Add Two 32 bit Numbers

data segment
abc dd 12345678h
def dd 9ABCDEF0h
ghi dw ?
data ends

code segment
assume cs:code, ds:data
start:
mov ax,data
mov ds,ax
mov dl,00h
mov ax, word ptr abc
mov bx, word ptr def
add ax,bx
mov word ptr ghi,ax
mov ax, word ptr abc+2
mov bx, word ptr def+2
adc ax,bx
mov word ptr ghi+2,ax
jnc move
inc dl
move: mov byte ptr ghi+4,dl
int 3
code ends
end start

Output

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

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

  50288 + 450368 Bytes symbol space free

      0 Warning Errors
      0 Severe  Errors

C:\TASM>link an32add.obj

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

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

C:\TASM>debug an32add.exe
-g

AX=ACF1  BX=9ABC  CX=0038  DX=0000  SP=0000  BP=0000  SI=0000  DI=0000
DS=0B97  ES=0B87  SS=0B97  CS=0B98  IP=0027   NV UP EI NG NZ AC PO NC
0B98:0027 CC            INT     3
-d 0B97:0000
0B97:0000  78 56 34 12 F0 DE BC 9A-68 35 F1 AC 00 00 00 00   xV4.....h5......
0B97:0010  B8 97 0B 8E D8 B2 00 A1-00 00 8B 1E 04 00 03 C3   ................
0B97:0020  A3 08 00 A1 02 00 8B 1E-06 00 13 C3 A3 0A 00 73   ...............s
0B97:0030  02 FE C2 88 16 0C 00 CC-63 83 C4 06 FF 36 24 21   ........c....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

7 thoughts on “8086 Assembly Program to Add Two 32 bit Numbers”

  1. mov ax, word ptr abc + 2
    mov bx, word ptr def + 2
    adc ax, bx ; makes no sense

    mov byte ptr ghi+4, dl ; WTF ghi + 4 actually points outside of the data segment.

    here’s how it’s done:
    jmp @f
    extra dw 0
    n1 dd 0FFFFFFFFh
    n2 dd 0FFFFFFFFh
    @@:
    mov ax, word ptr [n1]
    mov bx, word ptr [n1+2]
    mov cx, word ptr [n2]
    mov dx, word ptr [n2+2]
    add ax, cx
    adc bx, dx
    adc [extra], 0

  2. ; ADDs two 32-bit integers into a 64-bit integer using 16-bit CPU (8086-80286)
    ;
    ; ML ADD.ASM
    ; DEBUG ADD.EXE
    ; -g
    ; -d DS:0 F
    ; -q

    ; NOTE: x86 integers are Little Endian and so appear inverted in memory

    data segment
    a1 dd 0FFFFFFFFh ; 0xFFFFFFFF +
    a2 dd 2h ; 0x00000002 =
    res dq ? 0x0000000100000001
    data ends

    code segment
    assume cs:code, ds:data
    start:
    mov ax, data
    mov ds, ax
    xor dl, dl
    mov ax, word ptr a1 ; add 1st word and store result
    mov bx, word ptr a2
    add ax, bx
    mov word ptr res, ax
    mov ax, word ptr a1+2 ; add 2nd word *with carry* and store result
    mov bx, word ptr a2+2
    adc ax, bx
    mov word ptr res+2, ax
    jnc move
    inc dl ; in case of an overflow, result’s 3rd word becomes 1
    move:
    mov byte ptr res+4, dl
    int 3 ; breaks DEBUG
    mov ax, 4C00h ; terminates DOS program (if called from prompt)
    int 21h
    code ends
    end start

Leave a Reply

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