所以我在 Stack Overflow 上找到了很多答案,但我仍然無法讓它作業。我的代碼有什么問題?
mov al, 12
mov bl, 12
mul bl ; answer in ax
aam
mov bx, 10 ; Divisor constant
xor cx, cx ; Clear counter
.a: xor dx, dx ; Clear dx
div bx ; Divide ax with bx, quotient in al, remainder in ah
mov dh, ah ; Move ah to dh, so we can push the 16 bit register of dx
push dx ; Push dx which contains the remainder
inc cx ; Increment counter
test al, al ; If al (the quotient) is not zero, loop again.
jnz .a
.b: pop dx ; Pop the last remainder to dx from stack
add dx, '0' ; Add '0' to make it into character
push cx ; Push loop counter to stack so printing wont interfere with it
mov eax, 4 ;
mov ebx, 1 ;
mov ecx, edx ; Print last popped character
mov edx, 1 ;
int 0x80 ;
pop cx ; Pop the loop counter back to cx
loop .b ; loop for as long as cx is not 0
uj5u.com熱心網友回復:
該AAM指令對于結果最多為 2 位數的情況很有用。在你的情況下 12 x 12 = 144,所以這是不行的。
您使用的轉換回圈幾乎是正確的,但是您正在混合與DIV指令相關的大小。如果您提供一個位元組大小的運算元,DIV則該操作將除以AX該位元組,余數將在AH. 如果您提供一個字大小的運算元,DIV則該操作將除以DX:AX該字,余數將在DX.
因為您正在撰寫 32 位代碼,所以最好也使用 32 位暫存器撰寫轉換。
不要使用數字計數器,而是在堆疊上使用哨兵。ECX顯示時無需保存。
display 函式需要一個指標ECX。只需為此使用堆疊指標并在列印后彈出值。
mov al, 12
mov bl, 12
mul bl ; answer in AX
movzx eax, ax
mov ebx, 10 ; Divisor constant
push ebx ; Sentinel
.a: xor edx, edx
div ebx ; Divide EDX:EAX with EBX, quotient EAX, remainder EDX
add edx, '0' ; Add '0' to make it into character
push edx
test eax, eax ; If EAX (the quotient) is not zero, loop again.
jnz .a
.b: mov eax, 4
mov ebx, 1
mov ecx, esp
mov edx, 1
int 0x80
pop eax
cmp dword [esp], 10 ; Is it the sentinel ?
jne .b ; No, it's a digit
pop eax ; Remove sentinel
雖然不是 32 位代碼,但使用 DOS 顯示數字有更多關于將數字轉換為文本的資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363915.html
