Lecture 16 - RISC-V Assembly Language
Lecture 16 - RISC-V Assembly Language
Lecture 16:
RISC-V Assembly Language
Lecture 16
• Introduction
– Instruction Set Architecture (ISA)
– RISC-V History
• RISC-V Assembly Language
– Instructions
– Register Set
– Memory
– Programming constructs
• Introduction
– Instruction Set Architecture (ISA)
– RISC-V History
• RISC-V Assembly Language
– Instructions
– Register Set
– Memory
– Programming constructs
• Introduction
– Instruction Set Architecture (ISA)
– RISC-V History
• RISC-V Assembly Language
– Instructions
– Register Set
– Memory
– Programming constructs
• sub: mnemonic
• b, c: source operands
• a: destination operand
RISC-V is byte-addressable
00000003 4 0 F 3 0 7 8 8 Word 3
00000002 0 1 E E 2 8 4 2 Word 2
00000001 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
Note: RISC-V uses byte-addressable memory, which we’ll talk about next.
Digital Design and Computer Architecture: RISC-V Edition
Chapter 6 <24> Harris & Harris © 2020 Elsevier
Reading Word-Addressable Memory
• Memory read called load
• Mnemonic: load word (lw)
• Format:
lw <rd>, <offset>(<base register>)
lw t1, 5(s0)
Address calculation:
– add offset (5) to the base address (s0)
– address = (s0 + 5)
• Destination register (rd):
– t1 holds the value at address (s0 + 5)
Any register may be used as base address
Digital Design and Computer Architecture: RISC-V Edition
Chapter 6 <25> Harris & Harris © 2020 Elsevier
Reading Word-Addressable Memory
• Example: read a word of data at memory
address 1 into s3
– address = (0 + 1) = 1
– s3 = 0xF2F1AC07 after load
Assembly code
lw s3, 1(zero) # read memory word 1 into s3
00000003 4 0 F 3 0 7 8 8 Word 3
00000002 0 1 E E 2 8 4 2 Word 2
00000001 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
00000003 4 0 F 3 0 7 8 8 Word 3
00000002 0 1 E E 2 8 4 2 Word 2
00000001 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
0000000C 4 0 F 3 0 7 8 8 Word 3
00000008 0 1 E E 2 8 4 2 Word 2
00000004 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
width = 4 bytes
Digital Design and Computer Architecture: RISC-V Edition
Chapter 6 <29> Harris & Harris © 2020 Elsevier
Reading Byte-Addressable Memory
0000000C 4 0 F 3 0 7 8 8 Word 3
00000008 0 1 E E 2 8 4 2 Word 2
00000004 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
width = 4 bytes
0000000C 4 0 F 3 0 7 8 8 Word 3
00000008 0 1 E E 2 8 4 2 Word 2
00000004 F 2 F 1 A C 0 7 Word 1
00000000 A B C D E F 7 8 Word 0
width = 4 bytes
C D E F C F E D C
8 9 A B 8 B A 9 8
4 5 6 7 4 7 6 5 4
0 1 2 3 0 3 2 1 0
MSB LSB MSB LSB
Digital Design and Computer Architecture: RISC-V Edition
Chapter 6 <33> Harris & Harris © 2020 Elsevier
Big-Endian & Little-Endian Memory
• Jonathan Swift’s Gulliver’s Travels: the Little-Endians
broke their eggs on the little end of the egg and the Big-
Endians broke their eggs on the big end
• It doesn’t really matter which addressing type used –
except when the two systems need to share data!
Big-Endian Little-Endian
Byte Word Byte
Address Address Address
C D E F C F E D C
8 9 A B 8 B A 9 8
4 5 6 7 4 7 6 5 4
0 1 2 3 0 3 2 1 0
MSB LSB MSB LSB
Big-Endian Little-Endian
Word
Byte Address 0 1 2 3 Address 3 2 1 0 Byte Address
Data Value 23 45 67 89 0 23 45 67 89 Data Value
MSB LSB MSB LSB
Digital Design and Computer Architecture: RISC-V Edition
Chapter 6 <36> Harris & Harris © 2020 Elsevier
Programming
• High-level languages:
– e.g., C, Java, Python
– Written at higher level of abstraction
• Common high-level software constructs:
– if/else statements
– for loops
– while loops
– arrays
– function calls
target: # label
add s1, s1, s0 # s1 = 4 + 4 = 8
Labels indicate instruction location. They can’t be reserved words and
must be followed by colon (:)
target:
add s1, s1, s0 # s1 = 1 + 4 = 5
target:
add s1, s1, s0 # s1 = 1 + 4 = 5
{s4, s3} = s1 x s2
L1:
f = f – i; sub s0, s0, s3
else L1:
f = f – i; sub s0, s0, s3
done:
0x12340010 array[4]
0x1234800C array[3]
0x12348008 array[2]
0x12348004 array[1]
0x12348000 array[0]
loop:
bge s1, t2, done # if not then done
slli t0, s1, 2 # t0 = i * 4 (byte offset)
add t0, t0, s0 # address of array[i]
lw t1, 0(t0) # t1 = array[i]
slli t1, t1, 3 # t1 = array[i] * 8
sw t1, 0(t0) # array[i] = array[i] * 8
addi s1, s1, 1 # i = i + 1
j loop # repeat
done:
• Introduction
– Instruction Set Architecture (ISA)
– RISC-V History
• RISC-V Assembly Language
– Instructions
– Register Set
– Memory
– Programming constructs