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

1. Problem Description

As we all know, the computer is one of the greatest invention of the 20th century, how do computers work? Herein primarily by the structure and working principle of the computer, and the assembly code to the working process described in detail.

2. settlement process

3.1 Von Neumann architecture

Generally, according to modern computer architecture Von Neumann architecture, but also to the structure Princeton structure, a program instruction memory and data memory of a memory structure combined. Mainly constituted by the operator, controller, memory, input devices and output devices five members, wherein the operator, the controller, and the CPU is also referred to merge partial register, the whole system operation in binary logic, i.e. 0/1 coding. Structure diagram is as follows:

the reference link: a computer system

3.2 Computers Work

Based computer running CPU, CPU mainly comprises an arithmetic logic unit ALU, the program counter PC, the instruction register IR, the data register DR, the address register AR the like. CPU internal structure as shown below:

Almost all of the instructions executed by a CPU von Neumann-type computers are five stages: fetch, decode, fetch, execute, and writeback.

Fetch 3.2.1

Instruction fetch stage, the instruction fetch from the main memory to the instruction register IR of the process, while the address value pointing to the instruction currently executing program instruction register PC.

3.2.2 decoded

After fetching an instruction, an instruction decoder according to the format of the instruction, the instruction fetch of the split and parsing operations obtain different methods.

Fetch 3.2.3

The contents of the instruction, the instruction may be accessed again in accordance with the main memory address, read operands for arithmetic operations.

3.2.4 execution

At the end of the fetch and fetch, according to the opcode of the instruction, will be a series of different operations.

3.2.5 write-back

After execution of the instructions, we will write back the data stored in the internal register or an external memory space.
Instruction execution as shown below:

reference links: Principles of Computer Organization

3.3 assembler code simple work process

Assembly language (assembly language) is a low-level computer language, a microprocessor, microcontroller or other programmable device for sign language also known. In assembly language, machine code mnemonic instead of the operation instruction, with an address label or symbol instead of an operand or instruction address. In various devices, the assembly language instruction corresponds to a different set of machine language, into machine instructions through the assembly process.
Reference links: assembly language
Next by compiling a simple C language program and analyze their assembly instructions execution.

3.3.1 C language program

Use mian.c vim editor program code is as follows:

int g(int x)
{
    return x + 5;
}
int f(int x)
{
    return g(x);
}
int main(void)
{
    return f(10) + 2;
}

In the laboratory building environment, compile and execute commands by gcc main.c program, execute the command code is as follows:

#编译C语言代码文件
gcc main.c
#执行a.out可执行文件
./a.out
#输出程序返回值
echo $?

Run the program results as shown below:

3.3.2 assembly file

By gcc c language program command files into a file compilation main.s, vim editor and assembly code and the reading process, as follows:

#将c语言程序编译为main.s汇编文件
gcc –S –o main.s main.c -m32
#用vi方式打开汇编文件
vi main.s
#在命令模式下取除"."开头的辅助信息
g/\.s*/d

Assembly language is shown below:

3.4 Analysis of work stack assembly code

Stack (Stack), also known as the stack, which is a linear form of operation is limited. Defining a linear table insertion and deletion operations only in the trailer. This input is called the stack, relatively, and the other end is called the bottom of the stack. To insert a new element, also known as the stack into the stack, push or push, it is a new element into the top element of the above, making the new top of the stack; remove elements from one stack to stack or also known as unstack, it is the top element removed, so that the adjacent element becomes the new top of the stack, the stack is the most important feature, last out.
Reference links: the stack
during the execution of the assembly language instructions and data to be registered in the stack register is not the main form. 32-bit 8086CPU mainly by the ESP and EBP registers two pointers, and a common data register EAX.
Wherein ESP stack pointer register to hold the uppermost point to the system stack a stack frame of the stack, the base pointer EBP registers for storing the stack point system stack top end of a stack frame. More simply before the function call, the value of the EBP will be passed in the stack, the stack memory address ESP, then ESP will pass the value of EBP; after the function call, the value will return ESP EBP, and ESP points to the top address. In the present embodiment the construction of the stack grows downwards spatial unit number, the address is reduced downward, a memory unit 32 is not four bytes, in the initial state, EBP, and ESP pointers point to the initial reference position (2000) is 0, the following Figure:

Let's analyze the previous section main.s each line of assembly language implementation process, the change of stack space:

3.4.1 main function execution process

Program to execute lines 18-22, and then call the function f.

pushl %ebp

When this code is executed, ESP register will point to a unit number, then the value of the EBP register (address 2000) to the stored position of the stack reference numeral 1.

movl  %esp, %ebp 

When this code is executed, EBP register will point to a unit number.

subl $4, %esp.

movl $10, (%esp)

When this code is executed, the number 10 will be immediately stored in the storage unit 2, the ESP register points.

call f

When this code is executed, ESP register points to the next storage unit, the EIP register line 23 (the next line of the file line numbers run Call f assembler instructions) stored in the storage space 3, while the EIP register points to the first instruction of the function f line number 9.

3.4.2 f function execution process

pushl %ebp
movl %esp, %ebp

When this code is executed, ESP register to point to the next storage unit 4, and then the value stored in the storage unit of the EBP register number 4, then point to the same address as EBP ESP.

subl $4,  %esp
movl 8(%ebp), %eax

When this code is executed, ESP register points to the next first storage unit 5, and then move in the value storage unit 8 is stored in address locations 2 on the EAX register EBP, note here EBP register pointer is not actually changed.

movl %eax, (%esp)

When this code is executed, the value stored in the EAX register the position of the ESP register points to, i.e., the storage unit 5.

call g

When this code is executed, ESP register points to the next storage unit, the EIP register line 15 (the line number of the file operation call g assembler instruction next line) is stored in the storage space 6, while the EIP register points to the first instruction of the function f line No. 2.

3.4.3 g execution function

pushl %ebp
movl %esp, %ebp

When this code is executed, ESP register to point to the next storage unit 7, and then the value stored in EBP register number storing unit 7, then the same address as EBP point ESP.

movl 8(%ebp), %eax
addl $5, %eax

When this code is executed, the shift register points to EBP position 8 immediate location stored in the storage unit 10 in the EAX register, and then immediately added to the number of register EAX 5, to obtain the value of the EAX register 15 is stored.

popl %ebp
ret

When this code is executed, the EBP register contents back to the ESP register points to the storage unit 7, i.e. to restore the function f is a function call stack base address register EBP, ESP register points to the memory unit 6, and then the ESP points 15 of the content storage unit 6 into the EIP register, ESP register 4 is further moved upward to the storage unit addresses 5, EIP line 15 leave the next instruction execution.

3.4.4 f function process returns

leave

When this code is executed, the same EBP ESP register points to the first memory unit 4, and then jumps to the EBP register current contents of memory locations pointed to 1, then the ESP Jiangzai 4 is moved upward to the storage unit 3 address.

ret

Once the code execution, the content 32 ESP register points to write the EIP, the value of the next program jumps addl instruction execution, line 23, while the four address ESP moving upward.

3.4.5 main function returns process

addl $2, %eax

When this code is executed, the number of $ 2 immediately added to the EAX register, and returns the function value.

leave
ret

When this code is executed, the ESP register points to the address register points to EBP, EBP registers jump to the initial address (2000) according to a content storage unit, the base address and the ESP register also added up to four addresses initial address (2000).

4. Summary

This paper studied the von Neumann computer architecture, computer CPU works as well as changes in the compilation process execution language stack, von Neumann architecture as the current mainstream computer architecture, still in use. Early von Neumann architecture is operator-centric, but due to the expansion of the storage capacity and improve operations and controller integration technology, now mainly in the memory as the center. Computer CPU works involved in the operation instructions, as well as related hardware input and output data stream, prior to foundation courses computer composition principle had some learning. Change stack can be read in assembly language, assembly language, on the ground floor can be cleaned more describe information exchange and data dump process between the computer's internal hardware machine language, by this study, I stack structure, and wherein ESP and EBP two pointer registers left a deep impression. Learn more abstract knowledge, can learn and flexible model to interpret the image of the chart, you can improve learning efficiency.

Guess you like

Origin www.cnblogs.com/louhao-20199310/p/11553237.html