8086 MASM Assembly Program for Addition of Two 8-bit Numbers

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 variable a with the value 09h.
  • b db 02h → Defines an 8-bit variable b with the value 02h.
  • c dw ? → Defines a 16-bit variable c to store the result, ensuring space for potential overflow.

Code Segment:

  • mov ax, @data → Loads the address of the data segment into the ax register.
  • mov ds, ax → Sets the ds (Data Segment) register to access the defined variables.
  • mov al, a → Loads the value of a into the al register.
  • mov bl, b → Loads the value of b into the bl register.
  • add al, bl → Adds the value of bl to al, storing the sum in al.
  • mov ah, 0 → Clears the upper byte to store the result correctly in ax.
  • mov c, ax → Stores the computed sum in the memory location c.
  • int 3 → Interrupt for debugging purposes.
  • mov ax, 4C00h → Terminates the program cleanly using DOS interrupt 21h.

Flowchart

High-Level Overview

  1. Data Initialization: Declares two 8-bit variables (a and b) and reserves memory for the sum (c).
  2. Segment Setup: Loads and configures the data segment to access variables.
  3. Value Loading: Retrieves values of a and b into CPU registers.
  4. Addition Operation: Computes the sum using the ADD instruction.
  5. Result Storage: Saves the sum in a memory location.
  6. Program Termination: Stops execution using int 3 and DOS exit call.

Running the Program

To assemble, link, and execute the program, follow these steps:

  1. Assemble the Code:masm masm_add.asm;
  2. Link the Object File:link masm_add.obj;
  3. 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: Represents a.
  • 02: Represents b.
  • 0B: Represents the computed sum 0Bh stored in c.

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! ⚡

Leave a Reply

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