下面的代码有效,但只除以数字中的第一个数字,余数的输出将始终为零。除法实际上不起作用,例如除以 57/12 将返回商 5.
.MODEL SMALL
.STACK 200
.DATA
CRLF DB 0DH, 0AH, '$'
PROMPT1 DB 'Enter the first 2-digit number (10-99): ','$'
PROMPT2 DB 'Enter the second 2-digit number (10-99): ','$'
PROMPT3 DB 'The quotient is: ', '$'
PROMPT4 DB 'The remainder is: ', '$'
QUOTIENT DB ? ; quotient
REMAINDER DB ? ; remainder
NUM1 DB 2 DUP (?) ; array to store first number
NUM2 DB 2 DUP (?) ; array to store second number
.CODE
.STARTUP
MOV AX, @DATA
MOV DS, AX
; Prompt user for first number
LEA DX,PROMPT1 ;DISPLAY PROMPT1
MOV AH,09H
INT 21H
; Read first number
MOV CX, 2 ; read two digits
LEA DI, NUM1 ; point DI to first digit of NUM1
MOV AH,01H
READ1:
INT 21H
SUB AL,30H ; convert the character to number
MOV [DI], AL ; save digit in NUM1 array
INC DI
LOOP READ1
LEA DX,CRLF ;MOVE CURSOR TO NEXT LINE
MOV AH,09H
INT 21H
; Prompt user for second number
LEA DX,PROMPT2 ;DISPLAY PROMPT2
MOV AH,09H
INT 21H
; Read second number
MOV CX, 2 ; read two digits
LEA DI, NUM2 ; point DI to first digit of NUM2
MOV AH,01H
READ2:
INT 21H
SUB AL,30H ; convert the character to number
MOV [DI], AL ; save digit in NUM2 array
INC DI
LOOP READ2
; Calculate quotient and remainder
MOV AH, 0 ; clear AH
MOV AL, [NUM1] ; copy first digit of NUM1 to AL
MOV BL, [NUM2] ; copy first digit of NUM2 to BL
DIV BL ; divide AL by BL
MOV QUOTIENT, AL ; save quotient in QUOTIENT variable
MOV REMAINDER, AH ; save remainder in REMAINDER variable
; Display quotient and remainder
LEA DX, CRLF
MOV AH, 09H
INT 21H
LEA DX, PROMPT3
MOV AH, 09H
INT 21H
MOV DL, QUOTIENT
ADD DL, 30H
MOV AH, 02H
INT 21H
LEA DX, CRLF
MOV AH, 09H
INT 21H
LEA DX, PROMPT4
MOV AH, 09H
INT 21H
MOV DL, REMAINDER
ADD DL, 30H
MOV AH, 02H
INT 21H
.EXIT
END
我已经尝试了我能找到的一切,但它仍然是一样的。这样做的要点是真正让我们将输入的值存储在一个数组中,然后将它相除。