Study Hub

ISA / High

Assembly Tracing

RISC-V code를 PC, register, memory state 변화로 한 줄씩 실행하는 기술입니다.

Why This Matters

Trace는 Uebung 2, ABI stack, pipeline hazard의 공통 기반입니다.

state 변화를 직접 쓰면 load/store와 branch 실수를 줄일 수 있습니다.

Datapath에서 ALU, register file, memory, PC mux가 왜 필요한지도 자연스럽게 보입니다.

Beginner Story

PC는 지금 읽을 instruction 주소입니다.

register는 CPU 안의 작은 칸이고 memory는 address로 찾는 큰 표입니다.

instruction 하나는 old state를 읽고 new state 일부만 바꿉니다.

trace는 changed state만 정확히 갱신하는 연습입니다.

Glossary / Key Terms

  • PC현재 instruction 주소입니다. Exam tip: 각 step의 첫 칸입니다.
  • registerCPU 안의 빠른 저장 공간입니다. Exam tip: ABI alias와 x-number를 연결합니다.
  • x0항상 0인 register입니다. Exam tip: writeback을 무시합니다.
  • immediateinstruction 안의 constant입니다. Exam tip: signed offset인지 확인합니다.
  • effective addressload/store가 접근하는 실제 memory address입니다. Exam tip: base + offset으로 계산합니다.
  • loadmemory에서 register로 읽습니다. Exam tip: destination register가 바뀝니다.
  • storeregister 값을 memory에 씁니다. Exam tip: register는 보통 바뀌지 않습니다.
  • branch taken조건이 참이어서 target으로 가는 경우입니다. Exam tip: skipped line은 실행하지 않습니다.
  • jump조건 없이 target으로 PC를 바꿉니다. Exam tip: jal은 rd에 PC+4도 씁니다.
  • writeback결과를 register에 쓰는 단계입니다. Exam tip: sw와 branch는 보통 writeback이 없습니다.

Step-by-Step Method

  1. 1. 현재 PC 선택PC가 가리키는 instruction 하나만 실행합니다.
  2. 2. old state 읽기source register와 memory 값은 step 시작 상태에서 읽습니다.
  3. 3. 계산ALU result, effective address, branch condition을 씁니다.
  4. 4. changed state 갱신바뀐 register 또는 memory만 갱신합니다.
  5. 5. next PC 결정일반 instruction은 PC+4, taken branch와 jump는 target입니다.

Visual Model

PC=0x100inst=addiold t0=0new t0=3next PC=0x104

PC register memory trace table

PC, instruction, reads, calculation, writeback, next PC 열을 가진 표입니다.

PC, instruction, read rs1 rs2 imm, calculation, register write, memory write, next PC

Instruction state transition

old state에서 new state로 가는 흐름입니다.

fetch by PC, read old values, compute, write changed state, select next PC

Interactive Visual

Instruction Trace Stepper

PC, register, memory state가 instruction마다 어떻게 바뀌는지 한 step씩 따라가세요.

ready

JavaScript가 켜져 있으면 이 영역이 조작 가능한 visual lab으로 바뀝니다.

Worked Examples

addi와 add

Problem: 0x100 addi t0,zero,3; 0x104 add t3,t0,t1 with t1=7

  1. 첫 줄에서 t0=3입니다.
  2. 두 번째 줄은 새 t0=3을 읽습니다.
  3. t3=10, PC=0x108입니다.

source는 각 step의 old state에서 읽습니다.

store 뒤 load

Problem: sw t0,8(sp); lw t1,8(sp) with sp=0x8000 and t0=13

  1. store는 memory[0x8008]=13을 만듭니다.
  2. 다음 load는 바뀐 memory 값을 읽습니다.
  3. t1=13입니다.

초기 memory 99를 계속 쓰면 안 됩니다.

Common Mistakes

  • PC를 먼저 PC+4로 바꿉니다.current PC instruction을 실행한 뒤 next PC를 씁니다.
  • sw 뒤 register가 바뀐다고 씁니다.store는 memory write입니다.
  • taken branch 뒤 skipped line을 실행합니다.PC flow만 따라갑니다.

Active Recall

  • trace step의 첫 값은Hint: 실행할 줄을 고릅니다.
    정답 확인

    current PC입니다.

  • lw와 sw의 방향 차이는Hint: memory와 register 사이의 방향입니다.
    정답 확인

    lw는 memory to register, sw는 register to memory입니다.

  • x0에 write하면Hint: hardwired zero입니다.
    정답 확인

    무시되어 x0은 0입니다.

Exam Connection

Uebung 2 hand execution은 이 table을 채우는 문제입니다.

Klausur 답안에서는 changed register, changed memory, next PC를 분리하면 실수가 줄어듭니다.

Source Grounding

Grounding entries are course-file and source-window hints for study. When a problem needs an exact page number, branch penalty, address, or formula convention, verify the cited PDF window before finalizing the answer.

Full beginner lecture

Assembly Tracing

Intuition

Assembly tracing은 CPU가 된 것처럼 한 instruction씩 state를 바꾸는 연습입니다. state는 보통 PC, relevant registers, relevant memory입니다. 좋은 trace는 모든 register를 다시 쓰지 않습니다. 바뀐 register, 바뀐 memory, next PC만 정확히 표시합니다.

Rule

각 step은 같은 순서로 처리합니다. current PC의 instruction을 고릅니다. source register와 immediate를 old state에서 읽습니다. ALU result, effective address, branch condition을 계산합니다. register writeback 또는 memory write를 적용합니다. 마지막에 next PC를 정합니다.

일반 RV32I instruction은 4 byte이므로 next PC는 보통 PC+4입니다. taken branch와 jump는 target으로 갑니다. x0는 write해도 0입니다. lw는 memory에서 register로, sw는 register에서 memory로 값이 이동합니다.

Visual Block

One-step trace board

PC       instruction       read values       calculation        writeback       next PC
0x100    addi t0,zero,3    zero=0 imm=3      0+3=3             t0=3           0x104
0x104    sw t0,8(sp)       t0=3 sp=0x8000    EA=0x8008         mem[0x8008]=3  0x108
Memory direction for load and store

lw t1, 8(sp):  memory[sp+8]  -> t1
sw t1, 8(sp):  t1            -> memory[sp+8]

Worked Example 1

Initial state. PC=0x100, zero=0, t1=7, t2=5.

0x100: addi t0, zero, 3
0x104: add  t3, t0, t1
0x108: addi t2, t2, -2
  1. At 0x100, 0+3=3, so t0=3, next PC=0x104.
  2. At 0x104, read current t0=3 and t1=7, so t3=10, next PC=0x108.
  3. At 0x108, read old t2=5, so t2=3, next PC=0x10C.

Final changed registers are t0=3, t3=10, t2=3.

Worked Example 2

Initial state. PC=0x200, sp=0x8000, t0=13, memory[0x8008]=99.

0x200: sw t0, 8(sp)
0x204: lw t1, 8(sp)
0x208: addi t1, t1, 1
  1. sw computes EA=0x8000+8=0x8008 and stores t0=13. Memory becomes memory[0x8008]=13.
  2. lw reads the current memory value 13 and writes t1=13.
  3. addi reads old t1=13, writes t1=14.

Final state is memory[0x8008]=13, t1=14, PC=0x20C.

Common Mistakes

Active Recall

  1. What is the first value to inspect in a trace step.
  2. In addi t0,t0,1, do you read old t0 or new t0.
  3. For beq t0,t1,L, what happens when the values differ.
  4. What changes after sw t1,0(sp).

Source Grounding