Reverse Engineering for Beginners

(avery) #1

CHAPTER 18. ARRAYS CHAPTER 18. ARRAYS


GCC also generates equivalent routines, but slightly different:


Listing 18.23: Optimizing GCC 4.9 x64

; RDI=address of array
; RSI=a
; RDX=b


get_by_coordinates1:
; sign-extend input 32-bit int values "a" and "b" to 64-bit ones
movsx rsi, esi
movsx rdx, edx
lea rax, [rdi+rsi4]
; RAX=RDI+RSI
4=address of array+a4
movzx eax, BYTE PTR [rax+rdx]
; AL=load byte at address RAX+RDX=address of array+a
4+b
ret


get_by_coordinates2:
lea eax, [rdx+rsi4]
; RAX=RDX+RSI
4=b+a4
cdqe
movzx eax, BYTE PTR [rdi+rax]
; AL=load byte at address RDI+RAX=address of array+b+a
4
ret


get_by_coordinates3:
sal esi, 2
; ESI=a<<2=a4
; sign-extend input 32-bit int values "a
4" and "b" to 64-bit ones
movsx rdx, edx
movsx rsi, esi
add rdi, rsi
; RDI=RDI+RSI=address of array+a4
movzx eax, BYTE PTR [rdi+rdx]
; AL=load byte at address RDI+RDX=address of array+a
4+b
ret


18.6.3 Three-dimensional array example


It’s thing in multidimensional arrays. Now we are going to work with an array of typeint: each element requires 4 bytes in
memory.


Let’s see:


Listing 18.24: simple example

#include <stdio.h>


int a[10][20][30];


void insert(int x, int y, int z, int value)
{
a[x][y][z]=value;
};


x86


We get (MSVC 2010):


Listing 18.25: MSVC 2010

_DATA SEGMENT
COMM _a:DWORD:01770H
_DATA ENDS
PUBLIC _insert
_TEXT SEGMENT
_x$ = 8 ; size = 4

Free download pdf