使用汇编中的移位进行乘法。但得到的数字太高了!我哪里错了?

发布于 2024-10-24 03:09:43 字数 960 浏览 8 评论 0原文

我在使用移位来乘以用户给出的两个数字时遇到问题。 它要求用户输入两个整数,并将它们相乘。 我的程序在询问整数时效果很好,但是当它给出产品时,它是一个天文数字,根本不正确。 我哪里错了?它正在读取什么寄存器?

%include "asm_io.inc"
segment .data

message1 db "请输入数字:", 0 message2 db "请输入另一个号码:", 0 message3 db "这两个数字的乘积是:", 0

段 .bss

input1 resd 1 input2 resd 1

段.text 全球主要 主要的: 输入 0,0 普沙

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        ; 

循环:
shl eax, 1

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

I am having issues with using shifts to multiply two numbers given by the user.
It asks the user to enter two integers and it is supposed to multiply them.
My program works well in asking for the integers, but when it gives the product it is an astronomical number no where near being correct.
Where am I going wrong? what register is it reading?

%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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-10-31 03:09:43

除了询问数字之外,你几乎在所有事情上都出错了。

  • 您的行为就像 read_int 第一次调用时将读取的整数写入 input1 中,第二次调用时写入 intput2 中。几乎可以肯定情况并非如此。
  • 即使是这种情况,您也将第一个数字加载到 eax 中,然后立即用 message2 的地址覆盖它。
  • 即使 eax 和 ebx 正确加载了输入值,您应该将两者相乘的代码实际上正在执行“如果第二个数字非零,则将 eax 乘以 2。否则保留它”独自的。”
  • 即使循环安排正确,它也会将 eax 乘以 2 的 ebx 次方。
  • 然后你无论如何都会用 message3 的地址覆盖这个结果,所以这些都不重要。
  • 最后,无法确定从这段代码中打印出哪个寄存器。在这个问题和你的其他问题之间,你似乎是期望 print_int 打印 eax、ebx 或 ecx 中的任何一个。

You are going wrong in pretty much everything besides asking for the numbers.

  • You are acting like read_int writes the read integer into input1 the first time it is called and into intput2 the second time. This is almost certainly not the case.
  • Even were that the case, you load the first number into eax and then immediately overwrite it with the address of message2.
  • Even if eax and ebx were loaded correctly with the input values, your code that is supposed to be multiplying the two is actually be doing something along the lines of "if the second number is non-zero, multiply eax by 2. Otherwise leave it alone."
  • Even were the loop arranged correctly, it would be multiplying eax by 2 to the power of ebx.
  • Then you overwrite this result with the address of message3 anyway, so none of that matters.
  • In the end, it is impossible to determine what register is getting printed from this code. Between this question and your other question, you seem to be expecting print_int to print any of eax, ebx, or ecx.
失而复得 2024-10-31 03:09:43

忽略您发布的代码,并严格查看如何将数字相乘(不使用乘法指令),您可以执行以下操作:

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

Ignoring the code you've posted, and looking strictly at how to multiply numbers (without using a multiply instruction), you do something like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文