Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

.

model small
.stack 100h

.data
num1 db ?
num2 db ?
op db ?
msg db "error input$"
sum db ?

msg1 db "Enter the first number: $"


msg2 db "Enter the operator (+, -, *, /): $"
msg3 db "Enter the second number: $"
msg4 db "Result: $"
msg5 db "Do you want to restart the program? (Y/N): $"
msg6 db "Program ended. Press any key to exit.$"

.code
main proc
mov ax, @data
mov ds, ax

start:
; Prompt for the first number
lea dx, msg1
mov ah, 9
int 21h

call newline

; Read the first number


mov cl, 10
mov ah, 1
int 21h
sub al, 30h ;for convert to ascii code
mov ah, 00h
mul cl
mov bl, al
mov ah, 1
int 21h
sub al, 30h
add al, bl
mov num1, al
call newline

; Prompt for the operator


lea dx, msg2
mov ah, 9
int 21h

call newline

call space

; Read the operator


mov ah, 1
int 21h
mov op, al

call newline

; Prompt for the second number


lea dx, msg3
mov ah, 9
int 21h

call newline

call space

; Read the second number


mov cl, 10
mov ah, 1
int 21h
sub al, 30h
mov ah, 00h
mul cl
mov bl, al
mov ah, 1
int 21h
sub al, 30h
add al, bl
mov num2, al

call newline

lea dx, msg4


mov ah, 9
int 21h

; Perform the operation based on the operator


cmp op, '+'
je add1

cmp op, '-'


je sub1

cmp op, '*'


je mul1

cmp op, '/'


je div1

; If the operator is invalid, display an error message


lea dx, msg
mov ah, 9
int 21h
jmp end1

add1:
; Addition operation
mov al, num1
add al, num2
mov sum, al
call print
call newline
jmp restart

sub1:
; Subtraction operation
mov al, num1
sub al, num2
mov sum, al
call print
call newline
jmp restart

mul1:
; Multiplication operation
mov ah, 00h
mov al, num1
mov bl, num2
mul bl
mov sum, al
call print
call newline
jmp restart

div1:
; Division operation
mov ah, 00h
mov al, num1
mov bl, num2
div bl
mov sum, al
call print
call newline
jmp restart

restart:
; Prompt for restarting the program
lea dx, msg5
mov ah, 9
int 21h

call newline

; Read the user's choice


mov ah, 1
int 21h
cmp al, 'y'
je start

; If the user doesn't choose to restart, end the program


lea dx, msg6
mov ah, 9
int 21h

mov ah, 4Ch


int 21h
end1:
mov ah, 4Ch
int 21h

print proc
mov dl, '='
mov ah, 2
int 21h
call space
mov bl, 10
mov ah, 00h
mov al, sum
div bl
mov dx, ax
add dx, 3030h
mov ah, 2
int 21h
mov dl, dh
int 21h
ret

space proc
mov dl, ' '
mov ah, 2
int 21h
ret

newline proc
mov dl, 0Dh
mov ah, 2
int 21h
mov dl, 0Ah
mov ah, 2
int 21h
ret

You might also like