Introduction To Assembly Language and RISC-V Instruction Set Architecture
Introduction To Assembly Language and RISC-V Instruction Set Architecture
Introduction To Assembly Language and RISC-V Instruction Set Architecture
1
Outline
• Assembly Language
• RISC-V Architecture
• RISC-V Instructions
• C-to-RISC-V Patterns
• And in Conclusion …
2
Outline
• Assembly Language
• RISC-V Architecture
• RISC-V Instructions
• C-to-RISC-V Patterns
• And in Conclusion …
3
Levels of Representation/Interpretation
Machine
Interpretation
Architecture
Implementation
• Each instruction does a small amount of work (a tiny part of a larger program).
• Examples: ARM, Intel x86, MIPS, RISC-V, IBM/Motorola PowerPC (old Mac), Intel IA64, ...
5
Assembly Language Programming ARM
• Back in the day, when ISAs where complex and compilers where
immature …. hand optimized assembly code could beat what the
compiler could generate.
• CS164: Compilers
• CS162: O/S
• OS often needs a small amount of assembly for doing things the "high level"
language doesn't support
• CS161: Security
• Exploit code ("shell code") is often in assembly and exploitation often requires
understanding the assembly language of the target.
7
RISC-V
Green Card
Computer Science 61C Spring 2019 Weaver
1/26/18
http://inst.eecs.berkeley.edu/~cs61c/resources/RISCV_Green_Sheet.pdf 8
Inspired by the IBM 360
“Green Card”
Computer Science 61C Spring 2019 Weaver
9
Outline
• Assembly Language
• RISC-V Architecture
• RISC-V Instructions
• C-to-RISC-V Patterns
• And in Conclusion …
10
What is RISC-V?
Platinum:
Rumble
12
Outline
• Assembly Language
• RISC-V Architecture
• RISC-V Instructions
• C-to-RISC-V Patterns
• And in Conclusion …
13
Assembly Variables: Registers
• More primitive, instead what simple CPU hardware can directly support
• Limited number of special places to hold values, built directly into the hardware
• Benefit:
• Since registers are directly in hardware, they are very fast to access
14
Registers live inside the Processor
Processor Memory
Enable?
Input
Control Read/Write
Data
Datapath
Address
PC Bytes
Register Write
Data
Arithmetic & Logic Unit
s
(ALU)
Read
Data
Program Output
• Given that
• Smaller is faster
16
Number of RISC-V Registers
• P&H CoD textbook uses the 64-bit variant RV64 (explain differences later)
• Example:
int fahr, celsius;
char a, b, c, d, e;
• Each variable can ONLY represent a value of the type it was declared (e.g., cannot
mix and match int and char variables)
• If types are not declared, the object carries around the type with it. EG in python:
a = "fubar" # now a is a string
a = 121 # now a is an integer
• In Assembly Language:
• But it is very very bad if you don't make sure they are...
• In fact, a RISC-V processor may natively only support aligned accesses, and do
unaligned-access in software!
An unaligned load could take hundreds of times longer!
19
RISC-V Instructions
• Must be word aligned, or half-word aligned if the 16b optional (C) instruction set is also
enabled
20
Outline
• Assembly Language
• RISC-V Architecture
• RISC-V Instructions
• C-to-RISC-V Patterns
• And in Conclusion …
21
RISC-V Instruction Assembly Syntax
22
Addition and Subtraction of Integers
Computer Science 61C Spring 2019 Weaver
• Addition in Assembly
a x1, b x2, c x3
• Subtraction in Assembly
• Why?
You may need to replace code later: No-ops can fill space, align data, and perform
other options
• add x0 x0 x0
• Why?
• Making a "standard" no-op improves the disassembler and can potentially improve the
processor
a = b + c + d - e;
f = g - 10 (in C)
27
Data Transfer:
Load from and Store to memory Weaver
Computer Science 61C Spring 2019
Processor Memory
Enable?
Input
Read/Write
Control
Data
Datapath Address Much larger place
7 6 5 4
• Word addresses are 4 bytes
3 2 1 0
apart
31 24 23 16 15 8 7 0
• Word address is same as address of
rightmost byte – least-significant byte
Least-significant byte
(i.e. Little-endian convention) gets the smallest address
29
Transfer from Memory to Register
• C code
int A[100];
g = h + A[3];
• C code
int A[100];
A[10] = h + A[3];
33
Your turn - clickers
34
Your turn - clickers
35
Your turn - clickers
36
Administrivia
• When the new lab starts, all those in the room from the previous lab have to
leave
• Link on Piazza
37
RISC-V Logical Instructions
C
Java
Logical operations operators 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 >> >> srl/sra
38
Logical Shifting
• Store in x11 the value from x12 shifted 2 bits to the left (they fall
off end), inserting 0’s on right; << in C
beq register1,register2,L1
• Also branch if less than (blt) and branch if greater than or equal (bge)
42
Outline
• Assembly Language
• RISC-V Architecture
• RISC-V Instructions
• C-to-RISC-V Patterns
• And in Conclusion …
43
Example if Statement
i → x13 j → x14
if (i == j) bne x13,x14,done
f = g + h; add x10,x11,x12
done:
44
Example if-else Statement
if (i == j) bne x13,x14,else
f = g + h; add x10,x11,x12
else j done
f = g – h; else: sub x10,x11,x12
done:
45
Magnitude Compares in RISC-V
47
C Loop Mapped to RISC-V Assembly
Computer Science 61C Spring 2019 Weaver
• Why?
• Getting the wrong answer fast is not what we want from you...
50
Outline
• Assembly Language
• RISC-V Architecture
• RISC-V Instructions
• C-to-RISC-V Patterns
• And in Conclusion …
51
In Conclusion,…
52