Lab01 Riscv Ict
Lab01 Riscv Ict
Goals
After this lab session, you will be able to use the RARS tool, write a simple assembly
program, simulate the execution of the program, and debug errors (if any). You will also
have a general understanding of the operation of a RISC-V processor when it executes
instructions.
References
- RISC-V documents, lecture notes.
- The RISC-V Instruction Set Manual: riscv-spec-20191213.pdf
About RARS
- Website: https://github.com/TheThirdOne/rars
- RARS version 1.6:
https://github.com/TheThirdOne/rars/releases/download/v1.6/rars1_6.jar
Kick-off
1
Hanoi University of Science and Technology
School of Information and Communications Technology
2
Hanoi University of Science and Technology
School of Information and Communications Technology
4. Edit/Execute:
Each source code file in the editing interface has two windows (2 tabs): Edit and
Execute.
o Edit tab: Write assembly programs with syntax highlighting.
o Execute tab: Compile the assembly program written in the Edit tab
into machine code, run it, and debug.
5. Message Areas: There are two windows at the bottom of the IDE interface.
o Run I/O is only active when running the program.
▪ It displays results output to the console.
▪ It inputs data into the program via the console.
The RARS tool has an option for all console input data to be displayed
again in the message area.
o Messages is used to display all other notifications, such as error
messages during compilation or while running the program. Click on
an error message to automatically jump to the line causing the error.
6. Registers: The table displays the values of the processor's registers, always
visible regardless of whether the assembly program is running or not. This
table also helps you remember the names and IDs of the registers when writing
programs. There are three tabs in this table:
o General-Purpose Registers are numbered from 0 to 31, including the
Program Counter register and can be used in any of the instructions.
o Floating Point Registers are used for performing floating-point
arithmetic instructions.
o Control and Status Registers are used for interrupt handling.
2. In the menu bar, select File / New to create a new assembly file.
3
Hanoi University of Science and Technology
School of Information and Communications Technology
5. To compile the assembly program into machine code, do one of the following:
- Go to Run / Assemble in the menu.
6. If the assembly code is correct, the RARS tool will switch from the Edit tab to the
Execute tab.
Note: If the assembly code has an error, the Messages window will display the details
of the error. Click on the error message line, and the editor will automatically jump to
the line of the error in the code, allowing you to correct it.
7. In the Execute tab, there are two main windows: Text Segment and Data
Segment.
4
Hanoi University of Science and Technology
School of Information and Communications Technology
• Text Segment is the memory space that contains the assembly code. In
the assembly source code, any code written after the .text directive
belongs to the Text Segment.
• Data Segment is the memory space that contains variables. In the
assembly source code, any code written after the .data directive belongs
to the Data Segment.
Note: For some reason, if you declare a variable after the .text directive or vice versa,
the compiler will either report an error or ignore the incorrect declaration.
8. In the Execute tab, use the checkboxes below to change the data display format for
easier observation.
• : Displays the address in the hexadecimal format.
• : Displays the register value in the hexadecimal
format.
• : Displays the value in memory as ASCII characters.
5
Hanoi University of Science and Technology
School of Information and Communications Technology
9. In the Execute tab, within the Text Segment window, the table has five columns:
• Bkpt: Breakpoints, the stopping points when running the entire program
using the button.
• Address: The address of the instruction in the integer format (see more
instructions in the Label window).
• Code: The instruction in the machine code format.
• Basic: The instruction in assembly language, as specified in the instruction
set. Here, all labels, mnemonics, etc., have been converted into constants.
• Source: The instruction in assembly language with additional macros,
labels, etc., to help write a program faster and more understandable. For
example:
i. The la instruction in the Source column is a pseudo-instruction, not
part of the instruction set, and is translated into two corresponding
instructions, auipc and addi in the Basic column.
ii. The msg label in the la a0, msg instruction in the Source column
is replaced by parameters for the auipc and addi instructions.
10. In the Execute tab, within the Data Segment window, the table has nine columns:
• Address: The address of the data or variable in the integer format. The
value of each row increases by 32 in decimal or 0x20 in hexadecimal
because each row presents 32 bytes at consecutive addresses.
• Value columns: Each column contains 4 bytes, and there are 8 columns,
corresponding to 32 consecutive bytes.
6
Hanoi University of Science and Technology
School of Information and Communications Technology
In the image above, the value of the variable x = 0x01020304 is displayed correctly
in the Data Segment with the number format , and the value of the string
“Truong Cong nghe thong tin va Truyen thong” is displayed correctly with the
ASCII format . Note that storing strings in memory in the little-endian
format is due to the way the software syscall function is programmed, not a
requirement of the MIPS processor. As can be seen, the RARS tool stores strings in
the big-endian format.
11. The Label window displays the label names and the corresponding address constants
when compiled into machine code. The Label window does not display
automatically. In the menu, go to the Settings / Show Labels Window to toggle the
display of the Label window.
7
Hanoi University of Science and Technology
School of Information and Communications Technology
2. Use the slider bar to adjust the execution speed of the instructions.
By default, the execution speed is set to maximum, and at this speed, it’s difficult
to intervene much in the operation of the instructions and control them. However,
you can move the slider bar to around one or two instructions/second for easier
observation.
8
Hanoi University of Science and Technology
School of Information and Communications Technology
• Click the Run icon to execute the entire program. When using Run, you can
observe the line highlighted in yellow, indicating where the assembly program is
being processed. Simultaneously, you can observe the data changes in the Data
Segment window and the Registers window.
• Click the Reset icon to restart the emulator to its initial state. All memory
blocks and registers are reset to 0.
• Click the Run one step icon to execute just one instruction and then wait to
click on the icon again to execute the next instruction.
• Click Run one step backwards icon to restore the state and go back to the
previously executed instruction.
• After running all the instructions of the Hello World program, you will see the
Run I/O window display the string “Truong Cong nghe thong tin va Truyen
thong”.
9
Hanoi University of Science and Technology
School of Information and Communications Technology
Consulting Help
10
Hanoi University of Science and Technology
School of Information and Communications Technology
There are many other simulation and emulation tools, such as:
1. Ripes, used to simulate code written in C or assembly language on a RISC-V
processor with the RV32I[M][C] instruction set. Available on both Web and
PC. (See details)
11