Click to See Complete Forum and Search --> : How to add, multiply...?


zifeer
March 27th, 2006, 05:14 AM
I've written this 32-bit assembly code:
(It's supposed to read 4 numbers from the keyboard and print the sum, product and average)
include irvine32.inc

.data
value1 dd ?
value2 dd ?
value3 dd ?
value4 dd ?
sum dd ?
prod dd ?
avg dd ?
.code
main PROC
call ReadInt
mov value1,eax
call ReadInt
mov value2,eax
call ReadInt
mov value3,eax
call ReadInt
mov value4,eax
mov sum,value1+value2+value3+value4
mov prod,value1*value2*value3*value4)
mov avg,sum/4
mov eax,sum
call WriteInt
mov eax,prod
call WriteInt
mov eax,avg
call WriteInt
exit
main ENDP
END main
After assembling:
Assembling: project1.asm
project1.asm(21) : error A2101: cannot add two relocatable labels
project1.asm(22) : error A2026: constant expected
project1.asm(23) : error A2026: constant expected
I'm confused, my assembly knowledge is very fresh and new; I've written this after looking at some functions in my book. I would appreciate it if somebody can tell me how to fix it.

Thanks.

wildfrog
March 27th, 2006, 06:38 AM
For addition you can use the ADD instruction.
For multiplication use MUL/IMUL instructions.
To get the average you'll need division; look up DIV or IDIV. In your case you're dividing with 4. When dividing by 2, 4, 8, 16 etc you can use the shift arithmetric right operator SAR.

- petter

zifeer
March 27th, 2006, 10:15 AM
Yeah, I've researched a little bit about it and figured out that I have this whole addition/multiplication/division issue all mixed up. I will loop and use these instructions you stated.

Thanks for your help.