Design a calculator in assembly that can handle +,-,/,%, * and expresses errors in the case of division by zero or invalid characters using the template below.
TITLE MASM Template (main.asm)
; Description: Calculator
; Revision date: April 29, 2014
INCLUDE Irvine32.inc
ENTER_KEY = 13
.data
prompt BYTE "Calculator: Enter an expression", 0dh, 0ah, 0
variableA dWord ?
variableB dWord ?
Problem BYTE "You cannot divide by 0",0dh,0ah,0
.code
calc_add PROC USES eax
mov eax, variableB
add variableA, eax
jno done
; display error
done:
ret
calc_add ENDP
calc_sub PROC
ret
calc_sub ENDP
calc_mul PROC
ret
calc_mul ENDP
calc_div PROC
; check divid by zero, remember to use cdq
ret
calc_div ENDP
calc_rem PROC
; check divid by zero, remember to use cdq
ret
calc_rem ENDP
main PROC
call Clrscr
mov ecx, OFFSET prompt
call WriteString ;print out: "Calculator: Enter an expression"
call Crlf ; new line
;read in the first number StateA: ;read in one digit at a time, continue reading until get a sign
mov eax, 0
call ReadChar
call IsDigit ;check to see if the value entered was a digit
jnz Error
sub al, '0'
mov variableA, eax
jmp StateB
StateB:
mov eax, 0
call ReadChar
cmp al, '+'
jnz L1
mov edx, calc_add
jmp StateC
cmp al, '-'
jnz L1:
mov edx, calc_sub
jmp StateC
; fill up the rest three operators
L1:
call IsDigit ;check to see if the value entered was a digit
jnz Error
sub al, '0'
mov ebx, eax
mov eax, 10
imul variableA
jo Error
mov variableA, eax ;
add variableA, ebx ; variableA = variableA * 10 + ebx
jmp StateB
StateC:
; similar to StateA
StateD:
; similar to StateB, except looking for ENTER
mov eax, 0
call ReadChar
cmp al, ENTER_KEY
jz Done
call isDigit
jnz Error
; fill up this part and compute variableB
jmp StateD
Done:
call edx
; display the result reading WriteInt
exit
main ENDP
END main