Instructors
Dan Garcia and Bora Nikolic
inst.eecs.berkeley.edu/~cs61c �CS61C : Machine Structures�� Lecture 8 – Introduction to RISC-V�More Decisions; Logical; Procedures�
�
2018-09-10
Image source: Western Digital
Western Digital believes Big Data and Fast Data will need application-specific solutions and that RISC-V is ideal for delivering them (November 2017)
All Western Digital’s processor cores will be RISC-V �(>1 billion cores/year)!
Review
lw, sw, lb, sb, lbu, beq, bne, blt, bltu, bge, j
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Loops in C/Assembly
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
RISC-V Logical Instructions
Logical operations | C operators | Java operators | RISC-V instructions |
Bit-by-bit AND | & | & | and |
Bit-by-bit OR | | | | | or |
Bit-by-bit XOR | ^ | ^ | xor |
Shift left logical | << | << | sll |
Shift right logical | >> | >> | srl |
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
RISC-V Logical Instructions
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Your Turn. What is in x11?
xor x11,x10,x10
ori x11,x11,0xFF
andi x11,x11,0xF0
0x0 |
0xF |
0xF0 |
0xFF00 |
0xFFFFFFFF |
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
No NOT in RISC-V
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Logical Shifting
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Arithmetic Shifting
1111 1111 1111 1111 1111 1111 1110 0111two= -25ten
1111 1111 1111 1111 1111 1111 1111 1110two= -2ten
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Your Turn. What is in x12?
x10 holds 0x34FF
slli x12,x10,0x10
srli x12,x12,0x08
and x12,x12,x10
0x0 |
0x3400 |
0x4F0 |
0xFF00 |
0x34FF |
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Helpful RISC-V Assembler Features
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Administrivia
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Assembler to Machine Code (more later in course)
foo.S
bar.S
Assembler
Assembler
foo.o
bar.o
Linker
lib.o
a.out
Assembler source files (text)
Machine code object files
Pre-built object file libraries
Machine code executable file
Assembler converts human-readable assembly code to instruction bit patterns
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
How Program is Stored
Memory
Bytes
Program
Data
One RISC-V Instruction = 32 bits
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Program Execution
Processor
Control
Datapath
PC
Registers
Arithmetic & Logic Unit
(ALU)
Memory
Bytes
Instruction
Address
Read Instruction Bits
Program
Data
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
C Functions
main() {�int i,j,k,m;�...�i = mult(j,k); ... �m = mult(i,i); ...
}
/* really dumb mult function */
int mult (int mcand, int mlier){�int product = 0;�while (mlier > 0) {� product = product + mcand;� mlier = mlier -1; }�return product;�}
What information must�compiler/programmer �keep track of?
What instructions can
accomplish this?
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Six Fundamental Steps in Calling a Function
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
RISC-V Function Call Conventions
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Instruction Support for Functions (1/4)
... sum(a,b);... /* a,b:s0,s1 */� }� int sum(int x, int y) {� return x+y;� }
address (shown in decimal)� 1000 � 1004 � 1008 � 1012 � 1016 � …� 2000 � 2004
C
RISC-V
In RISC-V, all instructions are 4 bytes, and stored in memory just like data. So here we show the addresses of where the programs are stored.
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Instruction Support for Functions (2/4)
... sum(a,b);... /* a,b:s0,s1 */� }� int sum(int x, int y) {� return x+y;� }
address (shown in decimal)� 1000 mv a0,s0 # x = a� 1004 mv a1,s1 # y = b � 1008 addi ra,zero,1016 #ra=1016� 1012 j sum #jump to sum� 1016 … # next instruction� …� 2000 sum: add a0,a0,a1� 2004 jr ra # new instr.“jump register”
C
RISC-V
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Instruction Support for Functions (3/4)
... sum(a,b);... /* a,b:s0,s1 */� }� int sum(int x, int y) {� return x+y;� }
2000 sum: add a0,a0,a1� 2004 jr ra # new instr. “jump register”
C
RISC-V
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
Instruction Support for Functions (4/4)
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
RISC-V Function Call Instructions
(really should be laj “link and jump”)
jal FunctionLabel
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB
“And in Conclusion…”
CS61C L08 Introduction to RISC-V : Decisions, Procedures
Garcia, Nikolić, Fall 2018 © UCB