Division and Multiplication part doesn't work. It jumps in to the prompt of the 2nd input number.
I ran the code the addition and subtraction part work, whereas the division and multiplication doesn't work that well. The 1st input prompt is skipped and directly executes the 2nd input prompt.
enter image description heremult partmult result [division]
Here is the full code:
.MODEL SMALL ; Declare the memory model as SMALL
.STACK 100H ; Set the stack size to 100H
.DATA ; Declare data segment
; Messages displayed to the user
MSG1 DB 'Enter 1 for Addition $'
MSG2 DB 10,13,'Enter 2 for Subtraction $'
MSG3 DB 10,13,'Enter 3 for Multiplication $'
MSG4 DB 10,13,'Enter 4 for Division $'
MSG5 DB 10,13,'Choose Any One: $'
MSG6 DB 10,13,10,13,'Enter 1st Number : $'
MSG7 DB 10,13,'Enter 2nd Number : $'
MSG8 DB 10,13,10,13,'The Result is : $'
MSG DB 10,13,10,13,' ------- THANK YOU FOR USING THE CALCULATOR -------$'
NUM1 DB ? ; Variable to store the first number entered
NUM2 DB ? ; Variable to store the second number entered
RESULT DB ? ; Variable to store the result
.CODE ; Start of the code
MOV AX,@DATA ; Move the data segment to AX
MOV DS,AX ; Set DS to the value in AX
LEA DX,MSG1 ; Load the MSG1 into DX
MOV AH,9 ; Set the INT 21H to 9
INT 21H ; Display message in DX
; Display messages
LEA DX,MSG2
MOV AH,9
INT 21H
LEA DX,MSG3
MOV AH,9
INT 21H
LEA DX,MSG4
MOV AH,9
INT 21H
LEA DX,MSG5
MOV AH,9
INT 21H
MOV AH,1 ; Read a single character
INT 21H ; Store input character in AL
MOV BH,AL ; Copy input character to BH
SUB BH,48 ; Convert the ASCII digit to its corresponding numeric value
CMP BH,1 ; Compare the input with 1
JE ADDITION ; If equal, jump to ADDITION
CMP BH,2 ; Compare the input with 2
JE SUBTRACTION ; If equal, jump to SUBTRACTION
CMP BH,3 ; Compare the input with 3
JE MULTIPLICATION ; If equal, jump to MULTIPLICATION
CMP BH,4 ; Compare the input with 4
JE DIVISION ; If equal, jump to DIVISION
ADDITION:
LEA DX,MSG6 ; Load address of MSG6 into DX
MOV AH,9 ; Set function number of INT 21H to 9
INT 21H ; Display the message in DX
MOV AH,1 ; Read a single character
INT 21H ; Store input character in AL
MOV BL,AL ; Copy input character to BL
LEA DX,MSG7 ; Load address of MSG7 into DX
MOV AH,9 ; Set INT 21H to 9
INT 21H ; Display message in DX
MOV AH,1 ; Read a single character
INT 21H ; Store input in AL
MOV CL,AL ; Copy input to CL
ADD AL,BL ; Add AL and BL
MOV AH,0 ; Clear AH
AAA ; Conversion
MOV BX,AX ; Move result to BX
ADD BH,48 ; Convert high-order digit to ASCII
ADD BL,48 ; Convert low-order digit to ASCII
LEA DX,MSG8 ; Load the address of MSG8 to DX
MOV AH,9 ; Set function number of INT 21H to 9
INT 21H ; Display the message in DX
MOV AH,2 ; Set the function number of INT 21H to 2
MOV DL,BL ; Move the low-order digit to DL
INT 21H ; Display character in DL
JMP EXIT_P ; Jump exit point
SUBTRACTION:
LEA DX,MSG6 ; Load address of MSG6 into DX
MOV AH,9 ; Set function number of INT 21H to 9
INT 21H ; Display the message in DX
MOV AH,1 ; Read a single character from the keyboard
INT 21H ; Store the input character in AL
MOV BL,AL ; Copy the input character to BL
LEA DX,MSG7 ; Load the address of MSG7 into DX
MOV AH,9 ; Set the function number of INT 21H to 9
INT 21H ; Display the message in DX
MOV AH,1 ; Read a single character
INT 21H ; Store the input character in AL
MOV CL,AL ; Copy input character to CL
SUB BL,CL ; Subtract the value in CL from BL
ADD BL,48 ; Convert the result to ASCII
LEA DX,MSG8 ; Load the address of MSG8 into DX
MOV AH,9 ; Set the function number of INT 21H to 9
INT 21H ; Display the message
MOV AH,2 ; Set the function number to 2
MOV DL,BL ; Move result to DL
INT 21H ; Display the character in DL
JMP EXIT_P ; Jump to exit
MULTIPLICATION:
LEA DX,MSG6 ; Load the address into DX
MOV AH,9 ; Set INT 21H to 9
INT 21H ; Display in DX
LEA DX,MSG7 ; Load MSG7 into DX
MOV AH,9 ; Set INT 21H to 9
INT 21H ; Display message in DX
MOV AH,1 ; Read a single character
INT 21H ; Store input character in AL
SUB AL,48 ; Convert ASCII digit to numeric value
MOV NUM2,AL ; Store second number in NUM2
MUL NUM1 ; Multiply NUM1 and AX
MOV RESULT,AL ; Store result in RESULT
AAM ; Conversion
ADD AH,48 ; Convert the high-order digit to ASCII
ADD AL,48 ; Convert the low-order digit to ASCII
MOV BX,AX ; Moveresult to BX
LEA DX,MSG8 ; Load address of MSG8 into DX
MOV AH,9 ; Set the function number to 9
INT 21H ; Display the message in DX
MOV AH,2 ; Set function number of INT 21H to 2
MOV DL,BH ; Move high-order digit to DL
INT 21H ; Display character in DL
MOV AH,2 ; Set function number of INT 21H to 2
MOV DL,BL ; Move low-order digit to DL
INT 21H ; Display character in DL
JMP EXIT_P ; Jump to exit point
DIVISION:
LEA DX,MSG6 ; Load address of MSG6 into DX
MOV AH,9 ; Set function number of INT 21H to 9
INT 21H ; Display message in DX
LEA DX,MSG7 ; Load address of MSG7 into DX
MOV AH,9 ; Set function number of INT 21H to 9 (display string)
INT 21H ; Display message in DX
MOV AH,1 ; Read a single character
INT 21H ; Store the input character in AL
SUB AL,48 ; Convert the ASCII digit to numeric value
MOV NUM2,AL ; Store second number in NUM2
MOV CL,NUM1 ; Move first number to CL
MOV CH,00 ; Clear high-order bits of CH
MOV AX,CX ; Move numbers to AX
DIV NUM1 ; Divide AX by NUM1
MOV RESULT,AL ; Store quotient in RESULT
MOV AH,00 ; Clear high-order bits of AH
AAD ; Adjust result after division
ADD AH,48 ; Convert high-order digit to ASCII
ADD AL,48 ; Convert low-order digit to ASCII
MOV BX,AX ; Move result to BX
LEA DX,MSG8 ; Load address of MSG8 into DX
MOV AH,9 ; Set the function number of INT 21H to 9
INT 21H ; Display message in DX
MOV AH,2 ; Set the function number of INT 21H to 2
MOV DL,BH ; Move the high-order digit to DL
INT 21H ; Display the character
MOV AH,2 ; Set the function number of INT 21H to 2
MOV DL,BL ; Move the low-order digit to DL
INT 21H ; Display the character
JMP EXIT_P ; Jump to exit point
EXIT_P:
LEA DX,MSG ; Load the address of MSG into DX
MOV AH,9 ; Set function number of INT 21H to 9
INT 21H ; Display message in DX
EXIT:
MOV AH,4CH ; Set the function number of INT 21H to 4CH
INT 21H ; Terminate the program
ENDS ; End of the segment
END ; End of the program