Answer to Question #348964 in Assembler for ghmkjcc

Question #348964

Create an assembly program to that accepts 2 single digit number (0-9) num1 and num2.



If num is greater that then display "Num1 is greater than Num 2" while it num? is lesser than then display "Num! is less than Num2 otherwise display "Num1 and Num2 are equal" Add another instruction for invalid input (alphabets, symbols

1
Expert's answer
2022-06-07T17:38:01-0400
DATA SEGMENT
 
MSG_EnterN1    DB 10,13,"Enter the first number: $"
MSG_EnterN2    DB 10,13,"Enter the second number: $"

greater    DB 10,13,"Num1 is greater than Num2$"
equal    DB 10,13,"Num1 and Num2 are equal$"
less    DB 10,13,"Num1 is less than Num2$"
invalid    DB 10,13,"Invalid input.",0ah,0dh,'$'
DATA ENDS

CODE SEGMENT

ASSUME DS:DATA,CS:CODE
START:
    MOV AX,DATA
    MOV DS,AX
    MOV ES,AX    

    LEA DX,MSG_EnterN1        ; "Enter the first number:"
    MOV AH,9            ; Display string
    INT 21H

    XOR BX,BX
    MOV AH,1            ; Waiting for a key press
    INT 21H
    
   cmp al,'0'    
   jl _wrong            ; char < '0'
   cmp al,'9'
   jg _wrong            ; char > '9'
   
; AL = ASCII code of key pressed
    SUB AL, 30H            ; AL NUMBER
    add BL,AL            ; BL = n1

    LEA DX,MSG_EnterN2        ; "Enter the second number: "
    MOV AH,9            ; Display string
    INT 21H

    MOV AH,1            ; Waiting for a key press
    INT 21H
   cmp al,'0'    
   jl _wrong            ; char < '0'
   cmp al,'9'
   jg _wrong            ; char > '9'    
    
; AL = ASCII code of key pressed
    SUB AL, 30H            ; AL NUMBER
    add BH,AL
    
; compare
    cmp bl, bh
    jg _greater
    jl _less
    
    LEA DX,equal        ;  
    MOV AH,9            ; Display string
    INT 21H
    jmp final
_greater:
    LEA DX,greater        ;  
    MOV AH,9            ; Display string
    INT 21H
    jmp final
_less:    
    LEA DX,less        ;  
    MOV AH,9            ; Display string
    INT 21H
    jmp final    
 
_wrong:
   mov dx, OFFSET invalid
       mov ah, 9
    int 21h      
 
final:    
    mov ah, 4ch
    int 21h


CODE ENDS
END START



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS