Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to dereference something inside of a dereference in assembly?

Consider the following procedure that fills a dword array with values, and takes in 2 parameters: at EBP + 08h is the size of the array, and at EBP + 0Ch is the offset of the given array. (i.e. 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

Where I try to move EDI into [ESI + 04h * [EBP - 04h]] is an example of what I am trying to do, since the dword at address EBP - 4 is the index of the array.
Is there any way to actually move EDI into the dword at address ESI + 4 * the dword at address EBP - 4? Or am I looking at this the wrong way?

like image 668
mike bayko Avatar asked Jan 04 '23 17:01

mike bayko


1 Answers

You're making this procedure overly complicated. All you need to do is the following:

 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

Most ABI's consider EAX, ECX & EDX volatile, but if you need to preserve them, by all means.

like image 169
Shift_Left Avatar answered Jan 13 '23 16:01

Shift_Left