Addition of two matrices (3x3)

In the following program example we will add two matrices (3x3) and save the result in a third matrix. Then, in a loop, we will output the third matrix to the output device.

Program source code

; Author: Evgeny Sergeevich Dovgopolov

matrix1: db 01h, 02h, 03h
         db 04h, 05h, 06h
         db 07h, 08h, 09h

matrix2: db 01h, 02h, 03h
         db 04h, 05h, 06h
         db 07h, 08h, 09h

matrix3: db 00h, 00h, 00h
         db 00h, 00h, 00h
         db 00h, 00h, 00h

main:
    lxi     sp, 0100h   ; Initialize the stack pointer at address 0100h

    lxi     h, matrix1  ; Load the address of the variable matrix1 into the HL register pair
    lxi     d, matrix2  ; Load the address of the variable matrix2 into the DE register pair
    lxi     b, matrix3  ; Load the address of the variable matrix3 into the BC register pair
    mvi     a, 09h      ; Set the loop counter A to 9

AddLoop:
    push    psw         ; Push the contents of the program status word PSW onto the stack

    ldax    d           ; Load the value at the memory address (matrix2) into accumulator A
    add     m           ; Add the value in register A to the value in the memory location
    stax    b           ; Store the value in A at the memory address pointed to by BC

    inx     h           ; Increment the HL register pair by 1
    inx     d           ; Increment the value of the DE register pair by 1
    inx     b           ; Increment the value of the BC register pair by 1

    pop     psw         ; Pop the value from the stack into register PSW
    dcr     a           ; Decrement the A counter register by 1
    jnz     AddLoop     ; Jump to the label AddLoop until register A becomes 0

    lxi     h, matrix3  ; Load the address of the variable matrix3 into the HL register pair
    mvi     c, 09h      ; Set the loop counter C to 9

PrintLoop:
    mov     a, m        ; Copy the memory value pointed to by the HL register pair into A
    out     0Ah         ; Send the data to the output device through port 10h

    inx     h           ; Increment the HL register pair by 1
    dcr     c           ; Decrement the C counter register by 1
    jnz     PrintLoop   ; Jump to the label PrintLoop until register C becomes 0

    hlt                 ; Stop program execution

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".