Reverse Engineering for Beginners

(avery) #1

CHAPTER 18. ARRAYS CHAPTER 18. ARRAYS


mov [esp+eax*4+70h+i_2], edx
add [esp+70h+i], 1 ; i++

loc_804840A:
cmp [esp+70h+i], 13h
jle short loc_80483F7
mov [esp+70h+i], 0
jmp short loc_8048441


loc_804841B:
mov eax, [esp+70h+i]
mov edx, [esp+eax*4+70h+i_2]
mov eax, offset aADD ; "a[%d]=%d\n"
mov [esp+70h+var_68], edx
mov edx, [esp+70h+i]
mov [esp+70h+var_6C], edx
mov [esp+70h+var_70], eax
call _printf
add [esp+70h+i], 1


loc_8048441:
cmp [esp+70h+i], 13h
jle short loc_804841B
mov eax, 0
leave
retn
main endp


By the way, variableais of typeint*(the pointer toint)—you can pass a pointer to an array to another function, but it’s more
correct to say that a pointer to the first element of the array is passed (the addresses of rest of the elements are calculated
in an obvious way). If you index this pointer asa[idx],idxis just to be added to the pointer and the element placed there
(to which calculated pointer is pointing) is to be returned.


An interesting example: a string of characters like“string”is an array of characters and it has a type ofconst char[]. An index
can also be applied to this pointer. And that is why it is possible to write things like“string”[i]—this is a correct C/C++
expression!


18.1.2 ARM.


Non-optimizing Keil 6/2013 (ARM mode)


EXPORT _main
_main
STMFD SP!, {R4,LR}
SUB SP, SP, #0x50 ; allocate place for 20 int variables


; first loop


MOV R4, #0 ; i
B loc_4A0
loc_494
MOV R0, R4,LSL#1 ; R0=R42
STR R0, [SP,R4,LSL#2] ; store R0 to SP+R4<<2 (same as SP+R4
4)
ADD R4, R4, #1 ; i=i+1


loc_4A0
CMP R4, #20 ; i<20?
BLT loc_494 ; yes, run loop body again


; second loop


MOV R4, #0 ; i
B loc_4C4
loc_4B0
LDR R2, [SP,R4,LSL#2] ; (second printf argument) R2=(SP+R4<<4) (same as
(SP+R4*4))
MOV R1, R4 ; (first printf argument) R1=i
ADR R0, aADD ; "a[%d]=%d\n"

Free download pdf