8086 assembly language program to check if entered number is odd or even Edit

Murugan Andezuthu Dharmaratnam | 01 March 2021 | 1333

Here is a sample code is written in the 8086 assembly language to take a number as input from the keyboard and check if the number is odd or even. Here we will use dos interrupt 21h function 01 to take a character input from the keyboard. Validation is done to check if the entered character is a number. 0x30h is the character code for zero, and 0x39h is the character code for 9. so to validate we compare if the character entered is between 0x30h and 0x9h. If it's a valid input, the next thing to do is to subtract 0x30h from entered character to convert the character to numeric. Then we use the SHR instruction to check if it's odd or even and the message is displayed based on even or odd. We use the function 4ch dos interrupt 21h to terminate and return to DOS.

Check this link to see the ascii character table

Code to check if the input is odd or even

.model tiny
.code
org 100h


main proc near
    
validate:    
    mov ah,09h  ; dos function to display a string
    mov dx,offset messageinput   ; offset of input message
    int 21h     ; dos interrupt 21h
    
    mov ah,01h  ; function to Read character from keyboard
    int 21h     ; dos interrupt 21h
    
    cmp al,30h
    jle validate
    
    cmp al,39h
    jge validate 
    
    
    sub al,30h  ; hax value of character 0 is 30, to convert it into number we have to decrease it by 30h
    
    
    shr al,1    ; SHR shifts the bits within the destination operand to the right, by value specified in second parameter. 
    jc odd      ; jump to odd if cary flag is set
    
    
    mov ah,09h  ; dos function to display a string
    mov dx,offset messageeven   ; offset of message even
    int 21h     ; dos interrupt 21h
    
    jmp exit    ; after displaying even message jump to exit to terminte the program 
    
    
odd:   
    mov ah,09h  ; ; dos function to display a string
    mov dx,offset messageodd    ; offset of odd message
    int 21h     ; dos interrupt 21h
        

exit:
    mov ah,4ch  ; dos function to terminate and return to dos
    mov al,00
    int 21h     ; dos interrupt 21h
    ret

endp
messageinput db 0ah,0dh,"Please enter a number $"
messageodd db 0ah,0dh,"Entered Number is odd$"
messageeven db 0ah,0dh,"Entered Number is even$"
end main