跳转至

Lecture 8 Data Transfer

Data Transfer: Load from and Store to

alt text

  • Data typically smaller than 32 bits, but rarely smaller than 8 bits (e.g., char type) – works fine if everything is a multiple of 8 bits
  • 8 bit chunk is called a byte (1 word = 4 bytes)
  • Memory addresses are really in bytes, not words

alt text

Word-Level Transfer

lw / sw

load to rsg

C
1
2
int a[100];
g = h + a[3];

In RISC-V:

Text Only
1
2
lw x10, 12(x15) # x10 get a[3]
add x11, x12, x10 # g = h + a[3]
  • x15: base register (pointing to a)
  • 12: offset in bytes
  • Offset must be a constant known at assembly time

store to memory

C
1
2
int a[100];
a[10] = h + a[3];

In RISC-V:

Text Only
1
2
3
lw x10, 12(x15) # x10 get a[3]
add x11, x12, x10 # g = h + a[3]
sw x10, 40(x15) # a[10] = g

(x15 + 12), (x15 + 40) must be multiples of 4

Byte-Level Transfer

lb / lbu / sb

  • load byte from: lb(signal-extend) / lbu (0-extend)
  • store byte to: sb

alt text

  1. lb is 32-bit instruction, but only 8 bits are used
  2. lb sign-extends the 8-bit value to 32 bits
  3. lbu is the same as lb, but zero-extends the 8-bit value to 32 bits

???+ note why have sb, but not sbu

Text Only
1
2
3
RISC-V 有"无符号字节"加载指令(lbu),它会将加载的字节零扩展以填充寄存器。但是没有"无符号存储字节"(sbu)指令:

符号扩展只在将数据加载到更大的寄存器时才有意义。当从寄存器(32)存储数据到内存(8)时,不需要符号扩展或零扩展,因为只有最低有效位会被写入内存。

Summary

  • Memory: byte-addressable (1 byte = 8 bits)
  • Register: word-size (1 word = 4 bytes = 32 bits)

exercise:

alt text

alt text