Chapter III Machine-level representation of the program

Source: Internet
Author: User

Machine-level representation of the program

3.1 Historical View

8086-〉80286-〉i386-〉i486-〉pentium-〉pentiumpro-〉pentium-〉pentium-〉pentium4-〉pentium4e-〉core 2 Duo-〉core i7

3.2 Program code

1.gcc-01–o p p1.c p2.c using first level optimization

2. The program counter (%EIP) indicates the address of the next instruction that will be executed in memory.

3. Register File

Assembly code generated by the 4.-S:C language compiler

Example: Gcc-01–s code.c will produce a compilation file code.c

3.3 Data formats

Char

Bytes

B

1

Short

Word

W

2

Int

Double word

1

4

Long int

Double word

1

4

Long Long int

4

char*

Double word

1

4

Float

Single precision

S

4

Double

Double precision

L

8

Long double

Extended Precision

T

10/12

3.4 Access Information

1. Operand indicator Type: Immediate count, register, register

2. Data transfer Instructions

instruction

Effect

Description

mov      s,d

/td>

s<-d

Transfer

Movb

MOVW

Movl

Transfer bytes

Routing Word

Transfer double character

 

movs     s,d

d<-symbol extension (S)

Transfer byte of symbol extension

movz     s,d

/td>

d<-0 extension (S)

Shipping 0 Extended bytes

3.5 Arithmetic and logic operations (20135315 Han Yuqi's blog)

1. Load valid address: Leal is actually a MOVL variant that generates pointers for memory references

2. Unary operation and second courtyard operation: 1) ++,--;2) + =

    • Unary operation
    • -INC plus 1
    • -DEC minus 1
    • -NEG Take negative

-Not take compensation

    • Only one operand, both a source and a destination, can be a register, or a memory location.
    • Binary operation
    • -Add Plus
    • -SUB minus
    • -Imul Multiply
    • -XOR different or
    • -OR OR

-AND and

    • The first operand can be an immediate number, a register, or a memory location
    • The second operand is both a source and a destination. Can be a register or a memory location, but not a memory location at the same time.
    • Note The order of operations:

The first operand of the second operand operator

3. Shift Operation:>>,<<

    • The shift amount is given first, and the second item gives the value to be shifted.
    • -SAL left Shift
    • -SHL left shift (equivalent to Sal)
    • -SAR Arithmetic right shift

-SHR Logical Right Shift

    • SOURCE operand (shift amount): Immediate number or in single-byte register element%CL.
    • Intent operand: A register or a memory location.

4. Special Arithmetic operations

    • Multiplication
    • Product truncation
    • Imull double operand

-Produces a 32-bit product from two 32-bit operands.

    • Product Not truncated
    • Mull unsigned number multiplication
    • Imull signed number multiplication
    • -Requires that one parameter must be in the register%eax and the other as the source operand of the instruction.

-The high 32 bits of the product are in%edx, and the low 32 bits are in%eax.

    • Division
    • Signed Division
    • IDIVL operands
    • -Dx:ax 64-digit number as dividend, divisor in operand

-Result: Quotient in ax, remainder in DX.

    • Unsigned division
    • DIVL directive

-The register%edx is usually set to 0 in advance.

3.6 Control

1. Condition Code:

CF: Carry sign ZF: 0 sign SF: symbol mark of: Overflow flag

2. Access Condition code

    • Set instruction: Performs a comparison instruction and sets the condition code based on the results of the computed t=a-b

3. Jump instruction and its code: JMP *%eax

    • Unconditional Jump
    • Direct jump: The jump target is encoded as part of the instruction.
    • Indirect jump: A jump target is read from a register or memory location.

4. Conditional delivery Instructions (reference 20135202 Shang blog)

    • The most common way to translate conditional expressions and statements from C to machine languages is to combine conditional and unconditional jumps.
    • Assembly structure of the If-else
    • Generic form template
    • if (test-expr)
    • Then-statement
    • Else
    • Else-statement

(Note: test-expr integer expression [false/true])

    • Assembly Implementation Form
    • t = test-expr;
    • if (!t)
    • Goto false;
    • Then-statement
    • Goto done;
    • False
    • Else-statement

Done

5.switch statements

3.7 Process

1. Stack frame structure: The machine uses stack frames to pass process parameters, store return information, save registers for future replies, and local storage. The portion of the stack allocated for a single process is called a stack frame

2. Frame pointer:%EBP, stack pointer:%%esp

3. Transfer Control

Call Label Procedure Calls

Call *operand Procedure Calls

Leave to prepare the stack for return

RET returns from the procedure call

4. Register Usage Conventions

1).%eax,%edx,%ecx caller Save

2).%EBX,%esi,%edi are saved by the caller

5. Recursive procedure: Recursive invocation of a function itself is the same as calling other functions. More complex calls to each other

Problem:

1. What is the difference between the comparison instruction CMP and the subtraction instruction sub?

The Sub d,s is D-S, and the result is sent back to the destination operand.

CMP D,s is also D-S, but the results are not sent back to the target operand, and are compared using subtraction for two numeric values.

Homework

    • MAIN.C:

    • Assembly Code:

    • To view compiler directives with VI:

    • Remove the GCC generation code with "." After the start of the compiler directive:

    • Analysis:
    • The main function saves the%EBP and sets a new frame pointer.

      pushl%ebpmovl%esp,%ebp
    • Allocating 4 bytes of stack space

      subl$4,%esp
    • Set Arg1=8

      movl$8,(%esp)
    • Call Invoke FH
    • FH is called, initializes the frame pointer, allocates the stack space.
    • %ESP (8) to%eax, which is stored in the stack

      movl%eax,(%esp)
    • Call calls GH
    • GH is called, initializes the stack pointer, allocates stack space
    • Add%eax to immediate number 3

      add$3,%eax
    • Play stack before GH ends

      popl%ebp
    • RET returns the call position in FH
    • FH also ends, return returns to the location of call in main
    • Main continues the%eax plus 1 operation

      addl$1,%eax
    • Leave to return to prepare the stack, equivalent to%ebp out of the stack, finally ret end.

Chapter III Machine-level representation of the program

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.