簡體   English   中英

使用裝配中的移位進行乘法。 但是得到一個太大的數字! 我要去哪里錯了?

[英]Multiplying using shifts in Assembly. But getting a way too high number out! Where am I going wrong?

我在使用shift將用戶給定的兩個數字相乘時遇到問題。 它要求用戶輸入兩個整數,然后將它們相乘。 我的程序在要求整數時效果很好,但是當給出乘積時,它是一個天文數字,幾乎沒有正確的數字。 我要去哪里錯了? 它在讀什么寄存器?

%include "asm_io.inc"
segment .data

message1 db "Enter a number: ", 0 message2 db "Enter another number: ", 0 message3 db "The product of these two numbers is: ", 0

segment .bss

input1 resd 1 input2 resd 1

segment .text Global main main: enter 0,0 pusha

mov     eax, message1   ; print out first message
call    print_string
call    read_int    ; input first number
mov     eax, [input1]


mov     eax, message2   ; print out second message
call    print_string
call    read_int    ; input second number
mov ebx, [input2]

cmp     eax, 0      ; compares eax to zero
cmp ebx, 0      ; compares ebx to zero
jnz LOOP        ; 

LOOP:
shl eax, 1

dump_regs 1 mov eax, message3 ; print out product call print_string mov ebx, eax call print_int

除了要求提供數字外,您在其他所有方面都出了問題。

  • 您的行為就像read_int第一次將讀取的整數將其寫入input1 ,第二次將其寫入intput2一樣。 幾乎可以肯定不是這種情況。
  • 即使是這種情況,也可以將第一個數字加載到eax中,然后立即用message2的地址覆蓋它。
  • 即使使用輸入值正確加載了eax和ebx,您應該將二者相乘的代碼實際上仍在執行以下操作:“如果第二個數字非零,則將eax乘以2。否則將其保留單獨。”
  • 即使正確安排了循環,也將eax乘以2乘以ebx 的冪
  • 然后,無論如何,您都用message3的地址覆蓋了此結果,所以這無關緊要。
  • 最后,無法確定從該代碼打印哪個寄存器。 在這個問題和另一個問題之間 ,您似乎期望print_int打印eax,ebx或ecx中的任何一個。

忽略您發布的代碼,並嚴格考慮如何將數字相乘(不使用乘法指令),您將執行以下操作:

mult proc
; multiplies eax by ebx and places result in edx:ecx
    xor ecx, ecx
    xor edx, edx
mul1:
    test ebx, 1
    jz  mul2
    add ecx, eax
    adc edx, 0
mul2:
    shr ebx, 1
    shl eax, 1
    test ebx, ebx
    jnz  mul1
done:
    ret
mult endp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM