04 - Instruction Set Architecture-RV Part III V2
04 - Instruction Set Architecture-RV Part III V2
Sulochana Sooriyaarachchi
Chathuranga Hettiarachchi
2
Instruction Formats(Types)
Fields
◼ Opcode : 7bits
◼ funct 3 : 3 bit function
◼ funct 7 : 7 bit function
◼ rs1, rs2 : two source registers (5 bits each)
◼ rd : destination register (5 bits)
3
Instruction Formats (6 Types)
R-Format: instructions using 3 register inputs
– add, xor, mul —arithmetic/logical ops
I-Format: instructions with immediates, loads
– addi, lw, jalr, slli
4
Instruction Format-Register Type
(Register Type)
(Immediate Type)
6
Instruction Format-Upper Immediate
10
Memory Addressing
Place an
address on
address bus
Read or write
operation
Data placed on
data bus
Source: www.alf.sd83.bc.ca/courses/It12/using_it/processor_speed.htm
11
Memory Addressing (Cont.)
9
No of words
Address Data
Word
12
Addressing Modes
It is the way microprocessor:
◼ Identifies location of data
◼ Access data
Absolute address
◼ Actual physical address
◼ Direct addressing
Relative address
◼ Address relative to a known reference
◼ Indirect addressing 13
Load and Store Instructions
14
Activity
Try the following
program with RIPES
.data and explain the machine
A: .word 0x1F2F3F4F
.text code
.globl main
main:
la a0, A
la a0, A What is the machine
la a0, A
ret code of la a0, A ?
.end
Try to add a nop before
first la and revisit
machine code 15
la (Load Address) pseudo instruction
.data
A: .word 0x1F2F3F4F 000000d8 <main>:
.text d8: 00002517 auipc a0,0x2
dc: 0b050513 addi a0,a0,176 # 2188 <A>
.globl main e0: 00002517 auipc a0,0x2
main: e4: 0a850513 addi a0,a0,168 # 2188 <A>
la a0, A e8: 00002517 auipc a0,0x2
la a0, A ec: 0a050513 addi a0,a0,160 # 2188 <A>
la a0, A f0: 00008067 ret
ret
.end
17
Branching Instructions
(Branch type)
18
Branching Instructions
PC-relative addressing
◼ Use immediate field 12 bits as a two’s complement
offset to PC.
◼ Branches generally change the PC only by a small
amount. But what is the reach?
◼ Can specify [-211 , 211) address offsets from the PC
20
Branching Instructions -Addressing
Recall: RISCV uses 32-bit addresses, and
memory is byte-addressed
Instructions are “word-aligned”: Address is
always a multiple of 4 (in bytes)
◼ Previous instruction offset : -4 bytes
◼ Next instruction offset : 4 bytes
◼ Only if compressed instructions (16 bytes) are used, it
could be multiple of 2 (in bytes) as well
PC ALWAYS points to an instruction
◼ PC is typed as a pointer to a word
◼ can do C-like pointer arithmetic
Branching Instructions -Addressing
Only 12 bits available for immediate offset
Offset Range [-211 , 211) Ignore the lsb,
always zero
1 1 1 1 1 1 1 1 1 1 1 0 0 (only 12 bits)
Offset : -4
1 1 1 9 8 7 6 5 4 3 2 1 0
2 1 0
12 bit Immediate field Provision for compressed instructions
(Two’s complement) (For normal instructions, this also will be
always zero)
23
Branching Instructions (B type)
main: bne x5,x6,repeat
addi t0,zero,10 Offset : -4 = -000100
add t1,zero,zero bne t0,t1,repeat = 111011 +1
repeat:
= 111100 (Sign extended)
addi t1,t1,1
bne t0,t1,repeat 1 1 1 1 1 1 1 1 1 1 1 0 0
ret 1 1 1 9 8 7 6 5 4 3 2 1 0
.end 2 1 0
31 20 19 15 14 12 11 7 6 0
main:
addi t0,zero,10
add t1,zero,zero
repeat:
addi t1,t1,1
bne t0,t1,repeat
ret
.end
Remember
bne t0,t1,repeat
Machine Instruction: 0xFE629EE3
26
Jump Instructions
27
Jump Instructions
Store a return address in a register (rd), so that you can come back
to original flow
Known as “Linking”
(Jump type and Immediate type)
28
Jump Instructions
For branches, we assumed that we won’t
want to branch too far, so we can specify a
change in the PC
For (jalr) jumps, we may jump to
anywhere in code memory
◼ Ideally, we would specify a 32-bit memory address
to jump to
◼ Unfortunately, we can’t fit both a 7-bit opcode
and a 32-bit address into a single 32-bit word
◼ Also, when linking we must write to an rd register
29
jal Instruction and j pseudo
instruction
jal saves PC+4 in register rd (the return
address)
Set PC = PC + offset (PC-relative jump)
Target somewhere within ±219 locations, 2 bytes
apart
“j” jump is a pseudo-instruction—the assembler
will instead use jal but sets rd=x0 to discard
return address
Immediate encoding optimized similarly to
branch instruction to reduce hardware cost 30
jal Instruction Encoding
jal x1,-12 -12 = -001100
jal ra,myfunction = 110011 +1
= … 1111 1111 1111 0100
31 20 19 15 14 12 11 7 6 0
Imm[20|10:1|11|19:12] rd J type
32
jal Instruction Usage
.globl main
myfunc:
addi t0, zero, 1
ret
main:
addi t0, zero, 0
jal ra,myfunc
ret
.end
33
jalr instruction (I type)
Jump and link register jalr to address given in
rs1 register + imm offset
RISCV Instructions:
# ret and jr psuedo-instructions
ret = jr ra = jalr x0, ra, 0
0 0 0 1 1 0 0 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1
0 0 0 0 8 0 E 7
.globl main
myfunc:
addi t0, zero, 1
ret
main:
addi t0, zero, 0
la ra,myfunc
jalr ra,ra,0
ret
.end
36
.globl main
myfunc:
addi t0, zero, 1
ret
main:
addi t0, zero, 0
la ra,myfunc
jalr ra,ra,0
ret
.end
37
Summary
39
Example – Logic Operations
Write an assembly program to convert a given
character from uppercase to lowercase & vice
versa
If we consider ASCII, this can be achieved by
changing the 5th bit
◼ A = 65 =0x41 = 01000001
◼ a = 97 =0x61 = 01100001
40
Homework
Write an assembly program to multiply 3 & 4
Steps:
◼ How many registers?
◼ What registers to use?
◼ What instructions to use?
41
Exercise
Which mask or filter value would you use, and
what operation would you perform to make the
2nd & 4th bits one (1) no matter what they were
before?
42
Exercise: Convert to Assembly
int total = 0;
for (int i=10 ; i!=0; i--)
{
total += i;
}
43
Exercise
Write a program to calculate the total of all
integers from 1 to 10
High-level program
int total = 0;
for (int i=1 ; i<=10; i++)
{
total += i;
}
44
Exercise (Cont.)
Steps
◼ Are there any conditions/loops?
◼ How many registers?
◼ What registers to use?
◼ What instructions to use?
45
Summary
Instruction Set Architecture (ISA) is the layer between
hardware & software
Specific to a given chip unless standardized
◼ Defines registers it contain
◼ Micro-operations performed on data stored on those registers
Typical Assembly instructions for
◼ Register operations
◼ Memory access (Load/Store)
◼ Upper immediates and Address calculations
◼ Branching
◼ Jump and Link
Assembler translates human readable Assembly
code to machine code 46
Sign extended immediate values
addi t1, t1, -0xFFFFFFFF # = 1 # ||
addi t1, t1, -0xFFFFFFFE # = 2 # ||
addi t1, t1, -0xFFFFFFFD # = 3 # ||
addi t1, t1, -0xFFFFF801 # = 2047 # ||
# addi t1, t1, -0xFFFFF800 # = 2048 (Out of range)
# addi t1, t1, -0xFFFFF7FF # = 2049 (Out of range)
# addi t1, t1, -0x00000801 # = -2049 (Out of range)
addi t1, t1, -0x00000800 # = -2048 # ||
addi t1, t1, -0x000007FF # = -2047 # ||
addi t1, t1, -0x00000001 # = -1 # ||
addi t1, t1, 0x00000000 # = 0 # ||
addi t1, t1, 0x00000001 # = 1 # ||
addi t1, t1, 0x000007FF # = 2047 # ||
# addi t1, t1, 0x00000800 # = 2048 (Out of range)
# addi t1, t1, 0x00000801 # = 2049 (Out of range)
# addi t1, t1, 0xFFFFF7FF # = -2049 (Out of range)
addi t1, t1, 0xFFFFF800 # = -2048
addi t1, t1, 0xFFFFFFFE # = -2
addi t1, t1, 0xFFFFFFFF # = -1
47
Thank you!
48
(Compare with RISC-V Instructions)
F→ file register
W→ working register
B→ bit
L→ literal (number)
Z→ conditional
execution
d→ destination bit
d=0 store in W
d=1 store in f
use , w or ,f instead
49