An example of array and pointer assembly code analysis in C language _c language

Source: Internet
Author: User

Watching the programmer's interview today, I happened to see the efficient access to arrays and pointers, idle boredom, wrote a paragraph of small code, a simple analysis of C language behind the compilation, may be a lot of people only pay attention to C language, but in practical applications, when there are problems, sometimes through the analysis of assembly code can solve the problem. This article is just for beginners, Daniel can drift through ~

C source code is as follows:

Copy Code code 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;
}

To view the assembly code step under VC6.0:
F9 set breakpoints in the front section of the main function-> compile->f5 right-click->go to disassembly in the debugging interface

Debug assembly Code (commented):

Copy Code code as follows:

4: #include "stdafx.h"
5:
6:int Main (int argc, char* argv[])
7: {
00401010 Push EBP
00401011 mov ebp,esp; save stack frame
00401013 Sub esp,54h; raising the top of the stack
00401016 push EBX
00401017 push ESI
00401018 push EDI; registers used in the press program to restore
00401019 Lea EDI,[EBP-54H]
0040101C mov ecx,15h
00401021 mov eax,0cccccccch
00401026 Rep STOs DWORD [edi], the data between the stack top and the stack frame is populated 0xcc, which is equivalent to int 3 in the assembly, because the variables on the stack are initialized to 0XCC in debug mode to check for uninitialized problems
8:char a=1;
00401028 mov byte ptr [ebp-4],1; ebp-4 is the space address allocated for variable a
9:char c[] = "1234567890";
0040102C mov eax,[string "1234567890" (0042201c)
00401031 mov dword ptr [Ebp-10h],eax; "1234567890" is a string constant stored at address 0042201c, EBP-10 is the first address of the space allocated for array C, the space size from ebp-0x10 to EBP -0X04, a total of 12 bytes. In this sentence, we first copy the 4 bytes of "1234" into the array C.
00401034 mov ecx,dword ptr [string "1234567890" 4 (00422020)]
0040103A mov dword ptr [ebp-0ch],ecx; function ibid., copy "5678" 4 bytes to array c
0040103D mov dx,word ptr [string "1234567890" 8 (00422024)]
00401044 mov word ptr [ebp-8],dx; function ibid., copy "90" 2 bytes to C
00401048 mov al,[string "1234567890" 0Ah (00422026)]
0040104D mov byte ptr [ebp-6],al; this is all ripe, don't forget the
10:char *p = "1234567890";
00401050 mov dword ptr [Ebp-14h],offset string "1234567890" (0042201c); ebp-0x14 is the space address assigned to the pointer p, the size is 4 bytes, and the value in the address is the string "1 234567890 "The first address
11:a = c[1];
00401057 mov cl,byte ptr [ebp-0fh]; Here is the focus, because array c is stored continuously on the stack, it is easy to find the address of one of the characters according to EBP, and take the value, assign to CL
0040105A mov byte ptr [ebp-4],cl; completion assignment
12:a = p[1];
0040105D mov edx,dword ptr [ebp-14h]; here is different from the above, because according to EBP only know the value of the pointer p, first get the value of P, that is, first get a pointer
00401060 mov al,byte ptr [edx 1]; Find a character in the string indirectly based on the resulting pointer
00401063 mov byte ptr [ebp-4],al
13:return 0;
00401066 xor eax,eax eax 0, as the return value of the main function
14:}
00401068 Pop EDI
00401069 pop ESI
0040106A pop ebx
0040106B mov esp,ebp
0040106D pop ebp; Recovery ebp
0040106E ret

Well, as you can see, it takes 2 steps to access an element with an array, and 3 steps when you use the pointer. The visible array is not the same as the pointer, and sometimes the idea that the name of the array can be viewed as a pointer is sometimes true, but sometimes it goes wrong. Let me give you a simple example, and the following example may be a problem that you will often encounter during the development process.

In file Test.cpp:

Copy Code code as follows:

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

There is an extern declaration that indicates that the chtest array is defined in an external file. Chtest is defined in inc.h:

Copy Code code as follows:

Char chtest[10]= "123456789";

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

Copy Code code as follows:

extern char *chtest;

At this time, the program in the compilation of the communication, but the error message is: redefinition; Different types of indirection, but this time there is no error in which line of instructions, if the development of a large project, then it is not easy to locate the problem where. The cause of the above error I think we all understand that, because when chtest as a pointer is referenced, its element access is different from the array, even if the program can compile, at run time, there will be errors.

Well, the above content is personal feeling and hair, is some simple fragmentary things, consultation fee. If there are any places that are not appropriate, look at them!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.