SlideShare a Scribd company logo
1
Processor Design
The Language of Bits
Prof. Smruti Ranjan Sarangi
IIT Delhi
Basic Computer Architecture
Chapter 6: RISC-V Assembly Language
(c) Smruti R. Sarangi, 2024 2
Download the pdf of the book
www.basiccomparch.com
videos
Slides, software, solution manual
Print version
(Publisher: WhiteFalcon, 2021)
Available on e-commerce sites.
The pdf version of the book and
all the learning resources can be
freely downloaded from the
website: www.basiccomparch.com
2nd
version
3
Outline
* RISC-V Machine Model
* Integer Instructions
* Control Transfer and Memory Instructions
* Floating Point Instructions
* Instruction Encoding
Code snippets beautified
with carbon.app
(https://carbon.now.sh)
4
Requirements of a Modern ISA
Compatible with all kinds of devices: 32, 64, 128 bits
No licensing requirement
Large software developer community
Extensibility: Possible to add all kinds of novel
extensions: vector, crypto, AI/ML, etc.
Why did the need
for RISC-V arise?
Project begins in Berkeley
2010
Creative Commons License
2015
Maintained by the RISC-V
Foundation
(c) Smruti R. Sarangi, 2024 5
RISC-V Machine Model
Name Description
RV32 32-bit ISA
RV32E 32-bit ISA (embedded version)
RV64 64-bit ISA
RV64E 64-bit ISA (embedded version)
RV128 128-bit ISA
Mnemonic Meaning
E Embedded version
I Base integer ISA
M Integer multiplication/division
instructions
A Atomic instructions
F Single-precision floating point
D Double-precision floating point
V Vector Instructions
Different variants of the
instruction set
Extensions
(c) Smruti R. Sarangi, 2024 6
More about Extensions
• Integer variants of the ISA
• RV32I is a regular 32-bit ISA with integer instructions
• RV64I, RV128I
• RV32IMA  integer, mul/div, atomic instructions
• RV32I1p3
• Major version number: 1
• Minor version number: 3
• G  base integer ISA, additional integer insts, FP instructions, basic
synch. instructions
• Popular format: RV32G
(c) Smruti R. Sarangi, 2024 7
Embedded and Compressed Instructions
• We quickly run out of letters !!!
• The `Z’ series was introduced
• Zfa  Additional floating-point instructions
• Embedded version
• Does not reduce the instruction width (still 32 bits)
• Reduces the number of registers by 50%
• Compressed version
• Width = 16 bits
• Access only 8 registers
• Limited opcode support, smaller immediate values
Even in 64 and 128-bit formats,
instructions are 32 bits.
48 and 64-bit
instructions are getting
ratified
(c) Smruti R. Sarangi, 2024 8
RISC-V Integer Registers
Register Mnemonic Description
x0 zero Hardwired to zero
x1 ra Return address
x2 sp Stack pointer
x3 gp Global pointer
x4 tp Thread pointer (thread-
local storage
x5-7 t0-2 Temporary registers
x8 s0/fp Saved register/frame
pointer
Register Mnemonic Description
x9 s1 Saved register
x10-11 a0-1 Function args/
return values
x12-17 a2-7 Function args
x18-27 s2-11 Saved registers
x28-31 t3-6 Temporary registers
x0 is hardwired to 0
There is a dedicated pc register
Bi-endian
(c) Smruti R. Sarangi, 2024 9
x0/zero
x1
x2
x3
x4
x5
x6
x7
x8
x9
RV32
RV64
RV128
x31
0
32
64
127
PC
0
32
64
127
View of Registers
(c) Smruti R. Sarangi, 2024 10
Outline
* RISC-V Machine Model
* Integer Instructions
* Control Transfer and Memory Instructions
* Floating Point Instructions
* Instruction Encoding
11
Moving values to registers
Semantics Example Explanation
addi rd, rs1, imm addi x1, x0, 5 x1 ← 0 + 5
add rd, rs1, rs2 add x1, x0, x3 x1 ← 0 + x3
• Register x0/ zero is hardwired to 0
• Moving a value to a register is accomplished by adding it to zero
• Immediates are normally 12 bits in RISC-V
• The addi instruction adds an immediate to a register (could be zero)
• The regular add instruction can be used
• If the first source register is set to zero 
Moving a value from one register to another
• There is no subi instruction because the immediate is signed
(c) Smruti R. Sarangi, 2024
(c) Smruti R. Sarangi, 2024 12
Loading Immediates Directly
• The lui instruction can load a 20-bit immediate directly into the upper 20 bits.
• Note that other instructions only load the lower 12 bits
• The li assembler directive can load a 32-bit value directly
• Translated to two instructions (internally)
• We can either use the x-series registers or the mnemonics
• The mnemonics are normally preferred because it is possible to honor the register-
usage semantics.
Semantics Example Explanation
lui rd, imm (20-bit) lui x1, 5 x1 ← 5 << 12
li rd, imm (32-bit) li t0, 0xABCD1234 t0 ← 0xABCD1234
(c) Smruti R. Sarangi, 2024 13
Add 409932 + 409823.
(c) Smruti R. Sarangi, 2024 14
• Using assembler directives like li makes writing assembly code easier
Add 409932 + 409823.
(c) Smruti R. Sarangi, 2024 15
Add/Sub Instructions
Semantics Example Explanation
add rd, rs1, rs2 add x1, x2, x3 x1 ← x2 + x3
addi rd, rs1, imm addi x1, x2, 5 x1 ← x2 + 5
sub rd, rs1, rs2 sub x1, x2, x3 x1 ← x2 - x3
(c) Smruti R. Sarangi, 2024 16
Compute 4 + 5 - 19.
(c) Smruti R. Sarangi, 2024 17
Swap two numbers stored in x1 and x2 (resp.).
(c) Smruti R. Sarangi, 2024 18
Multiplication and Division
• Part of the ‘M’ extension
• The default mul instruction computes the lower 32 bits
• The product ideally requires 64 bits
• The mulh and mulhu instructions can be used to compute the upper 32 bits
• The HW dynamically recognizes a consecutive mul-mulh instruction pair, and
replaces it with a single multiplication
Semantics Example Explanation
mul rd, rs1, rs2 mul x1, x2, x3 x1 ← x2 * x3
div rd, rs1, rs2 div x1, x2, x3 x1 ← x2 / x3
rem rd, rs1, rs2 rem x1, x2, x3 x1 ← rem (x2 / x3)
(c) Smruti R. Sarangi, 2024 19
Division Rules
Division Operation Quotient Remainder
4 ÷ 3 1 1
4 ÷ (-3) -1 1
(-4) ÷ 3 -1 -1
(-4) ÷ (-3) 1 -1
Round towards 0 Sign of the remainder is the same
as the sign of the dividend
Consecutive division and remainder instructions (with the same operands) can be fused.
(c) Smruti R. Sarangi, 2024 20
Multiply 3 with (-17).
(c) Smruti R. Sarangi, 2024 21
Compute 123
+ 1.
(c) Smruti R. Sarangi, 2024 22
Divide -50 / 3.
Dynamically fused by
hardware.
(c) Smruti R. Sarangi, 2024 23
Logical and Shift Instructions
Semantics Example Explanation
and rd, rs1, rs2 and x1, x2, x3 x1 ← x2 AND x3
andi rd, rs1, imm andi x1, x2, 6 x1 ← x2 AND 6
or rd, rs1, rs2 or x1, x2, x3 x1 ← x2 OR x3
ori rd, rs1, imm ori x1, x2, 9 x1 ← x2 OR 9
xor rd, rs1, rs2 xor x1, x2, x3 x1 ← x2 XOR x3
xori rd, rs1, imm xori x1, x2, 9 x1 ← x2 XOR 9
sll rd, rs1, rs2 sll x1, x2, x3 x1 ← x2 << x3
srl rd, rs1, rs2 srl x1, x2, x3 x1 ← x2 >> x3
sra rd, rs1, rs2 sra x1, x2, x3 x1 ← x2 >>> x3
slli rd, rs1, imm slli x1, x2, 3 x1 ← x2 << 3
srli rd, rs1, imm srli x1, x2, 3 x1 ← x2 >> 3
srai rd, rs1, imm srai x1, x2, 3 x1 ← x2 >>> 3
right-shift arithmetic
(c) Smruti R. Sarangi, 2024 24
Generic Format of Shift Instructions
Generic format
sll
srl
sra
slli
srli
srai
rd
rd
rd
rd
rd
rd
rs1
rs1
rs1
rs1
rs1
rs1
rs2
rs2
rs2
imm
imm
imm
,
,
,
,
,
, ,
,
,
,
,
,
,
,
,
,
,
,
(c) Smruti R. Sarangi, 2024 25
Compute the bitwise OR of A and B. Let A = 4 and B = 1.
(c) Smruti R. Sarangi, 2024 26
Compute t1 = t2 + t3 × 4
Strength
reduction
Multiplications are slow. Shifts are much faster.
(c) Smruti R. Sarangi, 2024 27
Outline
* RISC-V Machine Model
* Integer Instructions
* Control Transfer and Memory Instructions
* Floating Point Instructions
* Instruction Encoding
(c) Smruti R. Sarangi, 2024 28
Conditional Control Transfer Instructions
Semantics Example Explanation
beq rs1, rs2, label beq x1, x2, .foo Branch to the . foo label if x1 = x2
bne rs1, rs2, label bne x1, x2, .foo Branch to the .foo label if x1 ≠ x2
bge rs1, rs2, label bge x1, x2, .foo Branch to the .foo label if x1 ≥ x2
blt rs1, rs2, label blt x1, x2, .foo Branch to the .foo label if x1 < x2
bgeu rs1, rs2, label bgeu x1, x2, .foo Similar to bge, considers unsigned values.
bltu rs1, rs2, label bltu x1, x2, .foo Similar to blt, considers unsigned values.
Semantics Example Explanation
slt rd, rs1, rs2 slt x1, x2, x3 if (x2 < x3) set x1 to 1
slti rd, rs1, imm slti x1, x2, 5 if (x2 < 5) set x1 to 1
sltu rd, rs1, rs2 sltu x1, x2, 5 if (x2 <unsigned x3) set x1 to 1
sltui rd, rs1, imm sltui x1, x2, x3 if (x2 <unsigned 5) set x1 to 1
(c) Smruti R. Sarangi, 2024 29
Control Transfer Instructions
RISC-V does not have a flags register.
Instead, the flags are stored in a register that the programmer specifies.
Compare 2 and 5 and store the comparison
result in t2. [t2 = (2 < 5)]
(c) Smruti R. Sarangi, 2024 30
Add two 64-bit numbers (stored in two 32-bit registers)
Note: sltu does an unsigned comparison.
t0 < t4 → there
is a carry
(c) Smruti R. Sarangi, 2024 31
Add all the numbers from 1 to 10.
(c) Smruti R. Sarangi, 2024 32
Unconditional Branches
• The jal instruction performs the role of an unconditional branch and a call instruction:
• The return address is explicitly specified (there is no default)
• If the return addr. register is zero, then this is a regular unconditional branch instruction
• Otherwise, it is a call instruction
• jalr is like a return instruction
• In this case, the return address is stored in rd
• If the return addr. register is zero, then nothing is stored. Functions like an indirect branch.
Semantics Example Explanation
jal rd, label jal x1, func Jump to the func label and store the
return address in x1
jalr rd, rs1, offset jalr x1, x2, 20 Jump to the address x2 + 20 and
store the return address in x1
(c) Smruti R. Sarangi, 2024 33
Check if the number stored in a1 is a square or not?
(c) Smruti R. Sarangi, 2024 34
Check whether the number stored in a1 is a prime number.
(c) Smruti R. Sarangi, 2024 35
Find the number of ones in a 32-bit number stored in a1.
(c) Smruti R. Sarangi, 2024 36
A Program with a Simple Function Call
Return value
(c) Smruti R. Sarangi, 2024 37
Compute xn
and store the result in a0.
Initialization
Computation
(c) Smruti R. Sarangi, 2024 38
Load-Store Instructions
Semantics Example Explanation
lw rd, imm(rs1) lw x1, 32(sp) x1 ← mem[sp + 32]
sw rs2, imm(rs1) sw x1, 32(sp) mem[sp+32] ← x1
la rd, label la x1, pi x1 ← address(pi)
• The lw and sw instructions load and store 32-bit values
• There is a need for an assembler directive (la)
• Assign a constant to a label
• Load the label directly to a register
• Generate assembly statements to load the constant to a register
(c) Smruti R. Sarangi, 2024 39
Load Instruction
lw x1, imm(x5)
(c) Smruti R. Sarangi, 2024 40
Store Instruction
Register
file
Memory
x5
x1
imm
sw x1, imm(x5)
(c) Smruti R. Sarangi, 2024 41
Load a0 with the contents of the memory address
sp − s0 × 4 − 12.
Load 17 into the
register s0.
(c) Smruti R. Sarangi, 2024 42
Create a copy of a 10-element array. Assume the starting address of the
original array is stored in a1 and that of the destination array is stored in a2.
(c) Smruti R. Sarangi, 2024 43
Compute the sum of the elements in a 10-element array. Assume that the base
address of the array is stored in a1. Store the result in a0.
(c) Smruti R. Sarangi, 2024 44
Contd…
Add the array
element to a0
(c) Smruti R. Sarangi, 2024 45
Compute the result of the factorial function using a recursive algorithm.
(c) Smruti R. Sarangi, 2024 46
Contd …
(c) Smruti R. Sarangi, 2024 47
Outline
* RISC-V Machine Model
* Integer Instructions
* Control Transfer and Memory Instructions
* Floating Point Instructions
* Instruction Encoding
(c) Smruti R. Sarangi, 2024 48
Floating Point Registers
Register Mnemonic Description
f0-7 ft0-7 Temporary registers
f8-9 fs0-1 Saved registers
f10-11 fa0-1 Arguments/return values
f12-17 fa2-7 Function arguments
f18-27 fs2-11 Saved registers
f28-31 ft8-11 Temporary registers
• No register is hardwired to 0
• It is not possible to load values into FP registers directly
• They can only be initialized by loading values from memory (like x86)
(c) Smruti R. Sarangi, 2024 49
Floating Point Control and Status Register
Mnemonic Explanation
NV Invalid operation
DZ Divide by zero
OF Overflow
UF Underflow
NX Inexact
Rounding
Mode
Mnemonic Meaning
000 RNE Round to nearest, prefer even LSBs
001 RTZ Round towards zero
010 RDN Round down (towards -∞)
011 RUP Round up (towards +∞)
100 RMM Round to nearest (prefer maximum abs. val.)
101,110 Invalid. Reserved for future use
111 DYN Select a rounding mode dynamically (frm field)
Reserved Rounding mode
(frm)
8
31 7 5
24 3
Accrued exceptions (fflags)
NV DZ OF UF NX
1 1 1 1 1
4 0
(c) Smruti R. Sarangi, 2024 50
Floating Point Load and Store Operations
• We load the contents into memory first, and then transfer it to a FP
register.
Semantics Example Explanation
flw rd, imm(rs1) flw f1, 32(sp) f1 ← mem[sp + 32]
fsw rs2, imm(rs1) fsw f1, 32(sp) mem[sp+32] ← f1
(c) Smruti R. Sarangi, 2024 51
Load the value of a constant val into the
floating point register fs1.
Define the constant.
Store its address in a1
Load into an FP register.
(c) Smruti R. Sarangi, 2024 52
Arithmetic Instructions
Semantics Example Explanation
fadd.s rd, rs1, rs2 fadd.s f1, f2, f3 f1 ← f2 + f3
fsub.s rd, rs1, rs2 fsub.s f1, f2, f3 f1 ← f2 - f3
fmul.s rd, rs1, rs2 fmul.s f1, f2, f3 f1 ← f2 × f3
fdiv.s rd, rs1, rs2 fdiv.s f1, f2, f3 f1 ← f2 ÷ f3
fmin.s rd, rs1, rs2 fmin.s f1, f2, f3 f1 ← min(f2 , f3)
fmax.s rd, rs1, rs2 fmax.s f1, f2, f3 f1 ← max(f2 , f3)
fsqrt.s rd, rs1 fsqrt.s f1, f2 f1 ←
• .s is for single precision
• .d is for double precision
(c) Smruti R. Sarangi, 2024 53
Compute , and store the result in fa0.
(c) Smruti R. Sarangi, 2024 54
Multiplication and Conversion Instructions
Semantics Example Explanation
fmadd.s rd, rs1, rs2, rs3 fmadd.s f1, f2, f3, f4 f1 ← f2 * f3 + f4
fmsub.s rd, rs1, rs2, rs3 fmsub.s f1, f2, f3, f4 f1 ← f2 * f3 - f4
fcvt.s.w rd, rs1 fcvt.s.w f1, x5 f1 ← (float) x5
fcvt.w.s rd, rs1 fcvt.w.s x5, f1 x5 ← (int) f1
• fmadd.s and fmsub.s are compound instructions: multiply + add/subtract
• The convert instruction (fcvt) is another way of loading and storing FP values
(c) Smruti R. Sarangi, 2024 55
Compute π × e + 4, and store the result in ft0. Convert the result to an integer and
store the result in a0.
Need to convert
4 to 4.0 first
(c) Smruti R. Sarangi, 2024 56
Floating Point Comparison Instructions
Same as integer comparison
Semantics Example Explanation
flt.s rd, rs1, rs2 flt.s s1, f2, f3 if (f2 < f3) set s1 to 1
fle.s rd, rs1, rs2 fle.s s1, f2, f3 if (f2 ≤ f3) set s1 to 1
feq.s rd, rs1, rs2 feq.s s1, f2, f3 if (f2 == f3) set s1 to 1
(c) Smruti R. Sarangi, 2024 57
First, initialize a0 = 0, then set a0 = 17 if e < π.
Perform the
comparison and
store the result in t0
Check the result of
the comparison.
(c) Smruti R. Sarangi, 2024 58
Outline
* RISC-V Machine Model
* Integer Instructions
* Control Transfer and Memory Instructions
* Floating Point Instructions
* Instruction Encoding
(c) Smruti R. Sarangi, 2024 59
Format/Type Structure Instructions
R rd, rs1, rs2 add, sub, mul, div, rem, and, or, xor, sll, srl, sra, slt, sltu
I rd, rs1, imm
rd, imm(rs1)
addi, andi, ori, xori, slli, srli, srai, slti, sltiu
lw, jalr
S rs2, imm(rs1) sw
U rd, imm lui
Instruction Formats in 32-bit RISC-V
• Define a new instruction type for store instructions
• The default immediate size is 12 bits
• The U-type supports a 20-bit immediate
(c) Smruti R. Sarangi, 2024 60
add, slt and xor
opcode
rd
funct3
funct7 rs1
7
7 3 5
5
rs2
5
R-Type
Differentiate
between opcodes
Further differentiation
between opcodes (add, sub)
opcode
rd
funct3
funct7 rs1
7
12 3 5
5
imm I-Type
12-bit immediate
field
(c) Smruti R. Sarangi, 2024 61
20
opcode
rd
7
5
U-Type
imm
opcode
imm[4..0]
funct3
imm[11..5] rs1
7
7 3 5
5
rs2
5
S-Type
Split the
immediate
Split the
immediate
Do not define
a new format
20-bit immediate
(c) Smruti R. Sarangi, 2024 62
opcode
rd
7
5
20
J-Type
imm[19..12]
imm[10..1]
20 11
opcode
imm[..]
funct3
imm[10..5] rs1
7
7 3 5
5
rs2
5
B-Type
11
12
4..1
Format Structure Instructions
B rs1, rs2, imm beq, bne, blt, bge, bltu, bgeu
J rd, imm jal
immediate bits: LSB is 0
 Do not represent it
immediate bit
≈ ± 4 𝐾𝐵
≈ ± 1 𝑀𝐵
immediate bits: LSB is 0
(c) Smruti R. Sarangi, 2024 63
opcode
rd
width
funct7 rs1
7
12 3 5
5
imm LOAD-FP
12-bit immediate
field
opcode
imm[4..0]
width
imm[11..5] rs1
7
7 3 5
5
rs2
5
STORE-FP
#bytes
loaded/stored
(c) Smruti R. Sarangi, 2024 64
FP Instructions
opcode
rd
rm
funct5 rs1
7
5 2 3 5
5
rs2
fmt
5
opcode
rd
rm
rs3 rs1
7
5 2 3 5
5
rs2
fmt
5
precision
Used by all FP arithmetic,
compare and conversion
instructions.
rounding mode
Fused multiply-add instruction
(c) Smruti R. Sarangi, 2024 65
THE END

More Related Content

PPT
RISC-V processor- computer organization and design
PPTX
Chapter_06_riscv.pptx Chapter_11_memory_system this is part of computer archi...
PDF
Arithmetic, Logical and Shift Instructions.pdf
PDF
IT3030E-CA-Chap3-Instruction Set Architecture.pdf
PPTX
Chapter_04_ARM_Assembly.pptx ARM ASSEMBLY CODE
PDF
02-Language of the Computer.pdf wewe kaka de papa
PDF
Lecture7.pdf
PPTX
RISCV_processor_design_embedded_systems.pptx
RISC-V processor- computer organization and design
Chapter_06_riscv.pptx Chapter_11_memory_system this is part of computer archi...
Arithmetic, Logical and Shift Instructions.pdf
IT3030E-CA-Chap3-Instruction Set Architecture.pdf
Chapter_04_ARM_Assembly.pptx ARM ASSEMBLY CODE
02-Language of the Computer.pdf wewe kaka de papa
Lecture7.pdf
RISCV_processor_design_embedded_systems.pptx

Similar to chapter 6 here is about risc processors and ciscs (20)

PPTX
Chapter_04_ARM_Assembly ARM assembly language is the low-level programming.pptx
PPTX
RISC-V Introduction
PDF
IT3030E-CA-Chap3-ISA-Exercises_aaaaa.pdf
PPTX
Riscv 20160507-patterson
PDF
lecture07_RISCV_Impl.pdflecture07_RISCV_Impl.pdf
PPTX
06-cpu-pre.pptx
PPTX
Instruction set.pptx
PPTX
Risc proscesser qwertyuiop[asdfghjklzxc.pptx
PPTX
CA-Lec4-RISCV-Instructions-1aaaaaaaaaa.pptx
PPT
CO_Chapter2.ppt
PPTX
LU3- 4 Instructions and Sequencing.pptx
PDF
Arm instruction set
PPT
Design and implementation of five stage pipelined RISC-V processor using Ver...
PDF
ARM Architecture Instruction Set
PPTX
THE PROCESSOR
PDF
Procedures in Assembly Language in RISC V
PPT
Lect05 Prog Model
PDF
CAMP-V: Ultimate RISC-V Bootcamp sponsored by MERL and RISC-V
PDF
POWER processor and features presentation
PDF
Unit 3 The processor
Chapter_04_ARM_Assembly ARM assembly language is the low-level programming.pptx
RISC-V Introduction
IT3030E-CA-Chap3-ISA-Exercises_aaaaa.pdf
Riscv 20160507-patterson
lecture07_RISCV_Impl.pdflecture07_RISCV_Impl.pdf
06-cpu-pre.pptx
Instruction set.pptx
Risc proscesser qwertyuiop[asdfghjklzxc.pptx
CA-Lec4-RISCV-Instructions-1aaaaaaaaaa.pptx
CO_Chapter2.ppt
LU3- 4 Instructions and Sequencing.pptx
Arm instruction set
Design and implementation of five stage pipelined RISC-V processor using Ver...
ARM Architecture Instruction Set
THE PROCESSOR
Procedures in Assembly Language in RISC V
Lect05 Prog Model
CAMP-V: Ultimate RISC-V Bootcamp sponsored by MERL and RISC-V
POWER processor and features presentation
Unit 3 The processor
Ad

More from Elisée Ndjabu (20)

PPTX
Chapter_05_x86_assembly ARM processors have a number of general-purpose regis...
PPTX
Chapter_11_memory_system this is part of computer architecture.pptx
PPT
this is lecture two of the course that teaches about embedded systems
PPT
this is lecture one of the course that teaches about embedded systems
PPTX
Chapter_13_Input_Output_and_Storage_Devices.pptx
PPT
LES COURS DE RESEAUX INFORMATIQUE POUR LE DEBUTANTS
PPTX
Troubleshooting and software HADWARE.pptx
PPTX
this is the display for laptop computer sLCD.pptx
PPT
computer maintenance and repairMotherboard.ppt
PPT
253000215- ELECTRONI2QUE DE PUISSANCE Redresseur-C-ppt.ppt
PPT
COMPUTER FAULTS AND SOLUTIONS IN MAINTENANCE .ppt
PPT
Module_5-Processors and sockets.18213704.ppt
PPTX
381402142-Disassembling-System-Unit-Procedure-and-Safety-Precautions.pptx
PDF
les diodes electronique de puisssnce 4_diodes.pdf
PDF
pdfcoffee.com_polycopie-de-cours-ppt-lge604-20012-bf-pdf-free.pdf
PPT
couplage d'alternateurs couplage_alternateurs.ppt
PDF
electronique de puissance Electronique-de-puissance-cours-N°5.pdf
PPT
electronique puissance pour la genie informatique234.ppt
PPT
08_ digital electronics for engineers electronics.ppt
PPTX
11_Fundamentals_of_Digital_Electronics_L.pptx
Chapter_05_x86_assembly ARM processors have a number of general-purpose regis...
Chapter_11_memory_system this is part of computer architecture.pptx
this is lecture two of the course that teaches about embedded systems
this is lecture one of the course that teaches about embedded systems
Chapter_13_Input_Output_and_Storage_Devices.pptx
LES COURS DE RESEAUX INFORMATIQUE POUR LE DEBUTANTS
Troubleshooting and software HADWARE.pptx
this is the display for laptop computer sLCD.pptx
computer maintenance and repairMotherboard.ppt
253000215- ELECTRONI2QUE DE PUISSANCE Redresseur-C-ppt.ppt
COMPUTER FAULTS AND SOLUTIONS IN MAINTENANCE .ppt
Module_5-Processors and sockets.18213704.ppt
381402142-Disassembling-System-Unit-Procedure-and-Safety-Precautions.pptx
les diodes electronique de puisssnce 4_diodes.pdf
pdfcoffee.com_polycopie-de-cours-ppt-lge604-20012-bf-pdf-free.pdf
couplage d'alternateurs couplage_alternateurs.ppt
electronique de puissance Electronique-de-puissance-cours-N°5.pdf
electronique puissance pour la genie informatique234.ppt
08_ digital electronics for engineers electronics.ppt
11_Fundamentals_of_Digital_Electronics_L.pptx
Ad

Recently uploaded (20)

PPTX
Artificial Intelligence
PPT
Mechanical Engineering MATERIALS Selection
DOCX
573137875-Attendance-Management-System-original
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Well-logging-methods_new................
PPTX
Safety Seminar civil to be ensured for safe working.
PPT
Project quality management in manufacturing
PPT
introduction to datamining and warehousing
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Construction Project Organization Group 2.pptx
Artificial Intelligence
Mechanical Engineering MATERIALS Selection
573137875-Attendance-Management-System-original
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Operating System & Kernel Study Guide-1 - converted.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Well-logging-methods_new................
Safety Seminar civil to be ensured for safe working.
Project quality management in manufacturing
introduction to datamining and warehousing
bas. eng. economics group 4 presentation 1.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Internet of Things (IOT) - A guide to understanding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Construction Project Organization Group 2.pptx

chapter 6 here is about risc processors and ciscs

  • 1. 1 Processor Design The Language of Bits Prof. Smruti Ranjan Sarangi IIT Delhi Basic Computer Architecture Chapter 6: RISC-V Assembly Language
  • 2. (c) Smruti R. Sarangi, 2024 2 Download the pdf of the book www.basiccomparch.com videos Slides, software, solution manual Print version (Publisher: WhiteFalcon, 2021) Available on e-commerce sites. The pdf version of the book and all the learning resources can be freely downloaded from the website: www.basiccomparch.com 2nd version
  • 3. 3 Outline * RISC-V Machine Model * Integer Instructions * Control Transfer and Memory Instructions * Floating Point Instructions * Instruction Encoding Code snippets beautified with carbon.app (https://carbon.now.sh)
  • 4. 4 Requirements of a Modern ISA Compatible with all kinds of devices: 32, 64, 128 bits No licensing requirement Large software developer community Extensibility: Possible to add all kinds of novel extensions: vector, crypto, AI/ML, etc. Why did the need for RISC-V arise? Project begins in Berkeley 2010 Creative Commons License 2015 Maintained by the RISC-V Foundation
  • 5. (c) Smruti R. Sarangi, 2024 5 RISC-V Machine Model Name Description RV32 32-bit ISA RV32E 32-bit ISA (embedded version) RV64 64-bit ISA RV64E 64-bit ISA (embedded version) RV128 128-bit ISA Mnemonic Meaning E Embedded version I Base integer ISA M Integer multiplication/division instructions A Atomic instructions F Single-precision floating point D Double-precision floating point V Vector Instructions Different variants of the instruction set Extensions
  • 6. (c) Smruti R. Sarangi, 2024 6 More about Extensions • Integer variants of the ISA • RV32I is a regular 32-bit ISA with integer instructions • RV64I, RV128I • RV32IMA  integer, mul/div, atomic instructions • RV32I1p3 • Major version number: 1 • Minor version number: 3 • G  base integer ISA, additional integer insts, FP instructions, basic synch. instructions • Popular format: RV32G
  • 7. (c) Smruti R. Sarangi, 2024 7 Embedded and Compressed Instructions • We quickly run out of letters !!! • The `Z’ series was introduced • Zfa  Additional floating-point instructions • Embedded version • Does not reduce the instruction width (still 32 bits) • Reduces the number of registers by 50% • Compressed version • Width = 16 bits • Access only 8 registers • Limited opcode support, smaller immediate values Even in 64 and 128-bit formats, instructions are 32 bits. 48 and 64-bit instructions are getting ratified
  • 8. (c) Smruti R. Sarangi, 2024 8 RISC-V Integer Registers Register Mnemonic Description x0 zero Hardwired to zero x1 ra Return address x2 sp Stack pointer x3 gp Global pointer x4 tp Thread pointer (thread- local storage x5-7 t0-2 Temporary registers x8 s0/fp Saved register/frame pointer Register Mnemonic Description x9 s1 Saved register x10-11 a0-1 Function args/ return values x12-17 a2-7 Function args x18-27 s2-11 Saved registers x28-31 t3-6 Temporary registers x0 is hardwired to 0 There is a dedicated pc register Bi-endian
  • 9. (c) Smruti R. Sarangi, 2024 9 x0/zero x1 x2 x3 x4 x5 x6 x7 x8 x9 RV32 RV64 RV128 x31 0 32 64 127 PC 0 32 64 127 View of Registers
  • 10. (c) Smruti R. Sarangi, 2024 10 Outline * RISC-V Machine Model * Integer Instructions * Control Transfer and Memory Instructions * Floating Point Instructions * Instruction Encoding
  • 11. 11 Moving values to registers Semantics Example Explanation addi rd, rs1, imm addi x1, x0, 5 x1 ← 0 + 5 add rd, rs1, rs2 add x1, x0, x3 x1 ← 0 + x3 • Register x0/ zero is hardwired to 0 • Moving a value to a register is accomplished by adding it to zero • Immediates are normally 12 bits in RISC-V • The addi instruction adds an immediate to a register (could be zero) • The regular add instruction can be used • If the first source register is set to zero  Moving a value from one register to another • There is no subi instruction because the immediate is signed (c) Smruti R. Sarangi, 2024
  • 12. (c) Smruti R. Sarangi, 2024 12 Loading Immediates Directly • The lui instruction can load a 20-bit immediate directly into the upper 20 bits. • Note that other instructions only load the lower 12 bits • The li assembler directive can load a 32-bit value directly • Translated to two instructions (internally) • We can either use the x-series registers or the mnemonics • The mnemonics are normally preferred because it is possible to honor the register- usage semantics. Semantics Example Explanation lui rd, imm (20-bit) lui x1, 5 x1 ← 5 << 12 li rd, imm (32-bit) li t0, 0xABCD1234 t0 ← 0xABCD1234
  • 13. (c) Smruti R. Sarangi, 2024 13 Add 409932 + 409823.
  • 14. (c) Smruti R. Sarangi, 2024 14 • Using assembler directives like li makes writing assembly code easier Add 409932 + 409823.
  • 15. (c) Smruti R. Sarangi, 2024 15 Add/Sub Instructions Semantics Example Explanation add rd, rs1, rs2 add x1, x2, x3 x1 ← x2 + x3 addi rd, rs1, imm addi x1, x2, 5 x1 ← x2 + 5 sub rd, rs1, rs2 sub x1, x2, x3 x1 ← x2 - x3
  • 16. (c) Smruti R. Sarangi, 2024 16 Compute 4 + 5 - 19.
  • 17. (c) Smruti R. Sarangi, 2024 17 Swap two numbers stored in x1 and x2 (resp.).
  • 18. (c) Smruti R. Sarangi, 2024 18 Multiplication and Division • Part of the ‘M’ extension • The default mul instruction computes the lower 32 bits • The product ideally requires 64 bits • The mulh and mulhu instructions can be used to compute the upper 32 bits • The HW dynamically recognizes a consecutive mul-mulh instruction pair, and replaces it with a single multiplication Semantics Example Explanation mul rd, rs1, rs2 mul x1, x2, x3 x1 ← x2 * x3 div rd, rs1, rs2 div x1, x2, x3 x1 ← x2 / x3 rem rd, rs1, rs2 rem x1, x2, x3 x1 ← rem (x2 / x3)
  • 19. (c) Smruti R. Sarangi, 2024 19 Division Rules Division Operation Quotient Remainder 4 ÷ 3 1 1 4 ÷ (-3) -1 1 (-4) ÷ 3 -1 -1 (-4) ÷ (-3) 1 -1 Round towards 0 Sign of the remainder is the same as the sign of the dividend Consecutive division and remainder instructions (with the same operands) can be fused.
  • 20. (c) Smruti R. Sarangi, 2024 20 Multiply 3 with (-17).
  • 21. (c) Smruti R. Sarangi, 2024 21 Compute 123 + 1.
  • 22. (c) Smruti R. Sarangi, 2024 22 Divide -50 / 3. Dynamically fused by hardware.
  • 23. (c) Smruti R. Sarangi, 2024 23 Logical and Shift Instructions Semantics Example Explanation and rd, rs1, rs2 and x1, x2, x3 x1 ← x2 AND x3 andi rd, rs1, imm andi x1, x2, 6 x1 ← x2 AND 6 or rd, rs1, rs2 or x1, x2, x3 x1 ← x2 OR x3 ori rd, rs1, imm ori x1, x2, 9 x1 ← x2 OR 9 xor rd, rs1, rs2 xor x1, x2, x3 x1 ← x2 XOR x3 xori rd, rs1, imm xori x1, x2, 9 x1 ← x2 XOR 9 sll rd, rs1, rs2 sll x1, x2, x3 x1 ← x2 << x3 srl rd, rs1, rs2 srl x1, x2, x3 x1 ← x2 >> x3 sra rd, rs1, rs2 sra x1, x2, x3 x1 ← x2 >>> x3 slli rd, rs1, imm slli x1, x2, 3 x1 ← x2 << 3 srli rd, rs1, imm srli x1, x2, 3 x1 ← x2 >> 3 srai rd, rs1, imm srai x1, x2, 3 x1 ← x2 >>> 3 right-shift arithmetic
  • 24. (c) Smruti R. Sarangi, 2024 24 Generic Format of Shift Instructions Generic format sll srl sra slli srli srai rd rd rd rd rd rd rs1 rs1 rs1 rs1 rs1 rs1 rs2 rs2 rs2 imm imm imm , , , , , , , , , , , , , , , , , ,
  • 25. (c) Smruti R. Sarangi, 2024 25 Compute the bitwise OR of A and B. Let A = 4 and B = 1.
  • 26. (c) Smruti R. Sarangi, 2024 26 Compute t1 = t2 + t3 × 4 Strength reduction Multiplications are slow. Shifts are much faster.
  • 27. (c) Smruti R. Sarangi, 2024 27 Outline * RISC-V Machine Model * Integer Instructions * Control Transfer and Memory Instructions * Floating Point Instructions * Instruction Encoding
  • 28. (c) Smruti R. Sarangi, 2024 28 Conditional Control Transfer Instructions Semantics Example Explanation beq rs1, rs2, label beq x1, x2, .foo Branch to the . foo label if x1 = x2 bne rs1, rs2, label bne x1, x2, .foo Branch to the .foo label if x1 ≠ x2 bge rs1, rs2, label bge x1, x2, .foo Branch to the .foo label if x1 ≥ x2 blt rs1, rs2, label blt x1, x2, .foo Branch to the .foo label if x1 < x2 bgeu rs1, rs2, label bgeu x1, x2, .foo Similar to bge, considers unsigned values. bltu rs1, rs2, label bltu x1, x2, .foo Similar to blt, considers unsigned values. Semantics Example Explanation slt rd, rs1, rs2 slt x1, x2, x3 if (x2 < x3) set x1 to 1 slti rd, rs1, imm slti x1, x2, 5 if (x2 < 5) set x1 to 1 sltu rd, rs1, rs2 sltu x1, x2, 5 if (x2 <unsigned x3) set x1 to 1 sltui rd, rs1, imm sltui x1, x2, x3 if (x2 <unsigned 5) set x1 to 1
  • 29. (c) Smruti R. Sarangi, 2024 29 Control Transfer Instructions RISC-V does not have a flags register. Instead, the flags are stored in a register that the programmer specifies. Compare 2 and 5 and store the comparison result in t2. [t2 = (2 < 5)]
  • 30. (c) Smruti R. Sarangi, 2024 30 Add two 64-bit numbers (stored in two 32-bit registers) Note: sltu does an unsigned comparison. t0 < t4 → there is a carry
  • 31. (c) Smruti R. Sarangi, 2024 31 Add all the numbers from 1 to 10.
  • 32. (c) Smruti R. Sarangi, 2024 32 Unconditional Branches • The jal instruction performs the role of an unconditional branch and a call instruction: • The return address is explicitly specified (there is no default) • If the return addr. register is zero, then this is a regular unconditional branch instruction • Otherwise, it is a call instruction • jalr is like a return instruction • In this case, the return address is stored in rd • If the return addr. register is zero, then nothing is stored. Functions like an indirect branch. Semantics Example Explanation jal rd, label jal x1, func Jump to the func label and store the return address in x1 jalr rd, rs1, offset jalr x1, x2, 20 Jump to the address x2 + 20 and store the return address in x1
  • 33. (c) Smruti R. Sarangi, 2024 33 Check if the number stored in a1 is a square or not?
  • 34. (c) Smruti R. Sarangi, 2024 34 Check whether the number stored in a1 is a prime number.
  • 35. (c) Smruti R. Sarangi, 2024 35 Find the number of ones in a 32-bit number stored in a1.
  • 36. (c) Smruti R. Sarangi, 2024 36 A Program with a Simple Function Call Return value
  • 37. (c) Smruti R. Sarangi, 2024 37 Compute xn and store the result in a0. Initialization Computation
  • 38. (c) Smruti R. Sarangi, 2024 38 Load-Store Instructions Semantics Example Explanation lw rd, imm(rs1) lw x1, 32(sp) x1 ← mem[sp + 32] sw rs2, imm(rs1) sw x1, 32(sp) mem[sp+32] ← x1 la rd, label la x1, pi x1 ← address(pi) • The lw and sw instructions load and store 32-bit values • There is a need for an assembler directive (la) • Assign a constant to a label • Load the label directly to a register • Generate assembly statements to load the constant to a register
  • 39. (c) Smruti R. Sarangi, 2024 39 Load Instruction lw x1, imm(x5)
  • 40. (c) Smruti R. Sarangi, 2024 40 Store Instruction Register file Memory x5 x1 imm sw x1, imm(x5)
  • 41. (c) Smruti R. Sarangi, 2024 41 Load a0 with the contents of the memory address sp − s0 × 4 − 12. Load 17 into the register s0.
  • 42. (c) Smruti R. Sarangi, 2024 42 Create a copy of a 10-element array. Assume the starting address of the original array is stored in a1 and that of the destination array is stored in a2.
  • 43. (c) Smruti R. Sarangi, 2024 43 Compute the sum of the elements in a 10-element array. Assume that the base address of the array is stored in a1. Store the result in a0.
  • 44. (c) Smruti R. Sarangi, 2024 44 Contd… Add the array element to a0
  • 45. (c) Smruti R. Sarangi, 2024 45 Compute the result of the factorial function using a recursive algorithm.
  • 46. (c) Smruti R. Sarangi, 2024 46 Contd …
  • 47. (c) Smruti R. Sarangi, 2024 47 Outline * RISC-V Machine Model * Integer Instructions * Control Transfer and Memory Instructions * Floating Point Instructions * Instruction Encoding
  • 48. (c) Smruti R. Sarangi, 2024 48 Floating Point Registers Register Mnemonic Description f0-7 ft0-7 Temporary registers f8-9 fs0-1 Saved registers f10-11 fa0-1 Arguments/return values f12-17 fa2-7 Function arguments f18-27 fs2-11 Saved registers f28-31 ft8-11 Temporary registers • No register is hardwired to 0 • It is not possible to load values into FP registers directly • They can only be initialized by loading values from memory (like x86)
  • 49. (c) Smruti R. Sarangi, 2024 49 Floating Point Control and Status Register Mnemonic Explanation NV Invalid operation DZ Divide by zero OF Overflow UF Underflow NX Inexact Rounding Mode Mnemonic Meaning 000 RNE Round to nearest, prefer even LSBs 001 RTZ Round towards zero 010 RDN Round down (towards -∞) 011 RUP Round up (towards +∞) 100 RMM Round to nearest (prefer maximum abs. val.) 101,110 Invalid. Reserved for future use 111 DYN Select a rounding mode dynamically (frm field) Reserved Rounding mode (frm) 8 31 7 5 24 3 Accrued exceptions (fflags) NV DZ OF UF NX 1 1 1 1 1 4 0
  • 50. (c) Smruti R. Sarangi, 2024 50 Floating Point Load and Store Operations • We load the contents into memory first, and then transfer it to a FP register. Semantics Example Explanation flw rd, imm(rs1) flw f1, 32(sp) f1 ← mem[sp + 32] fsw rs2, imm(rs1) fsw f1, 32(sp) mem[sp+32] ← f1
  • 51. (c) Smruti R. Sarangi, 2024 51 Load the value of a constant val into the floating point register fs1. Define the constant. Store its address in a1 Load into an FP register.
  • 52. (c) Smruti R. Sarangi, 2024 52 Arithmetic Instructions Semantics Example Explanation fadd.s rd, rs1, rs2 fadd.s f1, f2, f3 f1 ← f2 + f3 fsub.s rd, rs1, rs2 fsub.s f1, f2, f3 f1 ← f2 - f3 fmul.s rd, rs1, rs2 fmul.s f1, f2, f3 f1 ← f2 × f3 fdiv.s rd, rs1, rs2 fdiv.s f1, f2, f3 f1 ← f2 ÷ f3 fmin.s rd, rs1, rs2 fmin.s f1, f2, f3 f1 ← min(f2 , f3) fmax.s rd, rs1, rs2 fmax.s f1, f2, f3 f1 ← max(f2 , f3) fsqrt.s rd, rs1 fsqrt.s f1, f2 f1 ← • .s is for single precision • .d is for double precision
  • 53. (c) Smruti R. Sarangi, 2024 53 Compute , and store the result in fa0.
  • 54. (c) Smruti R. Sarangi, 2024 54 Multiplication and Conversion Instructions Semantics Example Explanation fmadd.s rd, rs1, rs2, rs3 fmadd.s f1, f2, f3, f4 f1 ← f2 * f3 + f4 fmsub.s rd, rs1, rs2, rs3 fmsub.s f1, f2, f3, f4 f1 ← f2 * f3 - f4 fcvt.s.w rd, rs1 fcvt.s.w f1, x5 f1 ← (float) x5 fcvt.w.s rd, rs1 fcvt.w.s x5, f1 x5 ← (int) f1 • fmadd.s and fmsub.s are compound instructions: multiply + add/subtract • The convert instruction (fcvt) is another way of loading and storing FP values
  • 55. (c) Smruti R. Sarangi, 2024 55 Compute π × e + 4, and store the result in ft0. Convert the result to an integer and store the result in a0. Need to convert 4 to 4.0 first
  • 56. (c) Smruti R. Sarangi, 2024 56 Floating Point Comparison Instructions Same as integer comparison Semantics Example Explanation flt.s rd, rs1, rs2 flt.s s1, f2, f3 if (f2 < f3) set s1 to 1 fle.s rd, rs1, rs2 fle.s s1, f2, f3 if (f2 ≤ f3) set s1 to 1 feq.s rd, rs1, rs2 feq.s s1, f2, f3 if (f2 == f3) set s1 to 1
  • 57. (c) Smruti R. Sarangi, 2024 57 First, initialize a0 = 0, then set a0 = 17 if e < π. Perform the comparison and store the result in t0 Check the result of the comparison.
  • 58. (c) Smruti R. Sarangi, 2024 58 Outline * RISC-V Machine Model * Integer Instructions * Control Transfer and Memory Instructions * Floating Point Instructions * Instruction Encoding
  • 59. (c) Smruti R. Sarangi, 2024 59 Format/Type Structure Instructions R rd, rs1, rs2 add, sub, mul, div, rem, and, or, xor, sll, srl, sra, slt, sltu I rd, rs1, imm rd, imm(rs1) addi, andi, ori, xori, slli, srli, srai, slti, sltiu lw, jalr S rs2, imm(rs1) sw U rd, imm lui Instruction Formats in 32-bit RISC-V • Define a new instruction type for store instructions • The default immediate size is 12 bits • The U-type supports a 20-bit immediate
  • 60. (c) Smruti R. Sarangi, 2024 60 add, slt and xor opcode rd funct3 funct7 rs1 7 7 3 5 5 rs2 5 R-Type Differentiate between opcodes Further differentiation between opcodes (add, sub) opcode rd funct3 funct7 rs1 7 12 3 5 5 imm I-Type 12-bit immediate field
  • 61. (c) Smruti R. Sarangi, 2024 61 20 opcode rd 7 5 U-Type imm opcode imm[4..0] funct3 imm[11..5] rs1 7 7 3 5 5 rs2 5 S-Type Split the immediate Split the immediate Do not define a new format 20-bit immediate
  • 62. (c) Smruti R. Sarangi, 2024 62 opcode rd 7 5 20 J-Type imm[19..12] imm[10..1] 20 11 opcode imm[..] funct3 imm[10..5] rs1 7 7 3 5 5 rs2 5 B-Type 11 12 4..1 Format Structure Instructions B rs1, rs2, imm beq, bne, blt, bge, bltu, bgeu J rd, imm jal immediate bits: LSB is 0  Do not represent it immediate bit ≈ ± 4 𝐾𝐵 ≈ ± 1 𝑀𝐵 immediate bits: LSB is 0
  • 63. (c) Smruti R. Sarangi, 2024 63 opcode rd width funct7 rs1 7 12 3 5 5 imm LOAD-FP 12-bit immediate field opcode imm[4..0] width imm[11..5] rs1 7 7 3 5 5 rs2 5 STORE-FP #bytes loaded/stored
  • 64. (c) Smruti R. Sarangi, 2024 64 FP Instructions opcode rd rm funct5 rs1 7 5 2 3 5 5 rs2 fmt 5 opcode rd rm rs3 rs1 7 5 2 3 5 5 rs2 fmt 5 precision Used by all FP arithmetic, compare and conversion instructions. rounding mode Fused multiply-add instruction
  • 65. (c) Smruti R. Sarangi, 2024 65 THE END