简体   繁体   English

使用装配中的移位进行乘法。 但是得到一个太大的数字! 我要去哪里错了?

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

I am having issues with using shifts to multiply two numbers given by the user. 我在使用shift将用户给定的两个数字相乘时遇到问题。 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

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. 您的行为就像read_int第一次将读取的整数将其写入input1 ,第二次将其写入intput2一样。 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 . 即使是这种情况,也可以将第一个数字加载到eax中,然后立即用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." 即使使用输入值正确加载了eax和ebx,您应该将二者相乘的代码实际上仍在执行以下操作:“如果第二个数字非零,则将eax乘以2。否则将其保留单独。”
  • Even were the loop arranged correctly, it would be multiplying eax by 2 to the power of ebx. 即使正确安排了循环,也将eax乘以2乘以ebx 的幂
  • Then you overwrite this result with the address of message3 anyway, so none of that matters. 然后,无论如何,您都用message3的地址覆盖了此结果,所以这无关紧要。
  • 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. 在这个问题和另一个问题之间 ,您似乎期望print_int打印eax,ebx或ecx中的任何一个。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取以下代码的细分错误。 我无法弄清楚我要去哪里 - Getting segmentation fault for the code below. I am not able to figure out where am I going wrong 自定义服务启动在OLE6中不起作用。 我要去哪里错了? - Custom service start is not working in OLE6. Where am i going wrong? 出现错误:我明确拥有正确数字的参数太少 - Getting Error:Too few arguments where i clearly have the correct number 为什么我无法在NASM程序集中打印我的数字常数? - Why am I unable to print my number constant in NASM Assembly? x86-64 Linux程序集。 由于EFAULT,在argv上使用write无法正常工作? “地址错误” - x86-64 linux assembly. using write on argv doesn't work because of EFAULT? “Bad address” 无法解决cron电子邮件的去向 - cant work out where cron emails are going "<i>gnu ld treating assembly output as linker script, how to fix?<\/i> gnu ld 将程序集输出视为链接器脚本,如何解决?<\/b> <i>or am i doing something wrong in my compiling?<\/i>还是我在编译时做错了什么?<\/b>" - gnu ld treating assembly output as linker script, how to fix? or am i doing something wrong in my compiling? 无法弄清楚为什么我不从一个线程获取作为输入的数组并尝试使用另一个线程打印它 - Not able to figure out why am i not getting the array that i took as input from one thread and trying to print it using another 我收到 &#39;bash: cd: too many arguments&#39;,我不知道为什么? - I am getting 'bash: cd: too many arguments' and I have no idea why? 我使用Gio GFile monitor_file错误吗? - Am I using Gio GFile monitor_file wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM