Valhalla Legends Archive

Programming => General Programming => Assembly Language (any cpu) => Topic started by: Skywing on May 17, 2004, 10:28 PM

Title: Discussions on The C/C++ Reversing Reference Thread
Post by: Skywing on May 17, 2004, 10:28 PM
Quote from: TheMinistered on May 16, 2004, 08:21 PM
In C++, and many other languages, the compiler will generate the following code to access an item in an array:


mov eax, [arraybase+index*arraytypesize]


arraybase is the pointer to the base of the array, index is the item in the array you are trying to retreive, and arraytypesize is the size of the type the array is declared as.

Note that it would probably be a lea there, not a mov.  It's also not uncommon to see several leas if you are dealing with an array of structures.
Title: Re:The C/C++ Reversing Reference Thread
Post by: Maddox on May 18, 2004, 04:57 PM
Quote from: Skywing on May 17, 2004, 10:28 PM
Quote from: TheMinistered on May 16, 2004, 08:21 PM
In C++, and many other languages, the compiler will generate the following code to access an item in an array:


mov eax, [arraybase+index*arraytypesize]


arraybase is the pointer to the base of the array, index is the item in the array you are trying to retreive, and arraytypesize is the size of the type the array is declared as.

Note that it would probably be a lea there, not a mov.  It's also not uncommon to see several leas if you are dealing with an array of structures.

Why would it be lea? It would depend on what the code is doing. It's not uncommon to see mov ecx, [array+index*size].
Title: Re:The C/C++ Reversing Reference Thread
Post by: Adron on May 19, 2004, 05:18 AM
Quote from: iago on May 16, 2004, 09:19 PM
For some arithmatic:

This C code:
int edx = 3;
edx = edx * 2 + 5;


will probably look like this:
mov edx, 3
lea edx, [5 + edx*2]


lea's can be used for arithmatic.

Note that that particular C-code probably would be optimized to assign a constant to edx ;)
Title: Re:The C/C++ Reversing Reference Thread
Post by: iago on May 19, 2004, 07:09 AM
Quote from: Adron on May 19, 2004, 05:18 AM
Quote from: iago on May 16, 2004, 09:19 PM
For some arithmatic:

This C code:
int edx = 3;
edx = edx * 2 + 5;


will probably look like this:
mov edx, 3
lea edx, [5 + edx*2]


lea's can be used for arithmatic.

Note that that particular C-code probably would be optimized to assign a constant to edx ;)

Yes, but that would defeat the example :P