.MODEL SMALL      ; Define the memory model as small (single code and data segments)
.STACK 100H      ; Allocate 256 bytes for the stack

.DATA             ; Start of the data segment
MSG1 DB 10, 13, "ENTER A POSITIVE NUMBER (0-9): $" ; Message to prompt the user for input
                ; 10 is the ASCII for line feed (new line)
                ; 13 is the ASCII for carriage return (move cursor to beginning of line)
                ; "..." is the string to display, $ marks the end for DOS function 09h
MSG2 DB 10, 13, "THE NUMBER IS EVEN$"          ; Message to display if the number is even
MSG3 DB 10, 13, "THE NUMBER IS ODD$"           ; Message to display if the number is odd


.CODE             ; Start of the code segment
MAIN PROC         ; Define the main procedure (entry point of the program)
    MOV AX, @DATA   ; Load the address of the data segment into the AX register
    MOV DS, AX      ; Copy the data segment address from AX to the DS (Data Segment) register
                    ; This makes the data defined in the .DATA section accessible

    LEA DX, MSG1    ; Load the Effective Address (memory address) of MSG1 into the DX register
                    ; DX is used by DOS function 09h to point to the string to display
    MOV AH, 09H     ; Load 09h into the AH register. 09h is the DOS function code to display a string
    INT 21H         ; Call the DOS interrupt handler. This will display the string pointed to by DX

    MOV AH, 1       ; Load 01h into the AH register. 01h is the DOS function code to read a single character from the keyboard
    INT 21h         ; Call the DOS interrupt handler. The ASCII value of the typed character will be stored in AL register

CHECK:              ; Label marking the start of the even/odd check
    MOV DL, 2       ; Load the value 2 into the DL register. DL will be the divisor
    DIV DL          ; Divide the value in AL (the input character's ASCII) by the value in DL (2)
                    ; The quotient (result) is stored in AL, and the remainder is stored in AH
    CMP AH, 0       ; Compare the remainder in AH with 0
    JNE ODD         ; Jump if Not Equal (JNE). If the remainder is not 0, jump to the label ODD (meaning the number is odd)

EVEN:               ; Label reached if the remainder of the division was 0 (meaning the number is even)
    LEA DX, MSG2    ; Load the address of the "THE NUMBER IS EVEN$" message into DX
    MOV AH, 09H     ; Load 09h into AH to prepare for displaying a string
    INT 21h         ; Call DOS to display the even message
    JMP EXIT        ; Unconditionally jump to the EXIT label, skipping the ODD block

ODD:                ; Label reached if the remainder of the division was not 0 (meaning the number is odd)
    LEA DX, MSG3    ; Load the address of the "THE NUMBER IS ODD$" message into DX
    MOV AH, 09H     ; Load 09h into AH to prepare for displaying a string
    INT 21h         ; Call DOS to display the odd message
    JMP EXIT        ; Unconditionally jump to the EXIT label

EXIT:               ; Label marking the end of the program logic
    MOV AH, 4CH     ; Load 4Ch into AH. 4Ch is the DOS function code to terminate the program
    INT 21h         ; Call DOS to terminate the program

MAIN ENDP         ; Mark the end of the MAIN procedure
END MAIN            ; Mark the end of the assembly program and the entry point (MAIN)
Share.

I am a Full-Stack Web Developer & Security Analyst from Bangladesh. I have built web/online applications on various Open Source Stacks and love information security testing.

Leave A Reply