Examples of array and pointer assembly code analysis in C

  • 2020-04-02 03:06:42
  • OfStack

Today when watching "the programmer interview treasure dian" chanced upon speak array and pointer access efficiency, idle bored, he wrote a little code, simply analyse the assembly behind the C language, many people may only pay attention to the C language, but in practical application, when problems arise, or sometimes by analyzing the assembly code can solve the problem. This article is just for beginners, Daniel can drift through ~

C source code is as follows:


#include "stdafx.h"
int main(int argc, char* argv[])
{
       char a=1;
       char c[] = "1234567890";
       char *p = "1234567890";
       a = c[1];
       a = p[1];
       return 0;
}

See assembly code steps under VC6.0:
Set breakpoint - on a random line of F9 near the top of the main function > Compile - > F5 right-click - in the debug interface > Go to disassembly

Debug assembly code (annotated) :


4:    #include "stdafx.h"
5:
6:    int main(int argc, char* argv[])
7:    {
00401010   push        ebp    
00401011   mov         ebp,esp      ; Save the stack frame
00401013   sub         esp,54h        ; Pushing up the stack
00401016   push        ebx
00401017   push        esi
00401018   push        edi                     ; Press in the registers used in the program for recovery
00401019   lea         edi,[ebp-54h]            
0040101C   mov         ecx,15h
00401021   mov         eax,0CCCCCCCCh
00401026   rep stos    dword ptr [edi]    ; The data between the top of the stack and the frame of the stack is filled with 0xcc , equivalent to the assembly int 3 This is because debug The mode Stack All the variables are initialized to 0xcc To check for uninitialized problems
8:        char a=1;
00401028   mov         byte ptr [ebp-4],1      ; ebp-4 Is a variable a The address of the allocated space
9:        char c[] = "1234567890";
0040102C   mov         eax,[string "1234567890" (0042201c)]
00401031   mov         dword ptr [ebp-10h],eax   ; " 1234567890 "Is a string constant stored at an address 0042201c Place, ebp-10 For array C The first address of the allocated space, the size of the space from ebp-0x10 to ebp-0x04 , a total of 12 Bytes. In this sentence, first put" 1234 "This 4 Copy 1 byte into the array C In the
00401034   mov         ecx,dword ptr [string "1234567890" 4 (00422020)]
0040103A   mov         dword ptr [ebp-0Ch],ecx  ; As above, put" 5678 "This 4 Copy 1 byte into the array C In the
0040103D   mov         dx,word ptr [string "1234567890" 8 (00422024)]
00401044   mov         word ptr [ebp-8],dx   ; As above, put" 90 "This 2 Copy 1 byte to C In the
00401048   mov         al,[string "1234567890" 0Ah (00422026)]
0040104D   mov         byte ptr [ebp-6],al    ; Everyone is familiar with this. Don't forget it 0
10:       char *p = "1234567890";
00401050   mov         dword ptr [ebp-14h],offset string "1234567890" (0042201c) ; ebp-0x14 Is a pointer p The address of the allocated space, the size is 4 The value in the address is a string." 1234567890 "
11:       a = c[1];
00401057   mov         cl,byte ptr [ebp-0Fh]  ; This is the important thing because of the array C Continuous storage on the stack, easy to follow ebp Find the address of the first character and assign the value to cl
0040105A   mov         byte ptr [ebp-4],cl     ; To complete the assignment
12:       a = p[1];
0040105D   mov         edx,dword ptr [ebp-14h]  ; There is a difference between here and above, because of the basis ebp Just know the pointer p The value of p , that is, get a pointer first
00401060   mov         al,byte ptr [edx 1]    ; Indirectly finds a character in the string based on the resulting pointer
00401063   mov         byte ptr [ebp-4],al
13:       return 0;
00401066   xor         eax,eax         ; eax qing 0 As a main The return value of the function
14:   }
00401068   pop         edi
00401069   pop         esi
0040106A   pop         ebx
0040106B   mov         esp,ebp
0040106D   pop         ebp     ; restore ebp
0040106E   ret

Okay, so you can see that accessing an element with an array takes 2 steps, compared to 3 steps with a pointer. You can see that an array is not the same as a pointer, and sometimes people think that the name of an array is a pointer, which is sometimes true, but sometimes it's wrong. Let me give you another simple example, and the following example may be a common problem in the development process.

In the file test.cpp:


#include "stdafx.h"
#include "inc.h"
extern char chTest[10];
int main(int argc, char* argv[])
{
       printf("chTest=%sn", chTest);
       return 0;
}

There is an extern declaration above to indicate that the chTest array is defined in an external file. ChTest is defined in inc. H:


char chTest[10]="123456789";

The above program, after compiling, can run successfully. But if you change the red code to the following:


extern char *chTest;

At this point, the program will fail at compile time, and the error message is: redefinition; Different types of indirection, but at this point there is no error line, if you are developing a large project, it is not easy to identify where the problem is. The reason for the above error, which I think you all understand, is that when chTest is referenced as a pointer, its elements are accessed in a different way than an array, and even if the program can compile and pass, there will be an error at run time.

Well, the above content is a personal feeling, is some simple bits and pieces, laugh. If what place says is inappropriate, and hope to point out!


Related articles: