如何解決這個問題:我不知道為什么商和余數都是錯誤的。
我的 DOSBox 代碼:
.model small
.stack 100h
.data
Dividend db 0dh,0ah,"Enter Dividend : $" ;string
Divisor db 0dh,0ah,"Enter Divisor : $"
Quotient db 0dh,0ah,"Display Quotient : $"
Remainder db 0dh,0ah,"Display Remainder : $"
.code
main proc ;main program here
mov ax,@data ;initialize ds
mov ds,ax
mov ah,09h ;Show Enter Dividend
lea dx, Dividend
int 21h
mov ah,01h ;Input Dividend
int 21h
mov bh,al
mov ah,09h ;Show Enter Divisor
lea dx, Divisor
int 21h
mov ah,01h ;Input Divisor
int 21h
mov bl,al
mov ah,00h ;Divide
mov al,bh
div bl
mov cx,ax
add cx,3030h
mov ah,09h ;Show Display Quotient
lea dx, Quotient
int 21h
mov ah,02 ;Display Quotient
mov dl,cl
int 21h
mov ah,09h ;Show Display Remainder
lea dx, Remainder
int 21h
mov ah,02 ;Display Remainder
mov dl,ch
int 21h
mov ah,4Ch ;end here
int 21h
main endp
end main
錯誤結果:
Enter Dividend : 7 Enter Divisor : 3 Display Quotient : 1 Display Remainder : 4
uj5u.com熱心網友回復:
您向我們展示的奇怪結果源于程式執行除法 55 / 51 確實會產生商 1 和余數 4。
用于輸入的 DOS.GetCharacter 函式 01h 回傳 ASCII 碼AL。當您按下7鍵盤時,AL暫存器將保存十進制的 ASCII 碼 37h 或 55。在計算中使用此輸入之前,您需要將字符轉換為數字表示的值。一個簡單的sub al, 30h就可以了。當然,您也需要對第二個輸入做同樣的事情:
...
mov ah, 09h ;Show Enter Dividend
lea dx, Dividend
int 21h
mov ah, 01h ;Input Dividend
int 21h
sub al, 30h
mov bh, al
mov ah, 09h ;Show Enter Divisor
lea dx, Divisor
int 21h
mov ah, 01h ;Input Divisor
int 21h
sub al, 30h
mov bl, al
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/474980.html
