RISC-V Data Transfer
Announce-�ments
UC Berkeley�Teaching Professor �Dan Garcia
cs61c.org
Garcia, Kao
CS61C
Great Ideas
in�Computer Architecture
(a.k.a. Machine Structures)
08 RISC-V Data Transfer (1)
Garcia, Kao
Storing Data in Memory
08 RISC-V Data Transfer (2)
Garcia, Kao
RV32 So Far…
add rd, rs1, rs2
R[rd] = R[rs1] + R[rs2]
sub rd, rs1, rs2
R[rd] = R[rs1] - R[rs2]
addi rd, rs1, imm
R[rd] = R[rs1] + imm
08 RISC-V Data Transfer (3)
Garcia, Kao
Data Transfer: Load from and Store to memory
Much larger place �to hold values, �but slower than �registers!
Very fast, �but limited space to hold values!
C
on
t
r
ol
D
a
t
ap
at
h
P
r
o
g
r
am
C
oun
t
er (PC)
R
egi
s
t
e
r
s
A
r
i
t
hm
e
t
i
c
-Logic
U
nit
(
A
L
U)
P
r
o
c
e
s
sor
Memo
r
y
Enabl
e
?
R
ead
/
W
r
i
t
e
A
dd
r
e
s
s
W
r
i
t
e
D
a
t
a
R
ead
D
a
t
a
I
n
put
Ou
t
put
B
y
t
e
s
=
S
t
o
r
e
t
o
Memo
r
y
= Load from
Memo
r
y
P
r
o
g
r
am
D
a
t
a
08 RISC-V Data Transfer (4)
Garcia, Kao
Memory Addresses are in Bytes
really in bytes, not words
31
0
3 |
2 |
1 |
0 |
08 RISC-V Data Transfer (5)
Garcia, Kao
Memory Addresses are in Bytes
really in bytes, not words
Least-significant byte �in a word
31 24
23 16
15 8
7 0
Least-significant byte�gets the smallest address
15 | 14 | 13 | 12 |
11 | 10 | 9 | 8 |
7 | 6 | 5 | 4 |
3 | 2 | 1 | 0 |
08 RISC-V Data Transfer (6)
Garcia, Kao
Big Endian vs. Little Endian
Little Endian
ADDR3 ADDR2 ADDR1 ADDR0� BYTE3 BYTE2 BYTE1 BYTE0 �00000000 00000000 00000100 00000001
Examples
Names in the US (e.g., Dan Garcia)
Internet names (e.g., cs.berkeley.edu)
Dates written in Europe DD/MM/YYYY (e.g., 07/09/2020)
Eating Pizza skinny part first
The adjective endian has its origin in the writings of 18th century writer Jonathan Swift. In the 1726 novel Gulliver's Travels, he portrays the conflict between sects of Lilliputians divided into those breaking the shell of a boiled egg from the big end or from the little end. He called them the "Big-Endians" and the "Little-Endians".
Consider the number 1025 as we typically write it:�BYTE3 BYTE2 BYTE1 BYTE0�00000000 00000000 00000100 00000001
en.wikipedia.org/wiki/endianness
Big Endian
ADDR3 ADDR2 ADDR1 ADDR0� BYTE0 BYTE1 BYTE2 BYTE3 �00000001 00000100 00000000 00000000
Examples
Names in China or Hungary (e.g., Garcia Dan)
Java Packages: (e.g., org.mypackage.HelloWorld)
Dates in ISO 8601 YYYY-MM-DD (e.g., 2020-09-07)
Eating Pizza crust first
08 RISC-V Data Transfer (7)
Garcia, Kao
Data Transfer Instructions
08 RISC-V Data Transfer (8)
Garcia, Kao
Great Idea #3: Principle of Locality / Memory Hierarchy
Extremely fast
Extremely expensive�Tiny capacity
Processor chip
Fast
Priced reasonably�Medium capacity
DRAM chip
e.g. DDR3/4/5 HBM/HBM2/3
P
hy
si
c
al Memo
r
y
R
andom
-
A
cc
e
s
s Memo
r
y (RAM)
C
o
r
e
Registers
08 RISC-V Data Transfer (9)
Garcia, Kao
Speed of Registers vs. Memory
08 RISC-V Data Transfer (10)
Garcia, Kao
Jim Gray’s Storage Latency Analogy: �How Far Away is the Data?
Jim Gray�Turing Award
B.S. Cal 1966
Ph.D. Cal 1969
he
The
ima
…
T
h
T
he
im
T
h
e
T
h
e
T
h
T
h
e
T
h
T
he
im
The
ima
ge
…
The
image
part
…
T
h
T
h
T
h
T
h
T
h
e
T
h
T
h
T
he
T
he
The
ima
…
The
i
…
T
he
The
imag
…
The
ima
…
The
image
…
The
image
…
The
i
…
Registers
Memory
1
100
Sacramento
My Head
1.5 hr
1 min
T
h
e
T
h
e
T
h
e
T
h
T
h
e
T
he
im
The
ima
g
…
The
ima
ge
…
The
ima
g
…
The
ima
ge
…
The
imag
e
p
…
The
image
part
…
The
image
part
…
The
image
part
…
The
image
part
…
The
image
part wit
…
The
image
part
…
The image
part with
relationsh
…
The
image
part wit
…
The
im
…
[ns]
08 RISC-V Data Transfer (11)
Garcia, Kao
Load from Memory to Register
int A[100];
g = h + A[3];
lw x10,12(x15) # Reg x10 gets A[3]
add x11,x12,x10 # g = h + A[3]
Note: x15 – base register (pointer to A[0])
12 – offset in bytes
Offset must be a constant known at assembly time
Data flow
08 RISC-V Data Transfer (12)
Garcia, Kao
Store from Register to Memory
int A[100];
A[10] = h + A[3];
lw x10,12(x15) # Temp reg x10 gets A[3]
add x10,x12,x10 # Temp reg x10 gets h + A[3]
sw x10,40(x15) # A[10] = h + A[3]
Note: x15 – base register (pointer)
12,40 – offsets in bytes
x15+12 and x15+40 must be multiples of 4
Data flow
08 RISC-V Data Transfer (13)
Garcia, Kao
Loading and Storing Bytes
byte�loaded
xzzz zzzz
This bit
…is copied to “sign-extend”
xxxx xxxx xxxx xxxx xxxx xxxx
x10:
RISC-V also has “unsigned byte” loads (lbu) which zero extends to fill register. Why no unsigned store byte ‘sbu’?
08 RISC-V Data Transfer (14)
Garcia, Kao
What ends up in x12 ?
addi x11,x0,0x39C
sw x11,0(x5)
lb x12,0(x5)
x5
x11
x12
Memory
08 RISC-V Data Transfer (15)
Garcia, Kao
08 RISC-V Data Transfer (16)
Garcia, Kao
Example: Translate *x = *y
We want to translate *x = *y into RISC-V
x, y ptrs stored in: x3 x5
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)
1
2
3
4
5→6
6→5
7→8
08 RISC-V Data Transfer (17)
Garcia, Kao
08 RISC-V Data Transfer (18)
Garcia, Kao
And in Conclusion…
lw, sw, lb, sb, lbu
08 RISC-V Data Transfer (19)
Garcia, Kao
Substituting addi
The following two instructions:
lw x10,12(x15) # Temp reg x10 gets A[3]
add x12,x12,x10 # reg x12 = reg x12 + A[3]
Replace addi:
addi x12, value # value in A[3]
But involve a load from memory!
Add immediate is so common that it deserves its own instruction!
08 RISC-V Data Transfer (20)
Garcia, Kao