CS61C 2022fa L07-Intro-RISC-V
CS61C 2022fa L07-Intro-RISC-V
Great Ideas
UC Berkeley
in UC Berkeley
Teaching Computer Architecture Teaching
Professor (a.k.a. Machine Structures) Professor
Dan Garcia Lisa Yan
Intro to RISC-V
(California Gold)
Garcia, Yan
cs61c.org
07-Intro to RISC-V(1)
• Floating Point
Discussion
• The RISC-V Instruction
Floating Set Architecture
Point • Elements of
Architecture:
Discussion Registers
• Add/Sub Instructions
• Immediates Garcia, Yan
07-Intro to RISC-V(2)
Floating Point Fallacy
FP add associative? – 1.5 x
x
▫ x + (y + z) = –1.5x1038 + (1.5x1038 + 1.0) 1038
y 1.5 x 1038
= –1.5x1038 + (1.5x1038) = 0.0
▫ (x + y) + z = (–1.5x1038 + 1.5x1038) + 1.0 z 1.0
= (0.0) + 1.0 = 1.0
Therefore, Floating Point add is not associative!
▫ Why? FP result approximates real result!
▫ With bigger exponents, step size between floats gets bigger,
too!
▫ This example: 1.5 x 1038 is so much larger than 1.0 that 1.5
x 1038 + 1.0 in floating point representation rounds to 1.5 x
1038. Garcia, Yan
07-Intro to RISC-V(3)
Rounding Under the hood
Garcia, Yan
07-Intro to RISC-V(4)
IEEE FP’s Four Rounding Modes
Under the hood
07-Intro to RISC-V(5)
▪
FP Addition Under the hood
07-Intro to RISC-V(6)
Casting floats to ints and vice
versa
(int) floating_point_expression
Coerces and converts floating point to nearest
integer
(C uses truncation).
i = (int) (3.14159 * f);
(float) integer_expression
Converts integer to nearest floating point.
f = f + (float) i;
Garcia, Yan
07-Intro to RISC-V(7)
Double Casting Doesn’t Always
Work
int i = …;
if (i == (int)((float) i))
{
printf("true");
}
07-Intro to RISC-V(8)
Double Casting Doesn’t Always
Work
int i = …; float f = …;
if (i == (int)((float) i)) if (f == (float)((int) f))
{ {
printf("true"); printf("true");
} }
07-Intro to RISC-V(9)
Now you can make float jokes
too…
Saturday Morning
Breakfast
Comics
Side www.smbc-comics.com/comic/2013-06-05
note: the robot is using IEEE 754 double-precision.
(double) 0.3:
0.29999999999999999
(double) 0.1 + (double) 0.2:
0.30000000000000004 Garcia, Yan
07-Intro to RISC-V(10)
(change of gears)
Garcia, Yan
07-Intro to RISC-V(11)
• Floating Point
Discussion
The RISC-V • The RISC-V Instruction
Instruction Set Architecture
Set • Elements of
Architecture:
Architectur Registers
e • Add/Sub Instructions
• Immediates Garcia, Yan
07-Intro to RISC-V(12)
Great Idea #1: Abstraction
(Levels of Representation/Interpretation)
High Level Language temp = v[k];
v[k] = v[k+1];
Program (e.g., C) v[k+1] = temp;
Compiler Anything can be
lw x3, 0(x10)
Assembly Language lw x4, 4(x10) represented
Program (e.g., RISC-V) sw
sw
x4,
x3,
0(x10)
4(x10)
as a number,
i.e., data or instructions
Assembler 1000 1101 1110 0010 0000 0000 0000 0000
Machine Language 1000 1110 0001 0000 0000 0000 0000 0100
Program (RISC-V) 1010 1110 0001 0010 0000 0000 0000 0000
1010 1101 1110 0010 0000 0000 0000 0100
Reg[rs1]
1
A
al
u
pc+4
Architecture Implementation
m
W M
dr A
ta p.
B B
Garcia, Yan
07-Intro to RISC-V(13)
Assembly Language
Basic job of a CPU: execute lots of instructions.
Instructions are the primitive operations that
the CPU may execute.
▫ Like a sentence: operations (verbs) applied to operands
(objects), processed in sequence …
Different CPUs implement different sets of
instructions. The set of instructions a
particular CPU implements is an Instruction Set
Architecture (ISA).
▫ Examples: ARM (cell phones), Intel x86 (i9, i7, i5, i3),
IBM/Motorola PowerPC (old Macs), MIPS, RISC-V, ...
Garcia, Yan
07-Intro to RISC-V(14)
Book: Programming From the
Ground Up
“A new book was just released which is based on a new
concept – teaching computer science through assembly
language (Linux x86 assembly language, to be exact). This
book teaches how the machine itself operates, rather than
just the language. I've found that the key difference between
mediocre and excellent programmers is whether or not they
know assembly language. Those that do tend to understand
computers themselves at a much deeper level. Although
[almost!] unheard of today, this concept isn't really all that
new -- there used to not be much choice in years past. Apple
computers came with only BASIC and assembly language,
and there were books available on assembly language for
kids. This is why the old-timers are often viewed as 'wizards':
they had to know assembly language programming.”
-- slashdot.org comment, 2004-02-05
Garcia, Yan
07-Intro to RISC-V(15)
Instruction Set Architectures
Early trend was to add more and more instructions to
new CPUs to do elaborate operations.
▫ VAX architecture had an instruction to multiply polynomials!
Reduced Instruction Set Computer (RISC) philosophy:
▫ Cocke IBM, Patterson, Hennessy, 1980s.
▫ Keep the instruction set small and simple, makes it easier to build
fast hardware.
▫ Let software do complicated operations by composing simpler
ones.
▫ This went against the conventional wisdom of the time.
(he who laughs last, laughs best)
Garcia, Yan
07-Intro to RISC-V(16)
Patterson and Hennessy win
Turing!
07-Intro to RISC-V(17)
RISC-V Architecture IBM 360 Green Card
07-Intro to RISC-V(18)
RISC-V Origins
Started in Summer 2010 to support
open research and teaching at
UC Berkeley
▫ Lineage can be traced to RISC-I/II projects (1980s) RISC-I
As the project matured, it migrated
to RISC-V foundation (www.risc-v.org)
Many commercial and research projects
RISC-II
based on RISC-V.
▫ Open-source and proprietary
▫ Widely used in education
Read more:
▫ https://riscv.org/risc-v-history/
▫ https://riscv.org/risc-v-genealogy/ Garcia, Yan
07-Intro to RISC-V(19)
• Floating Point
Discussion
Elements • The RISC-V Instruction
of Set Architecture
Architectur • Elements of
Architecture:
e: Registers
Registers • Add/Sub Instructions
• Immediates Garcia, Yan
07-Intro to RISC-V(20)
Instruction Set
“instruction sets”
07-Intro to RISC-V(21)
Higher-Level Language vs. Assembly Language (1/2)
C, Java RISC-V
In C (and most high-level Assembly operands are
languages), variables declared registers.
first and given a type. ▫ Registers are limited number of special
locations, built directly into the
▫ int fahr, celsius;
hardware.
char a, b, c, d, e;
▫ RISC-V: Operations can only be
Each variable can ONLY performed on register operands.
represent a value of the type it In assembly language, the
was declared as (cannot registers have no type.
mix/match int/char variables). ▫ Register contents are just bits.
07-Intro to RISC-V(23)
Great Idea #3: Principle of Locality / Memory Hierarchy
Storage
Latency
Analogy
Garcia, Yan
07-Intro to RISC-V(24)
32 Registers in RISC-V
Drawback: Each ISA a predetermined number of
registers.
▫ Why? Registers are built into hardware.
▫ Solution: RISC-V code must be very carefully put together to
efficiently use registers.
▪ Nowadays: compiler maps C variables to RISC-V registers across declarations,
function calls, etc.
32 registers in RISC-V.
▫ Why 32? Smaller is faster, but too small is bad.
▫ Goldilocks principle (“This porridge is too hot; This porridge is too
cold; this porridge is just right”)
Each RISC-V register is 32 bits wide (in RV32 variant).
▫ Groups of 32 bits called a word in RV32. Garcia, Yan
07-Intro to RISC-V(25)
Register Names and Numbers
Registers are numbered from 0 to 31.
▫ Referred to by number x0 – x31.
x0 is special, always holds value zero.
▫ So only 31 registers able to hold variable values.
Each register can be referred to by number or name.
▫ We’ll explain names next week.
Ok, enough already…gimme my RV32!!!
Garcia, Yan
07-Intro to RISC-V(26)
• Floating Point
Discussion
• The RISC-V Instruction
Add/Sub Set Architecture
Instruction • Elements of
Architecture:
s Registers
• Add/Sub Instructions
• Immediates Garcia, Yan
07-Intro to RISC-V(27)
Higher-Level Language vs. Assembly Language (2/2)
C, Java RISC-V
Each line can contain Each statement is an instruction.
▫ An instruction executes exactly one
multiple operations. short list of simple commands.
a = b * 2 - (arr[2] + *p); Each line of assembly code
Common operations are: contains at most 1 instruction.
▫ =, +, –, *, / add x1, x2, x3
sub x1, x1, x4
▫ Here, * includes both multiply
and dereference
The compiler translates from higher-
level language down to assembly…
…so RISC-V instructions are often
closely related to common C/Java
operations.
Garcia, Yan
07-Intro to RISC-V(28)
RISC-V Arithmetic Instruction
Syntax
opname rd, rs1, rs2
operatio ”destinatio ”source1” ”source2”
n, by n” 1st 2nd
name operand operand for operand for
getting operation operation
result
Syntax is rigid:
▫ 1 operator, 3 operands.
▫ Why? Keep hardware simple via regularity. (more in a few
weeks)
Garcia, Yan
07-Intro to RISC-V(29)
RISC-V Addition and Subtraction
opname rd, rs1, rs2
operatio ”destinatio ”source1” ”source2”
n, by n” 1st 2nd
name operand operand for operand for
getting operation operation
Addition
result Subtraction
RISC-V add x1,x2,x3 RISC-V sub x4,x5,x6
(remember!
C a = b + c; C d = e – f; order of
operands
matter in sub)
Garcia, Yan
07-Intro to RISC-V(30)
RISC-V Arithmetic, Example 1
How could we translate this C statement?
a = b + c + d - e;
x10 x1 x2 x3 x4
▫ Assume the above mapping for C variables ↔︎RISC-V registers.
07-Intro to RISC-V(32)
Aside: Apollo Guidance Computer
Margaret Hamilton
Director of Software Engineering
Division of MIT Instrumentation
Laboratory
▫ Built Apollo Guidance Computer
Command Module (navigation, lunar landing)
▪ 72KB of memory on board Eagle (Apollo 11, 1969)
Margaret Hamilton
and Apollo code
Wikimedia Commons
Presidenti
al Medal
of
Freedom
ignition file (2016)
https://abcnews.go.com/Technology/apollo-11s-source-code-tons-easter-eggs-i Garcia, Yan
ncluding/story?id=40515222
07-Intro to RISC-V(33)
• Floating Point
Discussion
• The RISC-V Instruction
Set Architecture
Immediate • Elements of
s Architecture:
Registers
• Add/Sub Instructions
• Immediates Garcia, Yan
07-Intro to RISC-V(34)
Immediates
Immediates are numerical constants.
▫ They appear often in code; hence, they have separate instructions.
Add Immediate instruction:
RISC-V addi x3,x4,10 Syntax similar to add instruction. But
the
C f = g + 10; last operand must be a number, not a
register.
Garcia, Yan
07-Intro to RISC-V(35)
Immediates
Immediates are numerical constants.
▫ They appear often in code; hence, they have separate instructions.
Add Immediate instruction:
RISC-V addi x3,x4,10 Syntax similar to add instruction. But
the
C f = g + 10; last operand must be a number, not a
register.
There is no Subtract Immediate instruction in RISC-V.
Why?
RISC Philosophy: Reduce the possible
▫ While there are add
RISC-V addi x3,x4,-10 and sub instructions, to subtract an immediate,
types of operations to an absolute
just use addi:
minimum.
C f = g - 10; If an operation can be decomposed
into a simpler operation, don’t include
it in the ISA. Garcia, Yan
07-Intro to RISC-V(36)
Register Zero
One particular immediate, the number zero (0),
appears very often in code.
RISC-V hardwires the register zero (x0) to value 0:
RISC-V add x3,x4,x0
C f = g;
C f = 0xff;
Defined in hardware, so an instruction add
x0,x3,x4
will not do anything! Garcia, Yan
07-Intro to RISC-V(37)
Concept Check: True or False?
1. Types are associated with declaration in C
(normally), but are associated with instructions
(operators) in RISC-V.
2. Since there are only 32 registers, we can’t write
RISC-V for C expressions that contain > 32
variables. 123
A. FFF
3. If p (stored in x9) were a pointer to an array of B. FFT
ints, C. FTF
D. FTT
then p++; would be E. TFF
addi x9 x9 1 F. TFT
G. TTF
H. TTT
Garcia, Yan
07-Intro to RISC-V(38)
Concept Check: True or False?
1. Types are associated with declaration in C
(normally), but are associated with instructions
(operators) in RISC-V.
2. Since there are only 32 registers, we can’t write
RISC-V for C expressions that contain > 32
variables. 123
A. FFF
3. If p (stored in x9) were a pointer to an array of B. FFT
ints, C. FTF
D. FTT
then
1. True, we p++; would
saw that on an be
earlier slide. E. TFF
addi
2. False. wex9
sawx9how1 to break up longer equations to smaller F. TFT
ones already. G. TTF
H. TTT
3. False. Don’t forget that ints are (usually) 4 bytes wide, so
instruction would be addi x9, x9, 4. Garcia, Yan
07-Intro to RISC-V(39)