CMU Computer Systems: Machine-Level Programming (Control)

99 阅读1分钟

Control: Condition codes

Processor State

  • Information about currently executing program

    • Temporary data
    • Location of runtime stack
    • Location of current code control point
    • Status of recent tests

Condition Codes (Implicit Setting)

  • Single bit registers

    • CF, SF, ZF, OF
  • Implicitly set by arithmetic operations

  • Not set by leaq instruction

Condition Codes (Explicit Setting)

  • Explicit Setting by Compare Instruction

    • cmpq b,a like computing a-b without setting destiation
  • Explicit Setting by Test instruction

    • testq b,a like computing a&b without setting destination
    • Sets condition codes based on value of a&b

Reading Condition Codes

  • SetX Instructions

    • Set low-order byte of destination to 0 or 1 based on combinations of condition codes
  • One of addressable byte registers

    • Does not alter remaining bytes
    • Typically use movzbl to finish job

Conditional branches

Jumping

  • jX Instructions

    • Jump to different part of code depending on condition codes

Expressing with Goto Code

  • C allows goto statement
  • Jump to position designated by label

Using Conditional Moves

  • Conditional Move Instructions

    • Instruction supports
    • Supported in post-1995x86 processors
    • GCC tries to use them

Bad Cases for Conditional Move

  • Expensive Computations
  • Risky Computations
  • Computations with side effects

Loops

Do-While Loop

  • Use conditional branch

While Loop

  • Initial goto starts loop at test
  • Initial conditional guards entrance to loop

For Loop

  • Init Test Update Body

Switch Statements

Prototype

  • Multiple case labels
  • Fall through cases
  • Missing cases

Structure

  • From Jump Table to Jump Targets