目录 ← 首页
CS61C

Short Examples

Learning Outcomes

  • Practice loads and stores.
  • Translate array accesses in C code into assembly instructions (see Long Example).

No video. We recommend pulling up the memory section of the RISC-V Green Card.

Example 1

Consider the assembly code:

li x11 0x93F5 
sw x11 0(x5)
lb x12 1(x5)

Suppose that the memory layout starts as in #fig-rv-example-x12. After executing these instructions, what is in x12?

Initial state: registers x5, x11, and x12 hold 0x100, 0xABCDEFAB, and 0xCDEFABCD beside a little-endian memory grid from 0x100 through 0x10C with associated byte values.

Starting memory layout for Example 1.

Hint: See the section on pseudoinstructions like li.

Example 2

Suppose that x and y are int * pointers whose values are in registers x3 and x5.

How do we translate the statement *x = *y; into assembly?

Consider the following instructions:

  1. add x3 x5 zero
  2. add x5 x3 zero
  3. lw x3 0(x5)
  4. lw x5 0(x3)
  5. lw x8 0(x5)
  6. sw x8 0(x3)
  7. lw x5 0(x8)
  8. sw x3 0(x8)

And consider the following choices:

  • A. 1
  • B. 2
  • C. 3
  • D. 4
  • E. 5 → 6
  • F. 6 -> 5
  • G. 7 → 8
  • H. Something else