The 123 sequence

A program that checks whether the sequence of numbers 1, 2, 3 appears anywhere in an array.

Program source code

title Author: Evgeny Sergeevich Dovgopolov

.286C
.model small
.stack 100h

data segment public
    fmtout db  "Result: %s\n\0"
    array  dw  1, 2, 1, 2, 3, 1
    arrlen equ ($-array) / 2
data ends

code segment public                     ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc                ; Include the file with functions

    main proc                           ; Start of the main procedure
        call    SetDataSegment          ; Call the SetDataSegment procedure

        push    arrlen                  ; Save the value of the arrlen variable on the stack
        lea     ax, ds:[array]          ; Load the address of the array into the ax register
        call    Calculate               ; Call the Calculate procedure (return value in ax)

        push    ax                      ; 2nd: save the ax register value on the stack
        lea     ax, ds:[fmtout]         ; 1st: load the address of the fmtout format string into ax
        call    PrintFormatString       ; Call the PrintFormatString procedure
        add     sp, 2                   ; Align the stack

        xor     al, al                  ; Clear the al register
        call    ExitProcess             ; Call the ExitProcess procedure
    main endp                           ; End of the main procedure

    Calculate proc                      ; Start of the Calculate procedure
        push    bp                      ; Save the bp register value on the stack
        mov     bp, sp                  ; Copy the sp register value into bp

        push    bx                      ; Save the bx register value on the stack
        push    cx                      ; Save the value of the cx register on the stack
        push    si                      ; Save the value of the si register on the stack

    data segment
        msg_true  db "True\0"
        msg_false db "False\0"
    data ends

        xchg    bx, ax                  ; Swap the values of the ax and bx registers
        xor     si, si                  ; Clear the index register si
        mov     cx, word ptr ss:[bp+4]  ; Copy the second parameter at [bp+4] on the stack into cx

    @MainLoop:
        cmp     word ptr[bx+si], 1      ; Compare the value at bx indexed by si with 1
        jnz     @NextElement            ; If ZF=0, jump to the @NextElement label

        cmp     word ptr[bx+si+2], 2    ; Compare the value at bx indexed by si+2 with 2
        jnz     @NextElement            ; If ZF=0, jump to the @NextElement label

        cmp     word ptr[bx+si+4], 3    ; Compare the value at bx indexed by si+4 with 3
        jz      @True                   ; If ZF=1, jump to the @True label

    @NextElement:
        incw    si                      ; Call the incw macro. Increment the si index by 2
        loop    @MainLoop               ; Loop while CX≠0

        lea     ax, ds:[msg_false]      ; Load the address of the msg_false variable into the ax register
        jmp     @Done                   ; Jump unconditionally to the Done label

    @True:
        lea     ax, ds:[msg_true]       ; Load the address of the msg_true variable into the ax register

    @Done:
        pop     si                      ; Pop the value from the stack into the si register
        pop     cx                      ; Pop the value from the stack into the cx register
        pop     bx                      ; Pop the value from the stack into bx

        pop     bp                      ; Pop the value from the stack into bp
        ret     2                       ; Return from the procedure with stack alignment of 2 bytes
    Calculate endp                      ; End of the Calculate procedure

code ends                               ; End of the code segment
end main	                            ; End of file. The main procedure is the entry point

Result: True

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