Lecture6 RISC V Assembly II
Lecture6 RISC V Assembly II
Computer Architectures
2
Hexadecimal
● Base 16
○ Compact representation of bit strings, 4 bits per hex digit
3
RISC-V to Machine Language
● The segments of an instruction is called a field.
4
RISC-V R-format Instructions
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
5
R-format Example
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
6
R-format Example
funct7 rs2 rs1 funct3 rd opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
0 21 20 0 9 51
0000 0001 0101 1010 0000 0100 1011 0011 two = 015A04B316
7
RISC-V I-format Instructions
immediate rs1 funct3 rd opcode
12 bits 5 bits 3 bits 5 bits 7 bits
8
RISC-V I-format Instructions
immediate rs1 funct3 rd opcode
12 bits 5 bits 3 bits 5 bits 7 bits
9
RISC-V I-format Instructions
000010000000 10110 001 01001 0000011
12 bits 5 bits 3 bits 5 bits 7 bits
10
RISC-V S-format Instructions
Imm[11:5] rs2 rs1 funct3 imm[4:0] opcode
7 bits 5 bits 5 bits 3 bits 5 bits 7 bits
11
Instruction Encoding Example
● C code:
A[30] = h + A[30] + 1
12
Instruction Encoding Example
● C code:
A[30] = h + A[30] + 1
14
Instruction Encoding Example
ld x9, 240(x10) // Temporary reg x9 gets A[30]
add x9, x21, x9 // Temporary reg x9 gets h+A[30]
addi x9, x9, 1 // Temporary reg x9 gets h+A[30]+1
sd x9, 240(x10) // Stores h+A[30]+1 back into A[30]
000011110000 01010 011 01001 0000011
12 bits 5 bits 3 bits 5 bits 7 bits
16
Stored Program Computers
● Instructions represented in binary, just like data The BIG Picture
● Instructions and data stored in memory
● Programs can operate on programs
○ e.g., compilers, linkers, …
17
Logical Operations
● Instructions for bitwise manipulation
● Useful for extracting and inserting groups of bits in a word
After:
19
Shift Operations
● They move all the bits in a doubleword to the left or right
○ filling the emptied bits with 0s.
20
Shift Operations
● They move all the bits in a doubleword to the left or right
○ filling the emptied bits with 0s.
After:
21
Shift Operations
● They move all the bits in a doubleword to the left or right
○ filling the emptied bits with 0s.
22
Shift Operations
● Shift left logical (slli) and Shift right logical (srli)
23
Shift Operations
● slli example
● srli example
24
AND Operations
● Useful to mask bits in a word
○ Select some bits, clear others to 0
and x9,x10,x11
x10 00000000 00000000 00000000 00000000 00000000 00000000 00001101 11000000
25
OR Operations
● Useful to include bits in a word
○ Set some bits to 1, leave others unchanged
or x9,x10,x11
26
XOR Operations
● Differencing operation
○ Set some bits to 1, leave others unchanged
27
NOT Operation
● NOT takes one operand
○ places a 1 in the result if one operand bit is a 0, and vice versa.
29
Example
● Set the most significant 4 bits of a word in x9 to 1:
30
Example
● Rotate left (circular shift) the register x9 by four bits
○ four most significant bits of x9 will move to its least significant bits
31
Example
● Rotate left (circular shift) the register x9 by four bits
○ four most significant bits of x9 will move to its least significant bits
32
Logical Instructions - Summary
● RISC-V also provides the instructions and immediate (andi), or
immediate (ori), and exclusive or immediate (xori).
33
The end
● Read Sections 2.5 and 2.6 of your book.
34