2012-11-28 75 views
1

我有一個基於用戶輸入的具有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 
+0

你的堆棧不平衡。應該在第一次printf後添加esp,8。 – Gunner

+0

@Gunner爲什麼?唯一的參數是'summsg'(eax)。 – qwertz

+0

那麼推eax兩次肯定是錯誤的,但由於它的存在,堆棧是不平衡的。 – Gunner

回答

3

對於printf您需要傳遞值而不是變量的地址。而不是lea eax, sum; push eaxpush dword [sum]

除以4,只需右移2位。