"In-depth understanding of computer systems" Fifth Week study notes

Source: Internet
Author: User

Chapter III Machine-level representation of the program

I. Summary of knowledge points

A

1. The computer executes the machine code, encodes the low-level operation with the byte sequence, including processing the data, managing the memory, reading and writing the data on the storage device, utilizing the network communication.

The 2.GCC C language compiler produces output in the form of assembly code, which is a textual representation of machine code. Give each instruction in the program, then GCC calls the assembler and linker to generate executable machine code based on assembly code

3. Reverse process-understand system creation process through research system and reverse work

(ii) Program code

1.gcc-01-o P p1.c p2.c (-01 tells the compiler to use first-level optimizations)

2.GCC calls a series of programs to convert the source code into executable code. ①C Preprocessor Extension source code ② compiler generates two source code assembly code p1.s,p2.s ③ Assembler converts assembly code to binary target code the p1.o,p2.o④ linker merges two target code files with code that implements the library function and produces a final executable code file p

3. Two abstractions of machine-level programming: The ① instruction set architecture ② the memory address used by the machine-level program is the virtual address, and the actual implementation of the memory system is to combine multiple hardware memory and operating system software.

4.gcc-01-s code.c uses the- s option to get The assembler code generated by the C language compiler, producing a code.s

5.gcc-01-c code.c using the- C option produces a target code file code.o

6. The disassembler determines the byte representation of the program, and the-D in theLinux system can act as the role objdump-d code.o

(iii) data format

1.intel with "word" means the data type, the number of digits is "double word"

2. Most GCC -generated assembly code directives have a character suffix that indicates the size of the operand. Data transfer instruction:movb(transfer byte),movw(transfer word),movl(double word transfer). Note: Assembly code also uses the suffix l to represent 4 - byte integers and 8 -byte double-precision floating-point numbers

(iv) Access to information

1. A CPU contains a set of 8 registers that store The value of the values, which is used to store integer data and pointers

2. Operand type: Immediate Number ($ followed by an integer denoted by standard C notation ), register (%ax... ), Memory

3. Data Transfer Instructions

①MOV equivalent to C language Assignment "="

② Note the direction in the ATT format

③ note can not from memory address direct MOV to another memory address, to use register to relay a bit

④MOV: Copies the value of the source operand to the destination operand. PUSHL: pushes data into the stack. popl: Eject data

4. imm (Eb,ei,s)  = imm + r[eb] + r[ei]*s imm is an immediate number offset, eb Span style= "font-family: the song Body;" > is the base address register, ei is the variable-address register, is the scale factor (must be Span style= "Font-family:times New Roman;" >1 2 , 4 or 8

The 5.PUSHL function is to press the data onto the stack,popl is the popup data, these instructions have only one operand. Note that the address of the top element of the stack is the lowest of all the element addresses in the stack.

6. The pointer is the address; Local variables are stored in registers

(v) Arithmetic and logic operations

1. Load a valid address (P141)

2. Unary operation and two Yuan operation (P142)

3. Shift operation (P143)

4. Special arithmetic operations (P145)

(vi) control

1. Condition code (see P147)

2. Access condition code: Set one byte to 0 or 1According to a combination of criteria code, condition to jump to some other part of the program, conditionally transfer the data

3. Jump instruction and its encoding

Conditional jump (Implement if,switch,while,for), unconditionally jump to jmp ( implement goto). Conditional jump conditions Look at the condition code register, note that Leal does not change the condition code register.

4. Loop:Do While,while, for

5. Conditional Delivery Instructions

6.Switch statement: multiple branches

(vii) Process

A procedure call involves passing data (in the form of procedure parameters and return values) and control from one part of the code to another. You must allocate space for the local variables of the procedure when you enter, and free the space when you exit.

The IA32 implements the procedure call through the stack.

1. Stack frame structure: Stack supports process calls, machine stacks to pass process parameters, store return information, save registers for later recovery and local storage. The portion assigned to a single procedure becomes a stack frame. The register %ebp is the frame pointer, the register %esp is the stack pointer, the stack pointer is movable

2. Transfer control

Call/ret; function return value exists %eax in

3. Register Usage Conventions

Note: When a caller invokes a callee, the callee does not overwrite the value of a register that the caller will later use.

%EAX,%EDX,%ECX Caller Save Register (Q can overwrite,P 's data will not be destroyed)%ebx,%esi,%edi The callee saves the registers (Q must be pressed into the stack before overwriting these values and replying to them before returning)%EBP,%ESP Convention keeps %eax used to hold the return value

That is, to save a value to be available for future operations, there are two options:

① is saved by the caller. The stack is pressed before it is called.

The ② is saved by the callee, pressed into the stack when it was called, and resumed before returning.

4. Process example (Note:GCC allocates space that is never used)

5. Recursion process

(eight) array allocation and access

1. declaration of the data type T and integer constant N 5:T a[n];

2. Pointer arithmetic: the operator of the single operand & and * can produce pointers and indirect reference pointers. the Leal instruction is used to generate the address,movl To reference the memory

3. Nested arrays

4. Fixed-length array (it is best to use a constant as the dimension or buffer size of an array when using a #define declaration to associate the constant with a name, followed by the name instead of the constant value)

5. Variable-length array (the dimension of the allowed array is an expression)

(ix) Heterogeneous data structures

1. Two mechanisms for creating data types with different types of objects: structure (structure), Union (Union)

2. Structure: The selection of the various fields of the structure is processed at compile time, the machine code does not contain information about the field declaration or the field name.

3. Union: Allows you to reference an object in multiple types, using different fields to refer to the same memory block. The total size of a union equals the size of its maximum field. Unions can also be used to access bit patterns for different data types.

4. Data alignment: A type object must be a multiple of a K value (usually 2,4 , or 8). This alignment restriction simplifies the hardware design that forms the interface between the processor and the memory system

(10) Synthesis: Understanding pointers

1. Each pointer corresponds to a type

2. Each pointer has a value

3. Pointers are created with the & operator

4. Operator * Indirect reference for pointers

5. Arrays are closely linked to pointers

6. Cast the pointer from one type to another, changing only its type without changing its value

7. Pointers can also point to functions

(11) Application: Using GDB Debugger

start gdb with gdb prog

The gdb command about the stack frame is as follows:

Second, the experimental process

The source code written in the lab environment is as follows:

Executes Gcc-s-o main.s main.c-m32 command after compiling into assembly code

Third, the problems encountered

For the experiment how to extract the assembly code sequence is not very well understood, also not very clear each instruction corresponding stack frame situation, hoped that the teacher can explain the concrete process for us in the class.

"In-depth understanding of computer systems" Fifth Week study notes

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.