or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
| Syntax | Example | Reference | |
|---|---|---|---|
| # Header | Header | 基本排版 | |
| - Unordered List |
|
||
| 1. Ordered List |
|
||
| - [ ] Todo List |
|
||
| > Blockquote | Blockquote |
||
| **Bold font** | Bold font | ||
| *Italics font* | Italics font | ||
| ~~Strikethrough~~ | |||
| 19^th^ | 19th | ||
| H~2~O | H2O | ||
| ++Inserted text++ | Inserted text | ||
| ==Marked text== | Marked text | ||
| [link text](https:// "title") | Link | ||
|  | Image | ||
| `Code` | Code |
在筆記中貼入程式碼 | |
| ```javascript var i = 0; ``` |
|
||
| :smile: | ![]() |
Emoji list | |
| {%youtube youtube_id %} | Externals | ||
| $L^aT_eX$ | LaTeX | ||
| :::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx-
Any changes
Be notified of any changes
-
Mention me
Be notified of mention me
-
Unsubscribe
SubscribeAssignemnt 1:RISC-V Assembly and Instruction Pipeline
contribute By<chihenliu>
Introduction
linkedlist
"linked list" is a common data structure


As shown in the two diagrams above, the concept is to use nodes to record, represent, and store data. Each node has three components: Data, Pointer, and Address. Additionally, each node's pointer points to the address of the next node, continuing until it points to Null, signifying the end of this simple linked list. The time complexity is O(N)
Count leading zero
To calculate the number of consecutive zeros, counting from the Most Significant Bit (MSB) towards the right, until the first encountered '1' in a binary number
Ex: 0000000000000010 =14
Motivation
Before taking this course, I had no prior knowledge of data structures. Linked lists were the first data structure I learned about. Therefore, I wanted to try implementing the 32-bits Count Leading Zeros operation in RISC-V to further understand it。
Implement
C code
First, I defined the structure of a linked list and created a simple linked list with three nodes. Then, I used a 32-bit CLZ (Count Leading Zeros) function to calculate the total sum of leading zeros for the three nodes in the linked list.
case1:
Input list_1:23、15、8
OutPut1:
case2:
Input list_2:10、32、56
Output2:
case3:
Input list_3:89、125、256
Output3:
Table
Assembly Code(RISC-V)
The following is the RISC-V implementation of 32-bits Count Leading Zeros.
I have implemented it as a function.
This function calculates the sum of leading zeros obtained from the CLZ (Count Leading Zeros) operation on three nodes.
Full RISC-V code
For each test case, I will check the count of leading zeros obtained from the CLZ (Count Leading Zeros) operation for each node, and then sum them up
5-stage Pipeline Analysis
5-stage pipeline generated by Ripes


The above is my analysis of the pipeline within the main label
IF stage
This instruction,
jalr x1, x1, 72sets the PC (Program Counter) value to (x1 + 72) and stores the address of the next instruction in thex1register. This operation is typically used for function calls or branchingProgram Counter is 0x00000014, which refers to the next instruction address
The jalr instruction is
I-typeinstructionThis instruction by RISC-V GreenCard table is
R[rd]=PC+4;PC=R[rs1]+immLSB in jalr is set to zero and jalr instructionPCshould beIR+4 if no bracnching occuredID stage
0x0into the registerx1. Similarly, it is used to set the PC value, and this time the address is 0x0, indicating that the entry point of the program is the address of the mainU-typeinstructionR[rd]=PC+{imm,12'b0}Ex stage
The purpose of this instruction is to read the value at the memory address pointed to by
x8and store it in registerx10and use two OP implement
lw x10 0 x8lw is
I-typeinstruction,This instruction by RISC-V GreenCard table isR[rd]={32'bM[](31),M[R[rs1]+imm](31:0)}and Core instruction foramt isimm[11:0],rs1,funct3,rd,opcodeMem stage
addi x8,x8,0isI-typeinstruction,This instruction by RISC-V GreenCard table isR[rd]=R[rs1]+immWB stage
auipcinstruction that loads the immediate value0x10000into registerx8.CPU analysis
Conclusion
This is my first assignment that took me quite a while, constantly working between C language and the RISC-V architecture. Through the CLZ function, it has deepened my understanding of RISC-V instructions, and I have realized my clear shortcomings, requiring more time to enhance my background knowledge in this course. Thanks to my fellow students who discussed with me, it has also made me aware of how complex it can be to recreate a linked list and manage memory in RISC-V
Reference
Assignment1: RISC-V Assembly and Instruction Pipeline
The RISC-V Instruction Set Manual Volume I: Unprivileged ISA
RISC-V Assembly Programmer's Manual
Linked List: Intro
RISC-V Datapath Part4: Pipeline
RISC-V Greensheet
Find first set