assembly 需要帮助修复计算器的乘法和除法汇编语言

ryevplcw  于 2023-06-06  发布在  其他
关注(0)|答案(1)|浏览(137)

除法和乘法部分不工作。它跳转到第二个输入数字的提示。
我运行了代码,加法和减法部分工作,而除法和乘法不工作,以及。我厌倦了这种语言。跳过第一输入提示,直接执行第二输入提示。
enter image description heremult partmult result [分割]
下面是完整的代码:

.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
2g32fytz

2g32fytz1#

我运行代码的加减法部分的工作
减法看起来很好,但是加法,在正确计算总和时-您可以期望的最大总和是18(从9 + 9开始)忘记输出高阶数字。
跳过第一输入提示,直接执行第二输入提示。
对于乘法和除法,您确实打印了第一个提示符,但您忘记了包含实际输入 * NUM 1 * 变量值的指令。由于未初始化的 * NUM 1 * 变量最有可能为0,因此MUL NUM1指令将始终导致零,而DIV NUM1指令将始终产生#DE(被零除异常)
接下来的代码应该是程序中的子例程:

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

这种划分在其他几个方面是错误的。你使用的是字节大小的除法,它的被除数在AX中,很好,但是你要除以被除数中的相同值。这总是会产生商1。正确的指令是DIV NUM2
由于输入是一位数,因此您可以期望的最大商是9(从9 / 1开始),因此您需要仅输出一个值,而不是当前的两个值。唯一需要进一步转换的是添加48以产生ASCII。该AAD ; Adjust result *after* division指令没有任何用途,如果有的话,AAD助记符代表“ASCII Adjust AXbeforeDivision”。

简单代码

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
; -----------------------

相关问题