Michal Szalaty
Michal Szalaty

Reputation: 11

Dividing array in assembly language

I've got an array with 4 integers which base on user input. I want to divide the array by 4 and display the average.

I've done the part where I store 4 integers but I get some mad answer for the sum (tried to get sum before i do division) so i didnt even touch the division of the array. I know the array is correct, its the division bit i get wrong.

Is there any template of the code i can use to divide my array?

lea ebx,myarray // address of the array (its 0th element) is put in ebx
mov ecx,4 // size of the array is saved in the counter
mov eax,0 // eax will be used to hold the sum, initialise to 
push eax

lea eax, summsg
push eax
call printf
add esp,4


lea eax,sum // save location of var to read in the input
push eax
lea eax,formatstring // loads formatstring
push eax // push it onto the stack
call printf // call scanf and prints out the number which we entered
add esp,8

Upvotes: 1

Views: 1455

Answers (1)

Jester
Jester

Reputation: 58802

For printf you need to pass the value not the address of the variable. Instead of lea eax, sum; push eax do push dword [sum].

To divide by 4, just shift right by 2 bits.

Upvotes: 3

Related Questions