Array sorting

In the following program example we will sort an array.

Program source code

; Author: Evgeny Sergeevich Dovgopolov

array: db 05h, 03h, 08h, 01h, 09h, 02h, 07h, 04h, 06h, 0Ah

main:
    mvi     b, 09h     ; Set the outer loop counter B to 9

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

InnerLoop:
    mov     a, m       ; Copy the memory value pointed to by the HL register pair into A
    inx     h          ; Increment the HL register pair by 1
    cmp     m          ; Compare the value in register A with the value in the memory location
    jc      NoSwap     ; If C=1, then jump to the label NoSwap
    jz      NoSwap     ; If Z=1, then jump to the label NoSwap

    mov     d, m       ; Copy the value in memory pointed to by the HL register pair into D
    mov     m, a       ; Copy the memory value pointed to by the HL register pair into A
    dcx     h          ; Decrement the value of the HL register pair by 1
    mov     m, d       ; Copy the value in memory pointed to by the HL register pair into D
    inx     h          ; Increment the HL register pair by 1

NoSwap:
    dcr     c          ; Decrement the C counter register by 1
    jnz     InnerLoop  ; Jump to the label InnerLoop until C becomes 0

    dcr     b          ; Decrement the value of the counter register B by 1
    jnz     OuterLoop  ; Jump to the label OuterLoop until B becomes 0

    hlt                ; Stop program execution

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