Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a calculator in assembly that can handle +,-,/,%, * and expresses errors

ID: 3555926 • Letter: D

Question

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

Explanation / Answer

.model small .stack 100h .data msg1 db 13,10,13,10, "Enter 1st Number : $" msg2 db 13,10, "Enter 2nd Number : $" msgEr db 13,10, "Error $" msgCh db 13,10, "Press A to ADD , S to SUBTRACT ,D to MULTIPLY, F to DIVIDE, X to EXIT : $ " msgSum db 13,10,13,10, "Sum is : $" msgDif db 13,10,13,10, "Difference is : $" msgDiv db 13,10,13,10, "Quotient is : $" msgMul db 13,10,13,10, "Product is : $" tmp db ? .code start: mov ax, @data mov ds, ax lea dx, msg1 mov ah, 09h int 21h mov bx, 0 start1: mov ah, 01h int 21h cmp al,0dh je next1 mov ah,0 sub al,30h push ax mov ax,10d mul bx pop bx add bx,ax jmp start1 next1: push bx lea dx,msg2 mov ah,09h int 21h mov bx,0 start2: mov ah,01h int 21h cmp al,0dh je choice mov ah,0 sub al,30h push ax mov ax,10d mul bx pop bx add bx,ax jmp start2 choice: lea dx, msgCh mov ah, 09h int 21h mov ah, 01h mov answer, al int 21h cmp al,'f' je dividing cmp al,'a' je adding cmp al,'s' je subtracting cmp al,'d' je multiplying cmp al,'x' mov ah, 4ch int 21h error: lea dx,msgEr mov ah,09h int 21h jmp start dividing: pop ax div bx push ax lea dx,msgDiv mov ah,09h int 21h pop ax mov cx,0 mov dx,0 mov bx,10d jmp break adding: pop ax add ax,bx push ax lea dx,msgSum mov ah,09h int 21h pop ax mov cx,0 mov dx,0 mov bx,10d jmp break multiplying: pop ax mul bx push ax lea dx,msgMul mov ah,09h int 21h pop ax mov cx,0 mov dx,0 mov bx,10d jmp break subtracting: pop ax sub ax,bx push ax lea dx,msgDif mov ah,09h int 21h pop ax mov cx,0 mov dx,0 mov bx,10d break: div bx push dx mov dx,0 inc cx or ax,ax jne break ans: pop dx add dl,30h mov ah,02h int 21h loop ans jmp start end start

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote