How the first week of Linux kernel analysis notes computer works

Source: Internet
Author: User

First, how does the computer work?

1. Stored program Computer working model

1) von Neumann architecture

Learn the basic concepts of computer research. Refers to the storage program computer. All of the computing-capable electronic devices are smaller than calculators, and the core parts of supercomputers can be described in this architecture.

2) Stored program computer working model

From the hardware (motherboard of the computer): Logically abstracted as, between the CPU and memory through the bus connection, the CPU has a key register IP (instruction Pointer) (called IP in the 16-bit CPU, in the 32-bit CPU called the EIP, in the 64-bit CPU called RIP), Always point to a chunk of memory, point to the code snippet CS in memory Segment. The CPU takes an instruction from the memory address pointed to by the IP, completes the IP from +1, executes the next instruction, and then repeats the command execution.

From programmer: Memory to save instructions and data, the CPU is responsible for interpreting and executing these instructions.

3) What kind of instruction does the CPU recognize? How is it defined?

API: interface between programmer and computer.

ABI: Program and CPU interface

    • Assembly Instructions
    • What registers are agreed to use
    • For X86, most instructions have direct access to memory

EIP: In the X86 computer an EIP points to the memory of an instruction, the EIP automatically added to the next instruction, each instruction length is different, can also be called, RET, jmp, condition JMP instructions modified.

2, X86 Compilation Foundation

1) X86 CPU Registers

1. General-Purpose Registers:

32-bit registers, low 16 bits as 16-bit registers: AX, BX, CX, DX.

The register that begins with E is 32 bits. accumulator EAX, Base address register EBX, count register ecx, data register edx, stack base pointer ebp (important), variable address register ESI and EDI, stack top pointer esp (important). A stack is a fundamental thing in a computer.

The register starting with R is 64 bits. The mechanism differs little from the 32-bit.

2. Segment Register:

cs--Code Segment Register, whose value is the segment value of the code snippet;
ds--Data Segment Register, whose value is the segment value of the data segment;
es--Additional segment Register (Extra Segment register), whose value is the segment value of the additional data segment;
The ss--stack segment register (stack Segment register), whose value is the segment value of the stack segment;
fs--Additional segment Register (Extra Segment register), whose value is the segment value of the additional data segment;
The gs--additional segment register (Extra Segment register), whose value is the segment value of the additional data segment.

Where code snippets and stack segments use the most. The CPU accurately locates the memory address of an instruction according to CS:EIP when actually fetching the instruction.

3. Flag Register:

Used to identify the current state.

2) Common Assembly instructions

1.mov instruction and several memory addressing methods

B,w,l,q represent 8-bit, 16-bit, 32-bit, and 64-bit respectively

    

    • Register addressing, register identifier with%, Operation Register, independent of memory.
    • Immediate addressing, the immediate number is the value beginning with $, regardless of memory.
    • Direct addressing, which accesses data directly from a specified memory address. No $ is the address, and the memory data pointed to by the memory address 0x123 into the edx register.
    • Indirect addressing, the value of the Register as a memory address to access memory, register EBX stored value as memory address, the memory address store data into edx.
    • Variable addressing, which alters the value of the register at the time of the indirect addressing.
    • The a&t compilation format used by Linux is slightly different from the Intel assembler.

2. Several important assembly instructions push pop call RET

      

    • Stack: eax This register to the stack bottom, ebp points to the bottom of the stack, and puts the EAX in the memory location where the ESP resides. The position of the stack is growing.
    • Stack: The position of the stack is shrinking.
    • Function call: Put the address 0x12345 address into the EIP, the EIP stores the current CPU to read the address of the instruction (instruction pointer), indicating that the CPU the next command from the 0x1234 start execution.
    • Return:ret is to restore the previously saved EIP, and RET will continue executing the next instruction after the function call.

* Indicates a pseudo-directive and cannot be used directly by the programmer. Since EIP registers cannot be directly modified, they can only be modified indirectly through directives.

3) Practice

Example 1

Example 2

Example 3

3. Assemble a simple C program

Experimental process

Experiment Code

Assembly code

Analyze assembly Code

The return value of the function is returned to the upper-level function by default using the EAX store

4. Summary

I gained a lot of knowledge through the first study of the Linux kernel Analysis course. Firstly, the knowledge of von Neumann architecture is reviewed, and the working model of stored program computer is learned from two aspects. Then there is a knowledge of the password that the CPU can recognize. The second part has learned the basic knowledge of X86, the three registers of the CPU, as well as several important ways of addressing the Register address, the register identifier with%, Operation Register, and memory Independent. Immediate addressing, the immediate number is the value beginning with $, regardless of memory. Direct addressing, which accesses data directly from a specified memory address. Indirect addressing, the value of the Register as a memory address to access memory, register EBX stored value as memory address, the memory address store data into edx. Variable addressing, which alters the value of the register at the time of the indirect addressing. After learning and applying several important assembly instructions, push pop call ret, the specific meanings of these instructions are better understood through the exercises.

  

How the first week of Linux kernel analysis notes computer works

Related Article

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.