Pages

Tuesday, July 19, 2011

Some Pointy Pointers

#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

     int i = 5;
     int *ptr = &i;
     int **ptr2 = &ptr;
     int ***ptr3 = &ptr2;


     cout<<"&i: "<<&i<<endl
            <<"&ptr: "<<&ptr<<endl
            <<"&ptr2: "<<&ptr2<<endl
            <<"&ptr3: "<<&ptr3<<endl<<endl;


     cout<<"*ptr3: "<<*ptr3<<endl
            <<"**ptr3: "<<**ptr3<<endl
            <<"***ptr3: "<<***ptr3<<endl;
     printf("\n%d\n",***ptr3);
     return 0;


}


=====================Disassembled===========================


int i = 5;
003D35BE  mov         dword ptr [i],5
;at this point, we are putting the value of 5 into the address of the variable i


int *ptr = &i;
003D35C5  lea            eax,[i] 
003D35C8  mov         dword ptr [ptr],eax
;now we get the address of i, and store it at address ptr


int **ptr2 = &ptr;
003D35CB  lea            eax,[ptr]
003D35CE  mov         dword ptr [ptr2],eax
;now we get the address of ptr and store it at address ptr2


int ***ptr3 = &ptr2
003D35D1  lea            eax,[ptr2]
003D35D4  mov         dword ptr [ptr3],eax
;getting address of ptr2 and storing it at address ptr3


... ;skipping the cout part.



printf("%d\n",***ptr3);
01081739  mov         eax,dword ptr [ptr3] 
;moving the ptr2's address out of address at ptr3 and store it in eax
;eax is now equal to the address of ptr2


0108173C  mov         ecx,dword ptr [eax] 
;moving the value in the address of ptr2 and storing it in ecx
;ecx is now the address of ptr, read above if you don't remember


0108173E  mov         edx,dword ptr [ecx] 
;moving the address of i into edx, from address of ptr
;edx is now equal to the address of i, omg finally


01081740  mov         esi,esp 
;doing something


01081742  mov         eax,dword ptr [edx] 
;we are now getting the value stored in the address of edx
;which is the value of i
;eax = i


01081744  push        eax  
01081745  push        offset string "\n%d\n" (1087800h) 
0108174A  call          dword ptr [__imp__printf (108A404h)] 
;printf-ing the string and stuff...


01081750  add         esp,8 
01081753  cmp        esi,esp 
01081755  call         @ILT+430(__RTC_CheckEsp) (10811B3h) 
;doing some more things

=========================================================


Output:

&i:             0044FD74
&ptr:    0044FD68
&ptr2:  0044FD5C
&ptr3:  0044FD50


*ptr3:  0044FD68
**ptr3:  0044FD74
***ptr3:  5


5


Yep, some very very pointy pointers.....

No comments:

Post a Comment