First understanding of computer work process, first understanding of computer process

Source: Internet
Author: User

First understanding of computer work process, first understanding of computer process

This week, I learned "Linux Kernel Analysis" in the Netease cloud class-how computers work. This week, I learned how to store computer working models, build a foundation for x86, and decompile the source code of a simple C language. Here we will disassemble a simple C program and analyze the assembly code to understand how the computer works.

 

First paste an image of the assembly code after a simple C language program disassembly:

This assembly code is the assembly code that is decompiled to remove the lines starting.

In the code, % ebp is the bottom pointer of the stack, % esp is the top pointer of the stack, and % eax stores the returned values of the current function by default.

Pushl is a stack operation, popl is a stack operation, movl is a value assignment operation, add is a plus operation, sub is a minus operation, call is a function operation, leave is a stack operation, ret indicates the return operation.

 

Next, we will analyze the entire program execution process:

Because the program is executed from the main function, the eip first refers to the address of the main function of line 1, and presses the eip value to a stack, and then runs the main. c program.

The main function first pushes the address pointed to by % ebp into the stack calling the main function (pushl % ebp ). Then, the value of the top stack is assigned to the bottom stack (movl % esp, % ebp), which means a new empty stack is formed (used to execute operations in the main function ). Then, the stack top is reduced by 4 bytes (32-bit) (subl $4, % esp), and a new stack top storage space are formed. Then, assign the value 4 to the top of the stack (molv $4, (% esp )). Then call the function (call f ). The call operation pushes the next line of the current row to the stack and points the eip to the address of function f. After the call is complete, perform the add operation (addl % 7, % eax ). Next, perform the leave operation. The leave operation assigns the stack bottom pointer to the stack top (clears the stack that executes the main operation) and assigns the stack bottom address of the previous stack stored on the stack top to % ebp and pops up the value, this means that the stack of the currently executed function main has been destroyed and is returned to the stack that calls the main function. Finally, a return operation (ret) assigns the eip value stored in the stack to the current eip, pointing to the next line that calls the main function. The main function is executed.

The subsequent f and g functions also perform similar operations.

Because the g function does not operate on the top of the stack, the top of the stack and the bottom of the stack point to the same address, so the leave operation is not required to assign the address of the bottom of the stack to the top of the stack and then remove the bottom of the stack, just pop up the bottom of the stack.

During the execution of each function, the stack used to execute the function is created first, then the operation is executed, and the stack is destroyed and then returned. Not all operations are performed on the stack of the function.

 

Summary:

Today's computers are basically using the Von noiman architecture. The CPU and memory are connected through the bus, and the CPU gets commands from the corresponding address area in the memory and runs them. The most frequently used structure for executing commands on a computer is the stack structure. The execution result is pushed into the stack for future command retrieval and return.

For a single-task computer, the computer executes all commands sequentially. Each program can be executed only after the previous program is executed. Multi-task computers run multiple programs at the same time, and the same core jumps between different programs at a very fast speed. Only a few commands are executed at a time, in this way, it looks like running at the same time.

According to the current knowledge, the computer has three operations: addl, subl, and movl. Push, pop, call, leave, ret, and other operations are combined with the basic three operations. The computer executes all the operations through the Basic Three commands, which is efficient and convenient.

 

Note: I wrote this blog for the first time, but it is not very well written. If you have any shortcomings, please point out that I am humbly asking for advice. Thank you!

 

Author: Li ruosen

Source of original work reprinted

Course: Linux Kernel Analysis MOOC course http://mooc.study.163.com/course/USTC-1000029000

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.