Program that input two single-digit numbers and one arithmetic operator (among: +, -, *, /, %); your program should perform the arithmetic operation accordingly and display the result.
UNIVERSITY OF THE PUNJAB
Third Semester 2017
Examination: B.S. 4 Years Programme
Paper: Computer Organization and Assembly Language
Course Code: IT-203 / 21402
Question # 5:
Write a program that should input two single-digit numbers and store them into registers or variables, your program should also input one arithmetic operator (among: +, -, *, /, %); your program should perform the arithmetic operation accordingly and display the result.
Solution:
INCLUDE Irvine32.inc
.data
msg BYTE "Enter a single-digit number : ",0
error BYTE "Wrong Input!, Please enter a single-digit number : ",0
error2 BYTE "Invalid input!",0
msg2 BYTE "Please enter an arithmetic operator : ",0
result BYTE "Result is : ",0
val1 BYTE ?
val2 BYTE ?
op BYTE ?
res WORD 0
.code
main PROC
mov edx, offset msg
call WriteString
call ReadInt
call ValidateInput
mov val1, al
mov edx, offset msg
call WriteString
call ReadInt
call ValidateInput
mov val2, al
mov ax, 0
mov edx, offset msg2
call WriteString
call ReadChar
call WriteChar
mov op, al
mov bl, '+'
.IF(op=='+')
mov bl, val1
add bl, val2
mov byte ptr res, bl
.ELSEIF(op=='-')
mov bl, val1
sub bl, val2
mov byte ptr res, bl
.ELSEIF(op=='*')
mov al, val1
mul val2
;in al product moves to ax
mov res, ax
.ELSEIF(op=='/')
movzx ax, val1
div val2
mov byte ptr res, al
.ELSEIF(op=='%')
movzx ax, val1
div val2
mov byte ptr res, ah
.ELSE
mov edx, offset error2
call crlf
call WriteString
jmp quit
.ENDIF
call crlf
mov edx, offset result
call WriteString
movzx eax, res
call WriteDec
quit:
call crlf
call WaitMsg
exit
main ENDP
ValidateInput PROC
validate:
.IF(eax >= 0 && eax <= 9)
jmp quit
.ENDIF
BadInput:
mov edx, offset error
call WriteString
call ReadInt
jmp validate
quit:
ret
ValidateInput ENDP
END main
.data
msg BYTE "Enter a single-digit number : ",0
error BYTE "Wrong Input!, Please enter a single-digit number : ",0
error2 BYTE "Invalid input!",0
msg2 BYTE "Please enter an arithmetic operator : ",0
result BYTE "Result is : ",0
val1 BYTE ?
val2 BYTE ?
op BYTE ?
res WORD 0
.code
main PROC
mov edx, offset msg
call WriteString
call ReadInt
call ValidateInput
mov val1, al
mov edx, offset msg
call WriteString
call ReadInt
call ValidateInput
mov val2, al
mov ax, 0
mov edx, offset msg2
call WriteString
call ReadChar
call WriteChar
mov op, al
mov bl, '+'
.IF(op=='+')
mov bl, val1
add bl, val2
mov byte ptr res, bl
.ELSEIF(op=='-')
mov bl, val1
sub bl, val2
mov byte ptr res, bl
.ELSEIF(op=='*')
mov al, val1
mul val2
;in al product moves to ax
mov res, ax
.ELSEIF(op=='/')
movzx ax, val1
div val2
mov byte ptr res, al
.ELSEIF(op=='%')
movzx ax, val1
div val2
mov byte ptr res, ah
.ELSE
mov edx, offset error2
call crlf
call WriteString
jmp quit
.ENDIF
call crlf
mov edx, offset result
call WriteString
movzx eax, res
call WriteDec
quit:
call crlf
call WaitMsg
exit
main ENDP
ValidateInput PROC
validate:
.IF(eax >= 0 && eax <= 9)
jmp quit
.ENDIF
BadInput:
mov edx, offset error
call WriteString
call ReadInt
jmp validate
quit:
ret
ValidateInput ENDP
END main
Let me know in the comment section if you have any question.
Previous Post:
Program that inputs 10 digits, stores them into an array and finds the minimum digit.
Comments
Post a Comment