Look at the working process of computer from assembly

Source: Internet
Author: User

This week I learned the first lesson of Linux kernel analysis, and the teacher explained a simple C program that taught me about the work of a computer (especially a stack part).

Here's a simple example to analyze:

Here is the C program:

int g (int  x) {  return6int f (int  x) {   return  int main (void) {  return F (52 ;}

Use the following command to disassemble the C program to get the assembler

Gcc–s–o main.s Main.c-m32

Starting with the main function, we can find that the first two steps of each function are to stack the original EBP value, and the EBP is aligned with the ESP to prepare the stack space for a new function.

The number 5 is then stored in the ESP in main, and the EIP value (the next instruction address of call) is pressed into the stack when the function F,call F is called.

Jumping to the F function is also the first to put EBP on the stack and to its EBP esp. The value of the EBP address plus 8 in the memory space (EBP points to 4 that is, the value of 2 in 5) is passed to eax. The above is actually finished by passing the parameter 5 from the main function to

function f, and parameter 5 is also stored in the stack. The function g is then called.

The first 3 steps of G are also completed by the EBP stack, alignment, parameters passed to EAX and other operations. Then execute the G function to x+6. The left part of the stack above is parsed.

The following pop%ebp pops up the EBP and then RET then makes EBP esp revert to the state before call G, while the EIP points to the 15th line, leave,

The function of leave is to clear the stack space between EBP, ESP, then pop EBP, and RET is the same as above, and after the two steps are completed, ESP EBP returns to the state before the call F.

The program returns to line 23rd in main to continue execution.

From the simple analysis above, we can see. When our computer runs a program, the program and data are stored in memory space, and the computer executes the program according to the value of the IP median. While the memory of the stack space to store a variety of data, through a number of common registers work together, the data in memory space and register to implement data processing and storage. The notion that IP values are stored in the stack at the same time allows the program to execute in a non-sequential order.

The way the stored-program computer runs makes our CPU and memory work together, memory stores data and instructions, and the CPU parses and operates. This combines programs and operations organically, allowing modern computers to operate efficiently.

Chen Hao + original works reproduced please specify the source + "Linux kernel analysis" MOOC course http://mooc.study.163.com/course/USTC-1000029000

Look at the working process of computer from assembly

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.