2018fa CS61C L07 DG BN RV Lwswdecisions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

inst.eecs.berkeley.

edu/~cs61c
CS61C : Machine Structures
Lecture 7 – Introduction to RISC-V
Assembly language : lw, sw, decisions
Instructors
Dan Garcia and Bora Nikolic
2018-09-07
Fun video game
teaches assembly code!
The game “human resource
machine” takes players
through very simple
challenges through sorting
of zero-terminated strings!
My 9 & 12-year-old kids
loved it!
tomorrowcorporation.com/humanresourcemachine
Review
• In RISC-V Assembly Language:
– Registers replace C variables
– One instruction (simple operation) per line
– Simpler is Better, Smaller is Faster
• In RV32, words are 32b
• Instructions:
– add, addi, sub
• Registers:
– 32 registers, referred to as x0 – x31
– Zero: x0
Data Transfer:
Load from and Store to memory
Processor Memory Input
Enable?
Read/Write
Control
Program
Datapath Address Much larger place
PC
Write Data = Bytes To hold values, but
Registers Store to slower than registers!
memory
Arithmetic & Logic Unit Read Data = Data
(ALU) Output
Load from
memory
Fast but limited place
To hold values Processor-Memory Interface I/O-Memory Interfaces
3
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 Least-significant byte in a word
(1 word = 4 bytes)
… … … … …
• Memory addresses are really 15 143 13 12
in bytes, not words 11 102 9 8
• Word addresses are 4 bytes apart 7 61 5 4
– Word address is same as address of 3 20 1 0
rightmost byte – least-significant byte 31 24 23 16 15 8 7 0
Least-significant byte
(i.e. Little-endian convention)
gets the smallest address
4
en.wikipedia.org/wiki/Big_endian
Big Endian vs. Little Endian
Big-endian and little-endian derive from Jonathan Swift's Gulliver's Travels in which the Big Endians were
a political faction that broke their eggs at the large end ("the primitive way") and rebelled against the
Lilliputian King who required his subjects (the Little Endians) to break their eggs at the small end.
• The order in which BYTES are stored in memory
• Bits always stored as usual. (E.g., 0xC2=0b 1100 0010)
Consider the number 1025 as we normally write it:
BYTE3 BYTE2 BYTE1 BYTE0
00000000 00000000 00000100 00000001

Big Endian Little Endian


ADDR3 ADDR2 ADDR1 ADDR0 ADDR3 ADDR2 ADDR1 ADDR0
BYTE0 BYTE1 BYTE2 BYTE3 BYTE3 BYTE2 BYTE1 BYTE0
00000001 00000100 00000000 00000000 00000000 00000000 00000100 00000001

Examples Examples
Names in China (e.g., Garcia Dan) Names in the US (e.g., Dan Garcia)
Java Packages: (e.g., org.mypackage.HelloWorld) Internet names (e.g., www.cs.berkeley.edu)
Dates done correctly ISO 8601 YYYY-MM-DD (e.g., 2018-09-07) Dates written in England DD/MM/YYYY (e.g., 07/09/2018)
Eating Pizza crust first Eating Pizza skinny part first (the normal way)
Unix file structure (e.g., /usr/local/bin/python)
Great Idea #3: Principle of Locality /
Memory Hierarchy
Speed of Registers vs. Memory
• Given that
– Registers: 32 words (128 Bytes)
– Memory (DRAM): Billions of bytes (2 GB to 64 GB on laptop)
• and physics dictates…
– Smaller is faster
• How much faster are registers than DRAM??
– About 100-500 times faster! (in terms of latency of one access)

7
Jim Gray’s Storage Latency Analogy:
How Far Away is the Data?
Andromeda
10 9 Tape /Optical The
The
image h
T

e
2,000 Years
Robot
The image part i
with
T imag part
The h e with relati m
onshi a
The imag e part relati g
with onshi p ID
T imag e i
rId3 e
Th h e part m relati p ID p
rId3 was
T e e part with a onsh not a
Th h ima im with relat g ip ID was r
not found
T e e ge a relat ions e rId3 in the t
T h ima i par g ions hip p was found w
ID ar not in the file.
T h e ge m t e hip i
rId3 t foun file.
T h e i par a wit p ID t
T h e im m t g h ar rId3 was wi d in h
h e i a a wit e rela t was not th the r
e i mg g h p tio wi not foun re file. e
i m a e e rela ar nsh th foun d in la l
m a g p p tio t ip re d in the ti a
a g e ar a nsh wi ID la the file. o t
g e p t r ip th rId ti file. n i
e p a wi t ID re 3 o s o
p a r th w rId la wa ns hi n
a r t re i 3 ti s hi p s
T r t w la t wa o not p ID h
h t w i ti h s n fou ID rI i
e wTi Tt o r not s nd rI d p
i h t h ns e fou hi in d 3 I
i h T

6
m t e h e r hhiT lT nd p the 3 w D
i
a h r i e eph h a inThe ID file. w a r

Pluto
g r me ml i IDe e t the imagrI as s I

Disk
e a rI file. Th n d

2 Years
e l a i i d n
g a m im e

10
p l a g t ad m o a part 3 e T ot ot 3
a ae t e i g3a g n withw ima fo
h
fo w
p w T
r t i po e g e s a ge u u a
i a o n ase h relatis par en h n s
t a p p onsh i e
w or n r s an p iar ip IDn t m im
d d n
i nt s t h r ota tp rId3ot wit ain in o
w fo a
t s h wi t r wi I was fo h g g
th th t
h hi i i pwu t D th not
u rela
e e
e e f
t
r i pt I i n w re r founn tio
p fil
p
fil o
e ph I h Dt di la I d in d nsh ae.ar e. u
r
l I Dr r h t ti in d the in ip T n
e r t
a D r e I r thh 3 th ID
o file. rId t Twie
h d
t r l I l dee r ns w e i

Jim Gray
w h
i I a da 3l file hi a fil 3Th i thi n
t e. wae e re
o d 3t wa l p s e. t
m t
n 3i wi a t a n The s ima i laa h
o ID ge hmtig
s w a o s i t The o
rI imag not r a oe
e
par
h an s n n o i imag t
d
e fou
egnsp
f
s s o o f part ndt i
i n The
h s t n neo 3 le
with inwit a hia
p n o imaghf s w
part ppr
l
TheI oi t e s asu relat theh e
image i h with t
rela IDt a The
D t pf part oi h n
n ions file. i r rI
.
T partr f I o p u relat
d hip tio w The image
Th h withI oDu relat I n p i ions
with ot nsh ot di image part
I p i ID The

Turing Award
D fo nw with
Th e e relations
d ur n ions dD I hip n rId3 ip s 3t image
part
r IDu i w The relation
T e ima im hip ID I
3 n dhip ir D t was ID h h with
T h ima ge a rId3wwas ddi IIDn I r rId3 n rId t asr The image part relations ship ID
h not with
T h e ge g not a i 3n rId3 dt d
was 3 i hne Th image part hip ID rId3
par d I e foun relation
e 3h in
not pr ot with rId3 was was
T h i par t e found s innwt was 3 d thf d in wa l T e part
ship ID
i mt p the file. w foun s I e fo with relati not
h e wit n t a ha e 3 i
not the a Th h ima not
e i m a wit h ar h s e f w wde l in file. not Dl u tT e e ge relatio onshi rId3 found in found
o foun
s i a the fil r a n ima was
i m a g h rela t t en f d in a e fou T ih i par nship p ID the file. in the
n s e. nd Iht do rId3 not
m a g e rel tio wi f f o i the l n s file.
. e ge m t ID
found
file.
e o d i in was
a g p ati nsh th t
o i l tfile.eo n in e n i par a wit rId3
in the
g e p a on ip re l f . o the 3 i o ths t
m g h was not
u ef t found file.
e p ar r shi ID la n eo . o f t file. w
mn e h a wit e rela not

B.S. Cal 1966


a
as in the
p a t t p rId ti d .u u o f Tfilgi h p tio found
r wi w ID 3 o n s
gh e h e. Th
p rel ar nsh in the file.
a i o
r t th i rId wa ns d n u u n
eie Ipe ati Tt ip file.
n d n o
t w re t 3 s hi t i i d n pp im a Dima on h wiThID
la h wa p tarI r ge shi e the rIdT
w
T i not h n n i d a
ft D par h T
i t ti r s fou ID t g I p i ima3
h T o
e t n i or t t ID regewae h T T T
te h e not nd rI f h h t n wi e d w mla e h h Th
h n uI rId a tipars i
ih r T l fou in d e t th p 3 i wit h
e s
i
f e h h n
red m im e e e e T
r e h a nd the 3 ar wh 3 go t not ima h T
m il hi T
l f e d3 t rel a a
i i
e
a e t in file. w e i i f e la t a h wa e n witfou g m m i ge e h
m
a p h
i the as l i w ati
ti s ps h nd g m e
lg i wi s
r
T . i f e e a a a par i i

Sacramento
a ID e e l na on
o file e not a hirelin

Ph.D. Cal 1969


a t m n o th n g g m

1.5 hr
e g h T e l i t s p p t
t i rI
a im
n .e ot . l nre o l shi fou r atithe g a m
p e
o d a
s i
h Thfo . e hn pnd t ponfile.ar ar e e e wit
s
i
a p g e e u . e la t
eo a IDin ID t p p h g a
ro n 3
e g
h m The . hi ti ft wrIshi t wi ar p rela e g
a w e im iman ft wi a a
n s p i p o o i rId the i dp pTe
t rh a p a a ged imag Th iIDf th th t r r tio
s ar p g ns u 3file t 3ID nsh ah p
w ti g parin e l o o wa re wi t
ih
s
t ar
I e rI hi n . h wrId re la th t ip T r a
tD e e t th part ima The e

100 Memory
w n e
du la w Thw ID h t r
ti p wi p d ss r a3
iI ot wi p p wite with ge imag .3n not ti ti re i e i rIde w t
i
p th r The ID ih e s wa o la t ima
h tD fo th
ar ar h fil relat par e d s o im m
rI re I part imag The wrI n i fou l n ns ti h ge t 3 i aw
h
r u re
d wi
t t rele. ions t e aid tpnd a otnot n h waa t i
D
e la wi ati hip s hi o r par
rI n la
3 th wit with part image The sn3 h in t fofou r s g h t
g
lr e ti th on ID relat I p n
e eh
d d
o ti
w re h with part ntw e Dthe i u nd
hi Te te
Ia l3 in o re shi rId3 rel ions with imag h The file in p ID s l Twitnotp r p
td n a la was relat e otas fr on hi h a l Th e r
a
w th ns la p ati hip ions relati part foe
n image
iI . n the ID rI h h fouar a
i3 t s s ti
ti ID not ID onshi f part s dfile The rI d p e t e a end t Tl r e
a e hi
n o on hip uot i rel
t in h a T
w
o is
hi o rId foun shi rId3 p ID with ld inimage d T 3 ID ima t l The
fil p nifo with . mi i ati wie t h
a
n o p o n ns 3 d in was ID relat e
3 h thpart w rI o i ge the a
w imag Th
n e.
ID ID
t p rId3 rId3 ions dlu relation
.w i e with 3 h as d a mon o file.thi i e t The
s n rI s hi wa the not was e w e par re i
n a shi e e
n
h o
s rI f hi p s file. ID foun was not hipinn ship a IDpfil
relati a n 3 gs n t lamot i part ima image
The
d . T
io t
h d o p
ID not
rId
d in not ID thd rId3 s I e.onshi imot w e gp
h
s
wit a nh m o
with ge part
image
f 3 3
u ID 3 foun found rId3 ein was n D p ID s a fo a p e ID h ti n with part h
t
p io w rI fou wa the in the fil not n g a i p h o gs r a relat par
rId
i with e
n rI file. d in file. wasth o r rId3 s
If w ot e u s r pa 3 rel nse he g ions t relati

10
p as d nd s e.e found relati i
u d d the I was p h
e wit onshi

On Board Cache
o
D a not t n i
ati hip l i hip
I n 3 in not in the d fo n I r wa m
ru n
D
s i 3 w the
file. founfil f not u pd ot t D I on parpa p ID h p ID onshi
a
ot fou d in e. file. 3 found arin fo w t s p ID

This Campus
d n w D p
In ri n
fo as file nd
o
n t r wnotshi IDt I t a rId3 rel rId3 rId3 g
i r I

10 min
d ot t a the u w in the th u I wiDi r was ati was
I n . I p rIthr tD
i3 n fo u
h s in file. n a file. d wie n t i fou on not was e
d n ot in th d dID d o r not p
n
w t
3 u e n fo
the d s fil d h 3t nd rId reI n w foun shi found not
h d
f file n th re r h in3 3 I in the found ar
t n i 3 wlads i d in p
a w in ot u . e lae. in e wr the in the t
h
s e
a d i fo n n o
fil ti th a wwa asti3h d t the ID file.
f th
l t l e file
a 3
h file. wi
e
n s in u d t e o
s l s n i w file.w rId
e e. o th
f
o i th e n h f fil a .s na r 3
n in ns e. t n a n not ot s pe re

2 On Chip Cache
l fil
. o a
ti o e
e. d th e o t o fou fos I s wa la
e u hi i l
fl t fil in e f hinD s
e . e. th i n p o t i t nd u po a n not
ti
o fil r
.
f
e l d ID n f o f in n IDt to fou
o
u o e. the d I
n fil e i rI s ono rIf dit nd
n
u d h u s u file indo o s
d e. . n f in
n i nhn
. th 3o hi

This Room
t 3 3 u n the
i d e wu
n h w pdi d wna s file
p
i e as I i p i fil n ID
d h .
t n
f n Dn I n e.a s id rI
h t i
s n
e i ot r t Dt n no p i d
h l fo I hr h 3
f e e I ott t In w
e u d e fohf D t
i f
. n 3f d f e
u o h
r
a
l i d wi 3i n f u Ie
s

