This blog post will guide you through a MASM (Microsoft Macro Assembler) program that performs the addition of two 8-bit numbers. While this is a fundamental operation, it demonstrates crucial assembly programming concepts such as data handling, register usage, and memory storage. Let’s dive in!
Assembly Code
.model small
.data
a db 09h
b db 02h
c dw ?
.code
main proc
mov ax, @data
mov ds, ax
mov al, a
mov bl, b
add al, bl
mov ah, 0
mov c, ax
int 3 ; Breakpoint interrupt
mov ax, 4C00h ; Exit program
int 21h
main endp
end main
Understanding the Code
The program consists of two main sections: the data segment and the code segment.
Data Segment:
a db 09h→ Defines an 8-bit variableawith the value09h.b db 02h→ Defines an 8-bit variablebwith the value02h.c dw ?→ Defines a 16-bit variablecto store the result, ensuring space for potential overflow.
Code Segment:
mov ax, @data→ Loads the address of the data segment into theaxregister.mov ds, ax→ Sets theds(Data Segment) register to access the defined variables.mov al, a→ Loads the value ofainto thealregister.mov bl, b→ Loads the value ofbinto theblregister.add al, bl→ Adds the value ofbltoal, storing the sum inal.mov ah, 0→ Clears the upper byte to store the result correctly inax.mov c, ax→ Stores the computed sum in the memory locationc.int 3→ Interrupt for debugging purposes.mov ax, 4C00h→ Terminates the program cleanly using DOS interrupt21h.
Flowchart

High-Level Overview
- Data Initialization: Declares two 8-bit variables (
aandb) and reserves memory for the sum (c). - Segment Setup: Loads and configures the data segment to access variables.
- Value Loading: Retrieves values of
aandbinto CPU registers. - Addition Operation: Computes the sum using the
ADDinstruction. - Result Storage: Saves the sum in a memory location.
- Program Termination: Stops execution using
int 3and DOS exit call.
Running the Program
To assemble, link, and execute the program, follow these steps:
- Assemble the Code:
masm masm_add.asm; - Link the Object File:
link masm_add.obj; - Run in Debugger:
debug masm_add.exe
Memory Dump Analysis
After running the program, inspecting the memory reveals:
09 02 0B 00 00 00 00 00-00 00 00 00 00 00 00 00
09: Representsa.02: Representsb.0B: Represents the computed sum0Bhstored inc.
Conclusion
This MASM assembly program demonstrates the core concepts of handling 8-bit arithmetic in assembly language. Though simple, it forms the foundation for more complex operations in low-level programming.
Happy coding! ⚡