Machine-Level Programming II: Control¶
约 376 个字 70 行代码 2 张图片 预计阅读时间 3 分钟 共被读过 次
15-213/14-513/15-513: Introduction to Computer Systems
4th Lecture, Sept 5, 2024
📌 Overview¶
- Key Topics:
- Condition Codes (EFLAGS)
- Conditional Branches & Loops
- Switch Statements
- References: CSAPP 3.6.1–3.6.8
🔍 Review: Machine Instructions & Addressing Modes¶
1️⃣ movq
Instruction¶
- C Code:
*dest = t;
- Assembly:
movq %rax, (%rbx)
- Moves 8-byte value from
%rax
to memory locationM[%rbx]
. - Operands:
t
: Register%rax
dest
: MemoryM[%rbx]
2️⃣ General Addressing Mode¶
- Syntax:
D(Rb, Ri, S)
- Meaning:
Mem[Reg[Rb] + S * Reg[Ri] + D]
- Components:
D
: Displacement (½/4 bytes)Rb
: Base registerRi
: Index register (≠%rsp
)S
: Scale (½/4/8)
Special Cases:¶
Syntax | Meaning |
---|---|
(Rb) | Mem[Reg[Rb]] |
(Rb, Ri) | Mem[Reg[Rb] + Reg[Ri]] |
D(Rb, Ri) | Mem[Reg[Rb] + Reg[Ri] + D] |
(Rb, Ri, S) | Mem[Reg[Rb] + S * Reg[Ri]] |
3️⃣ lea
vs. Memory Access¶
lea
(Load Effective Address):- Does NOT access memory! Computes address and stores it in a register.
- Example:
- Use Cases:
- Pointer arithmetic (e.g., array indexing).
- Multi-operand calculations (e.g.,
rax = rbx * 3
vialea (%rbx, %rbx, 2), %rax
).
🚩 Condition Codes (EFLAGS)¶
Implicitly set by arithmetic/logical operations:
- CF (Carry Flag): Unsigned overflow.
- ZF (Zero Flag): Result is zero.
- SF (Sign Flag): Result is negative (signed).
- OF (Overflow Flag): Signed overflow.
⚙️ cmp
and test
Instructions¶
cmp a, b
: Computesb - a
and sets flags.test a, b
: Computesb & a
and sets flags (SF/ZF).- Common use:
test %rax, %rax
to check if%rax
is zero.
🔀 Conditional Branches¶
Jump Table (jX
Instructions)¶
Instruction | Condition | Description |
---|---|---|
jmp | Always | Unconditional jump |
je /jz | ZF=1 | Jump if equal/zero |
jne /jnz | ZF=0 | Jump if not equal |
jg | ~(SF^OF) & ~ZF | Jump if greater (signed) |
ja | ~CF & ~ZF | Jump if above (unsigned) |
🔄 Loops in Assembly¶
1️⃣ Do-While Loop¶
- C Code:
- Assembly:
2️⃣ While Loop (Jump-to-Middle)¶
- C Code:
- Assembly:
🔀 Switch Statements¶
- Jump Table: Maps case values to code addresses.
-
Example:
-
Assembly:
GASmy_switch: cmpq $6, %rdi # Compare x to 6 ja .L8 # If x > 6, jump to default jmp *.L4(,%rdi,8) # Jump via table at .L4 + x*8 .L3: movq %rsi, %rax # w = y imulq %rdx, %rax # w *= z ret .L5: movq %rsi, %rax cqto idivq %rcx # w = y/z jmp .L9 .L9: addq %rdx, %rax # w += z ret .L7: movq $1, %rax subq %rdx, %rax # w -= z ret .L8: movq $2, %rax # default: w = 2 ret
📊 Register Usage Table¶
Register | Use(s) |
---|---|
%rdi | Argument x |
%rsi | Argument y |
%rdx | Argument z |
%rax | Return value w |
🎯 Key Takeaways¶
- Condition Codes: Set implicitly by arithmetic operations.
- Branches: Use
jX
instructions to control flow. - Loops: Translated into conditional jumps and labels.
- Switch Statements: Implemented via jump tables for efficiency.
📝 Activity Time!¶
- Parts 1-4 (Q1-Q6): Practice with condition codes and loops.
- Parts 5-6 (Q7-Q11): Explore conditional moves and switch statements.
- Canvas Quiz: Complete the Day 4 quiz.