32位数组求和函数,esi 保存数组偏移,ecx 保存数组长度,同时用于 loop 计数, eax 返回和。
.386
.model flat, stdcall
.stack 4096
ExitProcess proto, dwExitCode:DWORD
.data
array DWORD 10000h, 20000h, 30000h, 40000h, 50000h
theSum DWORD ?
.code
main PROC
mov esi, offset array ;// esi 作为参数保存数组地址
mov ecx, lengthof array ;// ecx 是数组长度参数
call ArraySum
mov theSum, eax
invoke ExitProcess, 0
main ENDP
ArraySum PROC
push esi ;// 如果不希望函数修改寄存器,则用栈保存,返回时恢复
push ecx
mov eax, 0 ;// sum = 0
L1:
add eax, [esi]
add esi, type dword
loop l1
pop ecx
pop esi
ret
ArraySum ENDP
end main
函数中使用push pop 临时保存寄存器的值,并在ret前恢复。也可以用uses伪指令代替,让汇编器自动添加push pop。

本文介绍了一个32位数组求和的汇编语言函数实现,使用esi寄存器保存数组偏移,ecx寄存器保存数组长度并用于loop计数,eax寄存器返回求和结果。通过push和pop操作保存与恢复寄存器值,确保函数调用前后寄存器状态不变。
222

被折叠的 条评论
为什么被折叠?



