使用汇编中的移位进行乘法。但得到的数字太高了!我哪里错了?
我在使用移位来乘以用户给出的两个数字时遇到问题。 它要求用户输入两个整数,并将它们相乘。 我的程序在询问整数时效果很好,但是当它给出产品时,它是一个天文数字,根本不正确。 我哪里错了?它正在读取什么寄存器?
%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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了询问数字之外,你几乎在所有事情上都出错了。
read_int
第一次调用时将读取的整数写入input1
中,第二次调用时写入intput2
中。几乎可以肯定情况并非如此。message2
的地址覆盖它。message3
的地址覆盖这个结果,所以这些都不重要。print_int
打印 eax、ebx 或 ecx 中的任何一个。You are going wrong in pretty much everything besides asking for the numbers.
read_int
writes the read integer intoinput1
the first time it is called and intointput2
the second time. This is almost certainly not the case.message2
.message3
anyway, so none of that matters.print_int
to print any of eax, ebx, or 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: