LAB 09 RISC-V Assembly (Part I: Introduction) : EE-222 Microprocessors Systems April 11, 2019
LAB 09 RISC-V Assembly (Part I: Introduction) : EE-222 Microprocessors Systems April 11, 2019
LAB 09 RISC-V Assembly (Part I: Introduction) : EE-222 Microprocessors Systems April 11, 2019
Contents
1 Administrivia 2
1.1 Learning Outcomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Deliverable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 RISC-V 3
2.1 RISC-V Instruction Set Architecture . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Register File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 RISC-V Instruction Format . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.4 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5 Addressing Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 RISC-V Assembly 5
3.1 Venus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Writing Assembly Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.4 Assembler Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.5 Environment Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4 Lab Tasks 8
4.1 Task A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1.1 Answer the given questions . . . . . . . . . . . . . . . . . . . . . . . 8
4.2 Task B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.3 Task C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
1 Administrivia
1.1 Learning Outcomes
By the end of this lab you will be able to;
1.2 Deliverable
You are required to submit
2
2 RISC-V
RISC-V is an open source architecture based on reduced instruction set computing. In
contrast to most ISAs, the RISC-V ISA is free and can be used royalty-free for any purpose,
permitting anyone to design, manufacture and sell RISC-V chips and software.
Extension Functionality
RV32I Base 32-bit integer
RV64I Instructions to manipulate 64-bit data chunks are added
M Multiply and Divide instructions are added
F Single precision floating point instructions added
D Double precision floating point instructions added
A Atomic instruction for concurrent processing
3
2.3 RISC-V Instruction Format
RISC-V is a load-store architecture. All instructions are 32-bits wide and divided into four
categories.
Instruction Type Usage Format Example
Register-register
R-Type name dest, src1, src2 add s0, s1, s2
ALU instruction
ALU Instructions
I-Type with immediates name dest, src1, imm addi s0, s1, 9
(including Loads)
Compare and branch
S-Type name src1, src2, imm beq s0, s1, label
(including Stores)
U-Type Jump and Link name dest, imm jal ra, label
Jump and Link
UJ-Type name dest, src1, imm jalr ra, s1, label
Register
4
3 RISC-V Assembly
In this lab we will deal with RISC-V assembly. It is a convention to end assembly program
files with .s extension. To run RISC-V assembly we will use Venus simulator developed by
UC Berkeley.
3.1 Venus
Venus is a RISC-V (32-bit address space) instruction set simulator build for education pur-
pose available online here2
Follow the given instructions to properly use the simulator.
• Editor tab is for writing assembly code.
• Programs start at the first line regardless of the label. That means, the main function
must be put first.
• Once Venus is loaded into your browser, don’t refresh the page. You may lose your
code.
• When you are done editing, click the “Simulator” tab to prepare for execution.
• The right pannel in simulator shows the contents of general purpose registers and
memory.
• The rest is for you to explore.
5
3.3 Memory Allocation
When a program is run, the operating system creates specialized memory segments in the
main memory specific to that program.
Generally, four segments are created,
Directive Function
.data Store subsequent items in the static segment at the next available address.
.text Store subsequent instructions in the text segment at the next available address.
.byte Store listed values as 8-bit bytes.
.asciiz Store subsequent string in the data segment and add null-terminator.
.word Store listed values as unaligned 32-bit words.
.globl Makes the given label global.
6
3.5 Environment Call
These are calls to the operating system requesting specific services. Generally they are
associated with I/O. To issue an environment call we load register a0 with specific ecall
code.
7
4 Lab Tasks
4.1 Task A
Update the displacement in instructions “ lb dest, displacement(base) ” in the code
given below such that the program prints “SURPRISE”. You are not allowed to change
anything else in the code.
1 . data
2 . byte 7 3 , 4 9 , 5 6 , 85
3 n: . word 1 2 2 , 8 3 , 5 5 , 9 8 , 8 0
4 . a s c i i z ”R”
5 . byte 1 1 4 , 6 9
6 . text
7 . g l o b l main
8 main :
9 la s0 , n
10 a d d i a0 , z e r o , 11
11 lb a1 , 4( s0 )
12 ecall
13 lb a1 , 8( s0 )
14 ecall
15 lb a1 , 12( s0 )
16 ecall
17 lb a1 , 0( s0 )
18 ecall
19 lb a1 , 22( s0 )
20 ecall
21 lb a1 , 4( s0 )
22 ecall
23 lb a1 , 23( s0 )
24 ecall
25 lb a1 , 16( s0 )
26 ecall
27 end :
28 a d d i a0 , z e r o , 10
29 ecall
4.2 Task B
Complete the .text section of program given below, compute the following algebraic equation
and place your result in register a1. Your code must not exceed 20 lines, no register other
than a1, a2, a3, a4, a5 should be used and you are allowed to use variants of addition,
subtraction, shift, load and store instructions only.
Z = (A + B ∗ 8) − {(C + D)/4 + E ∗ 2} + {A/2 − D + (B − E)}
8
1 . data
2 A: . word 240
3 B: . word 50
4 C: . word 80
5 D: . word 32
6 E: . word 100
7 . text
8 . g l o b l main
9 main :
10 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
11 # Your Code Here
12 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
13
14 a d d i a0 , z e r o , 1 # p r i n t i n t e g e r
15 ecall
16 end :
17 a d d i a0 , z e r o , 10
18 ecall
4.3 Task C
Pseudo-instructions are made to create ease for assembly programmers. RISC-V ISA offers
some standard pseudo-instruction which can be used in Venus. Each pseudo-instruction can
be mapped to basic assembly instructions which is done behind the curtains by the assembler.
Write the basic assembly instructions to which the following pseudo-instructions are mapped.
You can take help from RISC-V Reference Card and RISC-V ISA Manual3
4. negate (neg)
Loads destination register with 2’s complement of source.
neg dest, src1
5. move (mv)
Move data from one register to another
mv dest, src1
3
https://content.riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf