Reputation: 11
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
Upvotes: 0
Views: 744
Reputation: 490338
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
Upvotes: 1
Reputation: 94834
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. 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.Upvotes: 2