C function calls generate assembly instructions and data in memory condition (2)

Source: Internet
Author: User

C function calls generate assembly instructions and data in memory condition (1)

The specific assembly instructions for the function call and the various variables in the memory of the specific allocation, smattering. All kinds of information are very detailed, but do not practice, do not personally check the memory of the total can not be assured. Then do it by yourself.

Two purposes:

First, functions and functions call the compiled assembly instructions Basic appearance

Second, the memory status of various variable types.

Second, the memory status of various variable types.

1) The location of common variables in memory

2) Custom Structure

1), the common variable in the memory location.

Conclusion: The Global variables: The program loads, and the code, already in memory, into the static zone.

Uninitialized, memory data is replaced with 00 or default.

Address variable (pointer type) is placed in the address straight.

Uninitialized into 0x00000000.

Local variables: Basic types, such as int and char, are not placed anywhere when the program loads.

Only by code can you know that a variable is defined.

When you run the code, push 1 is put into the stack, which is obtained through ebp+x.

While Int[5] and char[5] classes swab fixed-size data, it is generally put into the static area, the compiler in the compilation phase has used the variable is the place of the variable offset address. If the function is not used, directly as a parameter to other functions, it will also push directly. Do not put in the static area, the following example P_char2[5].

A variable that is not a fixed size is put into a static area, such as a pointer. Wait, what if you change the size halfway? Wait for the test. The test discovery will have 2 temporary variable names.

char * p_char3= "hi.";

int p_int2[5]={1,2,3,4,5};

P_char3= "Hihi.";

LC2:

DB "Hihi.", 0x00

LC0:

DB "Hi.", 0x00

Code

int g_int1=3;//static zone. Loading the program into memory

int g_int2;//static zone. The loader has been put into memory (char * p_char3= "hi.") ). With a 4-byte zero placeholder.

int Harimain (void)

{

int p_int=1;//code is not executed, there is no place, after execution, push 1, put into the stack.

Char p_char= ' a ';//code is not executed, there is no place, after execution, push 1, put into the stack.

Char p_char2[5]={' A ', ' B ', ' C ', ' d ', ' E '};//

Code is not executed, there is no place, after execution, with the MOV instruction into the stack.

mov BYTE [ -56+ebp],97

Char

* p_char3= "hi."; /static zone. Loading the program into memory

int p_int2[5]={1,2,3,4,5};//static zone. Loading the program into memory

unsigned int sum;

Sum=count (P_int,p_char,p_char2,p_char3,p_int2);

Sum+=g_int1;

Sum+=g_int2;

}

unsigned int count (int a,char c1,char C2[5],char * c3,int i2[5])

{

unsigned int c;

c=0;

C+=a;

C=C+C1;

C+=C2[0];

C+=C2[3];

C+=I2[0];

C+=I2[4];

C+=C3[0];

C+=C3[1];

return C;

}

The location of the data in memory

When the program loads,

Code Area 0X0028001B

//

//^

Stack top (empty stack) 0x00310000

Static Zone 0x00310000

When the program runs

Code Area 0X0028001B

/

Temporary variable (top of stack) of the person being tuned

Local variables of the person being tuned

Call before the EBP register straight (while the current EBP register stores the address of this location)

return address

Parameters

Bottom stack (empty stack) 0x00310000

Static Zone 0x00310000

Two, custom structure

Conclusion: A custom structure can be thought of as an array.

Custom structure, as a parameter, will put all member variables, one by one into the stack

If you pass a custom structure pointer, only the address is passed.

Global custom struct-body variables, and global fixed-length array classes swab.

The program loads, like code, already in memory, into the static zone.

Uninitialized put 00 data,

The name of the variable appears in the code and is replaced by the address. [_struce_a]

Straighten:

MOV BYTE [_mystruck_a+4],97

Direct address + digital positioning member

Global custom struct-body address variable (pointer),

The program loads, like code, already in memory, into the static zone.

But the size is not struck size, but 4B, that is, the size of an address variable.

Uninitialized into 0x00000000.

Straighten:

MOV Edx,dword [_mystruck_c]

MOV DWORD [8+edx],3

Must take the address straight to get the real address plus the digital locator member

Local variables,

The program is loaded and does not exist anywhere.

Only run-time, put in the stack. Such as:

struct Mystruck mystruck_d;

Compile to SUB esp,60

Mystruck_d.char_b= ' E '

Compiled to

MOV BYTE [ -36+ebp],101

The stack data after call.

struct mystruck{

int int_a;

Char Char_b;

int int_array[2];

char * CHAR_ARRAY;

};

int Harimain (void)

{

Char c1[2]={' B ', ' C '};

Mystruck_a.int_a=1;

Mystruck_a.char_b= ' a ';//mov BYTE [_mystruck_a+4],97

Mystruck_a.int_array[0]=1;//mov DWORD [_mystruck_a+8],1

Mystruck_a.int_array[1]=2;//mov DWORD [_mystruck_a+12],2

MYSTRUCK_A.CHAR_ARRAY=C1;

MOV Eax,dword [_mystruck_c]

Mystruck_c->int_a=2;//mov DWORD [eax],2

Mystruck_c->char_b= ' C ';//mov BYTE [4+eax],99

MOV Edx,dword [_mystruck_c]

Mystruck_c->int_array[0]=3;//mov DWORD [8+edx],3

Mystruck_c->int_array[1]=4;//mov DWORD [12+edx],4

Mystruck_c->char_array=c1;//lea Eax,dword [ -42+EBP] MOV DWORD [16+edx],eax

MOV Ebp,esp

SUB esp,60

struct Mystruck mystruck_d;

Mystruck_d.char_b= ' e ';//mov BYTE [ -36+ebp],101

Mystruck_d.int_array[0]=5;//mov DWORD [ -32+ebp],5

Mystruck_d.int_array[1]=6;//mov DWORD [ -28+ebp],6

unsigned int c=counta (mystruck_a,mystruck_c,mystruck_d);

Io_hlt ();

return 0;

}

unsigned int counta (struct mystruck mys,struct mystruck * mysc,struct mystruck mys2)

{

unsigned int c;

c=0;

C=mys.int_a;

C=c+mysc->int_a;//add Eax,dword [8+EBP]

C=c+mysc->char_array[0];//mov Edx,dword [28+EBP] MOV Edx,dword [16+edx] ADD Eax,edx//char_array[0 ]

c=c+mys.char_array[0];//

C=c+mys2.int_a;

return C;

}

The assembly instructions and data generated by the

C function call are in memory condition (2)

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.