0

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

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Suwaaaa
  • 11
  • 1
  • 1
    [Why should EDX be 0 before using the DIV instruction?](https://stackoverflow.com/q/38416593) for division. If you have other bugs, those are separate issues that should have their own [mcve]. – Peter Cordes Jun 05 '23 at 08:30

1 Answers1

0

I ran the code the addition and subtraction part work

The subtraction seems fine, but the addition, while calculating the sum correctly - the greatest sum you can expect is 18 (from doing 9 + 9) forgets to output the high-order digit.

The 1st input prompt is skipped and directly executes the 2nd input prompt.

For multiplication and for division, you do print the 1st prompt but you forgot to include the instructions to actually input the value for the NUM1 variable. Because the uninitialized NUM1 variable is most probably 0, the MUL NUM1 instruction will always result in zero and the DIV NUM1 instruction will always produce #DE (Division-by-zero exception)

Next code really should be a subroutine in your program:

LEA DX,MSG6 ; Load the address into DX
MOV AH,9 ; Set INT 21H to 9
INT 21H ; Display in DX

MOV AH,1            ; You
INT 21H             ;    forgot
SUB AL,48           ;          these
MOV NUM1,AL         ;               instructions

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

The division is wrong in several more ways. You are using the byte-sized division that has its dividend in AX, fine, but then you divide by the same value that is in the dividend. This is always going to produce a quotient of 1. The correct instruction is DIV NUM2.
Because of the single-digit inputs, the greatest quotient you can expect is 9 (from doing 9 / 1), so you need to output just the one value instead of the current two values. The only conversion that is further needed is adding 48 to produce ASCII. That AAD ; Adjust result *after* division instruction serves no purpose and if anything, the AAD mnemonic stands for "ASCII Adjust AX before Division".

A much simpler code

ADDITION:
  call input2       ; -> AX = NUM1
  add  al, NUM2     ; [0,18]
  aam
  jmp  EXIT_P

SUBTRACTION:
  call input2       ; -> AX = NUM1
  sub  al, NUM2     ; [0,9]
  jmp  EXIT_P

MULTIPLICATION:
  call input2       ; -> AX = NUM1
  mul  NUM2         ; [0,81]
  aam
  jmp  EXIT_P

DIVISION:
  call input2       ; -> AX = NUM1
  div  NUM2         ; [0,9] Should guard against NUM2 == 0
  cbw

EXIT_P:
  add  ax, '00'     ; Convert to ASCII
  mov  bx, ax
  mov  dx, OFFSET MSG8
  mov  ah, 09h
  int  21h
  mov  dl, bh       ; High-order digit
  mov  ah, 02h
  int  21h
  mov  dl, bl       ; Low-order digit
  mov  ah, 02h
  int  21h
  mov  dx, OFFSET MSG
  mov  ah, 09h
  int  21h
EXIT:
  mov  ax,4C00h
  int  21h
; -----------------------
; IN () OUT (ax) MOD (dx)
input2:
  mov  dx, OFFSET MSG6
  mov  ah, 09h
  int  21h
  mov  ah, 01h
  int  21h
  sub  al, '0'
  mov  NUM1, al

  mov  dx, OFFSET MSG7
  mov  ah, 09h
  int  21h
  mov  ah, 01h
  int  21h
  sub  al, '0'
  mov  NUM2, al

  mov  al, NUM1
  cbw
  ret
; -----------------------
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 1
    Thankyou for pointing out what I forgot. I was very tired doing a lot of things in college, I had a lot on my plate when I was doing this. That instruction fixed it. Thank you again! – Suwaaaa Jun 06 '23 at 01:12