[英]Assembly language Array traversal
我是匯編語言的新手,我遇到了一些基本的編程問題,我想知道你們是否可以指出我正確的方向。 我正在嘗試編寫一個遍歷數組的函數,並總結其元素的值。 給定一個指針int *數組和一些長度x。
到目前為止我能夠做的是寫入初始數據並放置初始指針,這不是很多,而是它的開始。 如何在程序集中使用循環遍歷數組?
PUSH EBX
PUSH ECX
PUSH EDX
PUSH ESI
PUSH EDI
MOV EBX, array
MOV ECX, x
mov eax, 2;
mov ebx, array;
lea edx, [ebx+eax*4];
您不需要保存所有這些寄存器。 如果你的函數使用esi
, edi
, ebx
, ebp
,那么你必須將它們保存在序言中並在結尾中恢復它們。 MASM可以uses
關鍵字uses
為您完成此操作
SomeProcedure proc uses esi, edi, ebx Val1:DWORD, Val2:DWORD
ret
SomeProcedure endp
這是你可以做到的一種方法:
.686
.model flat, stdcall
option casemap :none
include kernel32.inc
includelib kernel32.lib
.data
Array dd 1, 2, 3, 4, 5, 6, 7, 8, 9
Array_Size equ ($ - Array) / 4
.code
start:
push Array_Size - 1
push offset Array
call SumArray
; eax contains sum of array
; print it out here.
push 0
call ExitProcess
SumArray:
push esi ; need to preserve esi
mov esi, [esp + 8] ; address of array
mov ecx, [esp + 12] ; size of array - 1
xor eax, eax ; holds sum
xor edx, edx ; index
AddIt:
add eax, [esi + edx * 4]
inc edx
dec ecx
jns AddIt ; is ecx ! neg repeat loop
pop esi
ret
end start
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.