考虑以下用值填充dword数组的过程,并接受两个参数: at EBP + 08h是数组的大小,EBP + 0Ch是给定数组的偏移量。(即OFFSET myarray):
MyProc PROC
PUSH EBP
MOV EBP, ESP
SUB ESP, 04h
PUSH EDI
PUSH ESI
PUSH EBX
MOV EBX, [EBP + 08h] ;move the size of the array into EBX
MOV [EBP - 04h], 00h ;EBP - 04h will be the counter (or the index.)
MOV ESI, [EBP + 0Ch] ;move the offset of the array into ESI
MOV EDI, 01h
INC EBX
@@:
MOV [ESI + 04h * [EBP - 04h]], EDI ;How can I actually move EDI into
;the dword found at address ESI + 4 * the value found at address EBP - 4?
INC [EBP - 04h] ;increment the counter and the value to be stored.
INC EDI
CMP [EBP - 04h], EBX
JNE @B
POP EBX
POP ESI
POP EDI
MOV ESP, EBP
POP EBP
RET
MyProc ENDP我试图将EDI迁移到[ESI + 04h * [EBP - 04h]]中的地方就是我要做的事情的一个例子,因为地址EBP - 4处的dword是数组的索引。
是否有任何方法实际将EDI移动到地址ESI + 4 * the dword at address EBP - 4的dword中?还是我看错了?
发布于 2017-07-20 10:48:04
MOV [ESI + 04h * EBP - 04h],EDI;我如何实际移动EDI;地址ESI + 4处找到的dword *地址EBP -4处的值?增加计数器和要存储的值。
[EBP-4]的值将在dword数组中保存一个递增的索引。我看到了解决这个小问题的两种解决方案:
要考虑的错误:
EBX公司
这个inc会给你一个迭代太多!
假设您希望用比元素索引大1的递增值填充数组(a=1、a1=2、a2=3、.)通过预先递增索引并通过从地址中减去4来补偿此操作,您可以编写一个更好的例程:
MyProc PROC
PUSH EBP
MOV EBP, ESP
PUSH ESI
xor eax, eax ;EAX will be the counter (or the index.)
mov esi, [ebp + 12] ;move the offset of the array into ESI
@@:
inc eax ;increment the counter and the value to be stored.
mov [esi + eax * 4 - 4], eax
cmp eax, [ebp + 8] ;Compare index to size of the array
jb @B
POP ESI
MOV ESP, EBP
POP EBP
RET
MyProc ENDP使用较少的寄存器也意味着要保存的寄存器更少!
发布于 2017-07-19 23:51:21
你把这个程序弄得太复杂了。您所需要做的就是:
push ebp
mov ebp, esp
xor eax, eax ; Fill buffer with nulls
mov ecx, [ebp+8] ; Number of dwords to fill
push edi
mov edi, [ebp+12]
rep stosd
pop edi
leave
ret 8 ; Pop arguments passed by caller大多数ABI都会考虑EAX、ECX和EDX的易失性,但如果您需要保存它们,则必须采取一切方法。
发布于 2017-07-19 23:35:23
它需要两项指示:
MOV EAX, [EBP - 04h]
MOV [ESI + 4*EAX], EDI您还可以考虑在函数的序言和结尾部分保存/恢复EAX。在大多数环境中,EAX不需要保存。
https://stackoverflow.com/questions/45202706
复制相似问题