1 of 20

RISC-V Data Transfer

Announce-�ments

  • proj2a due date moved to monday (september 23rd)
  • lab3 due date moved to thursday (september 19th)
  • proj2b due date remaining the same

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

2 of 20

Storing Data in Memory

08 RISC-V Data Transfer (2)

Garcia, Kao

3 of 20

RV32 So Far…

  • Addition/subtraction

add rd, rs1, rs2

R[rd] = R[rs1] + R[rs2]

sub rd, rs1, rs2

R[rd] = R[rs1] - R[rs2]

  • Add immediate

addi rd, rs1, imm

R[rd] = R[rs1] + imm

08 RISC-V Data Transfer (3)

Garcia, Kao

4 of 20

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

5 of 20

Memory Addresses are in Bytes

  • 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

  • Word addresses are �4 bytes apart
    • Word address is same �as address of rightmost byte �– least-significant byte�(i.e. Little-endian convention)

31

0

3

2

1

0

08 RISC-V Data Transfer (5)

Garcia, Kao

6 of 20

Memory Addresses are in Bytes

  • 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

  • Word addresses are �4 bytes apart
    • Word address is same �as address of rightmost byte �– least-significant byte�(i.e. Little-endian convention)

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

7 of 20

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 BYTE000000000 00000000 00000100 00000001

  • The order in which BYTES are stored in memory
  • Bits always stored as usual within a byte (E.g., 0xC2=0b 1100 0010)

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

8 of 20

Data Transfer Instructions

08 RISC-V Data Transfer (8)

Garcia, Kao

9 of 20

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

10 of 20

Speed of Registers vs. Memory

  • Given that
    • Registers: 32 words (128 Bytes)
    • Memory (DRAM): Billions of bytes �(2 GB to 96 GB on laptop)
  • and physics dictates…
    • Smaller is faster
  • How much faster are registers than DRAM??
    • About 50-500 times faster! �(in terms of latency of one access - tens of ns)
      • But subsequent words come every few ns

08 RISC-V Data Transfer (10)

Garcia, Kao

11 of 20

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

12 of 20

Load from Memory to Register

  • C code

int A[100];

g = h + A[3];

  • Using Load Word (lw) in RISC-V:

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

13 of 20

Store from Register to Memory

  • C code

int A[100];

A[10] = h + A[3];

  • Using Store Word (sw) in RISC-V:

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

14 of 20

Loading and Storing Bytes

  • In addition to word data transfers �(lw, sw), RISC-V has byte data transfers:
    • load byte: lb
    • store byte: sb
  • Same format as lw, sw
  • E.g., lb x10,3(x11)
    • contents of memory location with address = sum of “3” + contents of register x11 is copied to the low byte position of register x10.

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

15 of 20

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

16 of 20

08 RISC-V Data Transfer (16)

Garcia, Kao

17 of 20

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

18 of 20

08 RISC-V Data Transfer (18)

Garcia, Kao

19 of 20

And in Conclusion…

  • Memory is byte-addressable, but lw and sw access one word at a time.
  • A pointer (used by lw and sw) is just a memory address, we can add to it or subtract from it (using offset).
  • Big- vs Little Endian
    • Tip: draw lowest byte on the right
  • New Instructions:

lw, sw, lb, sb, lbu

08 RISC-V Data Transfer (19)

Garcia, Kao

20 of 20

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