用汇编语言划分数组

时间:2012-11-28 14:06:48

标签: assembly x86

我有一个基于用户输入的4个整数的数组。我想将数组除以4并显示平均值。

我已经完成了存储4个整数的部分,但是我得到了一些疯狂的答案(在我进行除法之前试图获得总和)所以我甚至没有碰到数组的划分。我知道阵列是正确的,它的分区我错了。

我可以使用任何模板来分割我的数组吗?

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

1 个答案:

答案 0 :(得分:3)

对于printf,您需要传递值而不是变量的地址。而不是lea eax, sum; push eaxpush dword [sum]

要除以4,只需向右移2位。

相关问题