Squaring each element of an array

In the following program example we will square each element of an array. We will use a table of squares to compute the effective address. In this program register L will serve both as the loop counter and as a pointer to an element of the table variable.

Program source code

; Author: Evgeny Sergeevich Dovgopolov

array: db 3, 8, 4, 7, 5, 9, 2, 1, 6, 10
table: db 00h , 01h, 04h, 09h, 10h, 19h, 24h, 31h, 40h, 51h, 64h, 79h, 90h, 0A9h,
       db 0C4h, 0E1h

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

    lxi     b, array    ; Load the address of the variable array into the BC register pair
    lxi     d, table    ; Load the address of the variable table into the DE register pair
    mvi     l, 0Ah      ; Set the loop counter L to 10

SquareLoop:
    push    h           ; Temporarily push the value of the HL register pair onto the top of the stack

    ldax    b           ; Load the value at the memory address (array) into accumulator A
    mov     l, a        ; Copy the value of register A into register L
    mvi     h, 00h      ; Clear register H
    dad     d           ; Add the contents of the DE register pair to the contents of the HL register pair
    mov     a, m        ; Copy the memory value pointed to by the HL register pair into A
    inx     b           ; Increment the value of the BC register pair by 1

    pop     h           ; Pop the temporarily saved value from the stack into the HL register pair
    dcr     l           ; Decrement the L counter register by 1
    jnz     SquareLoop  ; Jump to the label SquareLoop until L becomes 0

    hlt                 ; Stop program execution

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