1 (ns) Registers My Head 1 min


e l
.
in a l wl di n d f n
e th s ea e i ot
. inl d3
e n. s . thei w l fo
fil o n e u
e. na n
e. t o fil t s.
f t d
e. h n in
o f
eo th
u o t
f e
n u
if fil
d n
lo e.
i d
eu
n i n
.
t n d
h t i
e h n
f e t
i f h
l i e
e l f
. e i
.
Load from Memory to Register
• C code
int A[100];
g = h + A[3];

Data flow

• 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
9
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]

Data flow

Note: x15 – base register (pointer)


12,40 – offsets in bytes
x15+12 and x15+40 must be multiples of 4
10
Loading and Storing Bytes
• In addition to word data transfers
(lw, sw), RISC-V has byte data transfers: n s i gn e d
o h a s “ u ro
– load byte: lb a l s c h ze
RISC-V s ( l b u ) w h i
no
– store byte: sb l o a d W hy
byte” f i l l re g i st e r.
• Same format as lw, sw te n d s to b u ?
ex o r e b y t e s
g n e d st
• E.g., lb x10,3(x11) unsi
– contents of memory location with address = sum of “3” + contents of
register x11 is copied to the low byte position of register x10.
xxxx xxxx xxxx xxxx xxxx xxxx xzzz zzzz
x10 byte
…is copied to “sign-extend”
: This bit
loaded
11
Your turn. What’s in x12?
addi x11,x0,0x3F5 0x0
sw x11,0(x5) 0x3
lb x12,1(x5) 0x5
0xF
0xFFFFFFFF

12
Computer Decision Making
• Based on computation, do something different
• In programming languages: if-statement

• RISC-V: if-statement instruction is


beq register1,register2,L1
means: go to statement labeled L1
if (value in register1) == (value in register2)
….otherwise, go to next statement
• beq stands for branch if equal
• Other instruction: bne for branch if not equal

13
Types of Branches
• Branch – change of control flow

• Conditional Branch – change control flow depending on


outcome of comparison
– branch if equal (beq) or branch if not equal (bne)
– Also branch if less than (blt) and branch if greater than or equal
(bge)

• Unconditional Branch – always branch


– a RISC-V instruction for this: jump (j), as in j label

14
Example if Statement
• Assuming translations below, compile if block
f → x10 g → x11 h → x12
i → x13 j → x14

if (i == j) bne x13,x14,Exit
f = g + h; add x10,x11,x12
Exit:
• May need to negate branch condition
15
Example if-else Statement
• Assuming translations below, compile
f → x10 g → x11 h → x12
i → x13 j → x14
if (i == j) bne x13,x14,Else
f = g + h; add x10,x11,x12
else j Exit
f = g – h; Else: sub x10,x11,x12
Exit:
16
Magnitude Compares in RISC-
V
• Until now, we’ve only tested equalities (== and != in C);
General programs need to test < and > as well.
• RISC-V magnitude-compare branches:
“Branch on Less Than”
Syntax: blt reg1,reg2, label
Meaning: if (reg1 < reg2) // treat registers as signed integers
goto label;
• “Branch on Less Than Unsigned”
Syntax: bltu reg1,reg2, label
Meaning: if (reg1 < reg2) // treat registers as unsigned integers
goto label;

17
C Loop Mapped to RISC-V Assembly
int A[20]; add x9, x8, x0 # x9=&A[0]
int sum = 0; add x10, x0, x0 # sum=0
for (int i=0; i < 20; i++) add x11, x0, x0 # i=0
sum += A[i]; addi x13,x0, 20 # x13=20
Loop:
bge x11,x13,Done
lw x12, 0(x9) # x12=A[i]
add x10,x10,x12 # sum+=
addi x9, x9,4 # &A[i+1]
addi x11,x11,1 # i++
j Loop
Done:
18
Peer Instruction
We want to translate *x = *y into RISC-V 1
x, y ptrs stored in: x3 x5 2
3
1: add x3, x5, zero 4
2: add x5, x3, zero 5® 6
3: lw x3, 0(x5) 6® 5
4: lw x5, 0(x3)
5: lw x8, 0(x5) 7® 8
6: sw x8, 0(x3)
7: lw x5, 0(x8)
8: sw x3, 0(x8)

19
“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).
• A Decision allows us to decide what to execute at run-time rather than
compile-time.
• C Decisions are made using conditional statements within if, while,
do while, for.
• RISC-V Decision making instructions are the conditional branches: beq
and bne.
• New Instructions:
lw, sw, lb, sb, lbu, beq, bne, blt, bltu, bge, j

You might also like