![](/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.