[英]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. 除了要求提供数字外,您在其他所有方面都出了问题。
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. message2
. message2
的地址覆盖它。 message3
anyway, so none of that matters. message3
的地址覆盖了此结果,所以这无关紧要。 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.