2019-2020-1 20199326 "Linux kernel principle and Analysis" in the second week of work

Week Summary: This week's study is the first chapter mainly Paodingjieniu Linux, and after reading the book, along with cloud and deepen class lesson learned about the contents of the first chapter. The first chapter describes some of the command linux in the assembly instructions, such as movl, pushl, popl and so on, these instructions are to make a fuss about the data structure of the stack. Throughout this chapter it is esp, ebp, eip pointer register. In simple terms, ebp refers to the bottom of the stack, esp is pointing to the top of the stack. eip is the location where the instruction being executed, and then repeated stack pop operation is performed.

C language program:

int g(int x)
{
    return x + 3;
}

int f(int x)
{
    return g(x);
}

int main(void)
{
    return f(8) + 1;
}

After removing the compilation of unrelated items:

g:
1   pushl   %ebp
2   movl    %esp, %ebp
3   movl    8(%ebp), %eax
4   addl    $3, %eax
5   popl    %ebp
6   ret
f:
7   pushl   %ebp
8   movl    %esp, %ebp
9   subl    $4, %esp
10  movl    8(%ebp), %eax
11  movl    %eax, (%esp)
12  call    g
13  leave
14  ret
main:
15  pushl   %ebp
16  movl    %esp, %ebp
17  subl    $4, %esp
18  movl    $8, (%esp)
19  call    f
20  addl    $1, %eax
21  leave
22  ret

ret instruction is equivalent to

popl %eip

call instruction is equivalent to

pushl %eip
movl f %eip

instruction is equivalent to leave

movl %ebp,%esp
popl %ebp

Resolution:

Program execution begins at 15, where, at this time, and esp ebp point to the end of the stack, numbered 0, line 15, 0 push ebp, esp-4, line 16, ebp-4, points to the same case ebp and esp at line 17, esp minus 4,18 line, the immediate 8 to keep the stack, i.e. the position indicated esp, line 19, perform call command, the eip 20 into the stack, then points to the top esp into the function f, after the line 7-8, esp both EBP and points to the top line 9, esp-4, line 10, the immediate ebp + 8 to the location of stored in eax 8, line 11, esp value 8 into a position indicated in eax, line 12, to eip 13 into the stack, performs the function g, after the first two steps, esp, EBP all points to the top line 3, the ebp + 8 8 referred unknown number stored in eax, and eax + 3 = 11, line 5, ebp pop, esp-4, ebp pointer falls back to the previous position of the pointer EBP, and ret, eip 13 pop , esp-4, line 13 to continue command, and then pop ebp, ebp pointer falls back to the previous pointer position ebp, line 14 and ret, eip 20 the stack, esp-4, line 20 perform command, ebp pop again, eax + 1 = 12, and then leave, after returning ret Value 12

Guess you like

Origin www.cnblogs.com/funmary/p/11562525.html