CS61C: Great Ideas in Computer Architecture (aka Machine Structures)
Lecture 9: RISC-V Part 2: Data Transfer
Instructors: Lisa Yan, Justin Yokota
#
CS 61C
Spring 2024
Agenda
2
CS 61C
Spring 2024
Agenda
3
CS 61C
Spring 2024
RV32 So Far…
add rd, rs1, rs2
rd = rs1 + rs2
sub rd, rs1, rs2
rd = rs1 - rs2
Where rd, rs1, rs2 are registers (x0 - x31)
addi rd, rs1, imm
rd = rs1 + imm
li rd, imm
rd = imm
Not a "real" instruction, because we can use existing instructions to do the same thing:
li rd, 5 -> addi rd, x0, 5
However, this is common enough that RISC-V includes this as a pseudoinstruction. Pseudoinstructions get replaced with their real instruction counterpart by the assembler.
4
CS 61C
Spring 2024
Data Transfer: Load from and Store to memory
5
Very fast, �but limited space to hold values!
Much larger place �to hold values, �but slower than �registers!
CS 61C
Spring 2024
Review: How memory works
6
CS 61C
Spring 2024
Agenda
7
CS 61C
Spring 2024
Load Word
Load Word syntax:
Means: Compute imm+rs1, then load the 4 bytes at that address into rd
8
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Load Word
Example: lw x10 12(x5) if x5 is 0x100
9
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Store Word
Store Word syntax:
Means: Compute imm+rs1, then store the 4 bytes of rs2 into that address.
10
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Store Word
Example: sw x10 0(x5) if x5 is 0x100, x10 is 0x1234 5678
11
Byte (0x) | 78 | 56 | 34 | 12 | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Loading and Storing Bytes
In addition to word data transfers (lw, sw), RISC-V has byte data transfers:
Load and store one byte instead of a full word
Problem: If registers contain 4 bytes, how do we load/store only 1 byte?
12
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Store Byte
Example: sb x10 0(x5) if x5 is 0x100, x10 is 0x1234 5678
For sb, we store the least significant byte.
In the above example, 0x78 is the LSB.
So x100 gets set to 0x78
13
Byte (0x) | 78 | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Load Byte
Example: lb x10 0(x5) if x5 is 0x100
For lb, we extend the numeric value to a full 32 bits.
In the above example, we load the number 0xEF. What 32-bit number has the same numeric value as 0xEF?
Answer: It depends on your representation scheme
14
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Sign- and Zero-extending
There are two main representation schemes used: unsigned numbers, and 2's complement
For unsigned numbers: 8-bit 0xEF = 239. 239 -> 32 bits is 0x000000EF. General rule: Fill the top bits with 0s (Zero-extension).
For signed numbers: 8-bit 0xEF = -17. -17 -> 32 bits is 0xFFFF FFEF.
General rule: Fill the top bits with the most significant bit of the number (Sign-extension).
15
Typo adjusted (-16 → -17)
See Ed
CS 61C
Spring 2024
Loading and Storing Bytes
In addition to word data transfers (lw, sw), RISC-V has byte data transfers:
Load and store one byte instead of a full word
16
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
CS 61C
Spring 2024
Example: What is in x12?
li x11,0x93F5
sw x11,0(x5)
lb x12,1(x5)
17
CS 61C
Spring 2024
18
CS 61C
Spring 2024
Example: What is in x12?
li x11,0x93F5
sw x11,0(x5)
lb x12,1(x5)
19
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
Register | Value |
x11 | 0x00000000 |
x12 | 0x00000000 |
x5 | 0x00000100 |
Current Line
Random garbage since data isn't set
CS 61C
Spring 2024
Example: What is in x12?
li x11,0x93F5
sw x11,0(x5)
lb x12,1(x5)
20
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
Register | Value |
x11 | 0x000093F5 |
x12 | 0x00000000 |
x5 | 0x00000100 |
Current Line
CS 61C
Spring 2024
Example: What is in x12?
li x11,0x93F5
sw x11,0(x5)
lb x12,1(x5)
21
Byte (0x) | EF | BE | AD | DE | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
Register | Value |
x11 | 0x000093F5 |
x12 | 0x00000000 |
x5 | 0x00000100 |
Current Line
CS 61C
Spring 2024
Example: What is in x12?
li x11,0x93F5
sw x11,0(x5)
lb x12,1(x5)
22
Byte (0x) | F5 | 93 | 00 | 00 | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
Register | Value |
x11 | 0x000093F5 |
x12 | 0x00000000 |
x5 | 0x00000100 |
Current Line
Little-Endian order
CS 61C
Spring 2024
Example: What is in x12?
li x11,0x93F5
sw x11,0(x5)
lb x12,1(x5)
23
Byte (0x) | F5 | 93 | 00 | 00 | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
Register | Value |
x11 | 0x000093F5 |
x12 | 0x00000000 |
x5 | 0x00000100 |
Current Line
CS 61C
Spring 2024
Example: What is in x12?
li x11,0x93F5
sw x11,0(x5)
lb x12,1(x5)
24
Byte (0x) | F5 | 93 | 00 | 00 | 43 | 53 | 36 | 31 | 43 | 20 | 52 | 49 | 53 | 42 | 56 | 00 |
Address (0x) | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 10A | 10B | 10C | 10D | 10E | 10F |
Register | Value |
x11 | 0x000093F5 |
x12 | 0xFFFFFF93 |
x5 | 0x00000100 |
Current Line
Sign-extend: Top bit of 0x93 is 1, so fill with 1s
CS 61C
Spring 2024
Agenda
25
CS 61C
Spring 2024
Converting C code to RISC-V
So far, we've assumed that each variable gets stored in one register. What if we have more than 32 variables? Let's translate a program under the following restrictions:
26
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string"; // Array will get stored on stack�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
27
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
Step 1: Assign each variable to some offset from sp.
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
28
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
li t0 5
sw t0 0(sp)
29
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
li t0 0x73
sb t0 4(sp)
li t0 0x74
sb t0 5(sp)
li t0 0x72�sb t0 6(sp)�li t0 0x69�sb t0 7(sp)�li t0 0x6E�sb t0 8(sp)�li t0 0x67�sb t0 9(sp)
sb x0 10(sp)
30
CS 61C
Spring 2024
Converting C code to RISC-V (Better Approach)
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
li t0 0x69727473
sw t0 4(sp)
li t0 0x0000676E
sw t0 8(sp)
31
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
Nothing
32
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
lb t0 7(sp)
sb t0 52(sp)
33
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
lw t0 0(sp)
lbu t1 52(sp)
add t2 t0 t1
sw t2 28(sp)
34
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
a: 0(sp)
b: 4(sp)
c: 12(sp)
d: 52(sp)
�
li t0 20
lw t1 0(sp)
sw t0 t1*4+12(sp)
slli t1 t1 2 #t1*=4
addi t1 t1 12
add t1 t1 sp
sw t0 0(t1)
35
CS 61C
Spring 2024
Converting C code to RISC-V
int a = 5;�char b[] = "string";�int c[10];�uint8_t d = b[3];�c[4] = a+d;�c[a] = 20;
�
li t0 5
sw t0 0(sp)
li t0 0x69727473
sw t0 4(sp)
li t0 0x0000676E
sw t0 8(sp)
lb t0 7(sp)
sb t0 52(sp)
lw t0 0(sp)
lbu t1 52(sp)
add t2 t0 t1
sw t2 28(sp)
li t0 20
lw t1 0(sp)
slli t1 t1 2 #t1*=4
addi t1 t1 12
add t1 t1 sp
sw t0 0(t1)
36
CS 61C
Spring 2024
Why we need so many registers
37
CS 61C
Spring 2024
RISC-V Guiding Philosophy
38
Extremely fast
Extremely expensive�Tiny capacity
Fast
Priced reasonably�Medium capacity
CS 61C
Spring 2024
Speed of Registers vs Memory
39
CS 61C
Spring 2024
Jim Gray’s Storage Latency Analogy:
How Far Away is the Data?
40
Jim Gray�Turing Award
B.S. Cal 1966
Ph.D. Cal 1969
Registers
Memory
1
100
My Head
1.5 hr
1 min
Sacramento
CS 61C
Spring 2024
And in Conclusion…
lw, sw, lb, sb, lbu
41
CS 61C
Spring 2024