![](/img/trans.png)
[英]Getting segmentation fault for the code below. I am not able to figure out where am I going wrong
[英]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
一样。 几乎可以肯定不是这种情况。 message2
的地址覆盖它。 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.