Due: March 31, 11:59PM
TA: Junwoo Park (jwpark@csl.korea.ac.kr)
In this assignment, you will implement the bubble sort algorithm in RISC-V (RV32I) assembly language. Pre-built tools are provided in the repository root so you can assemble and run your program without building them yourself:
| Tool | Binary | Description |
|---|---|---|
| Assembler | ./ku-rv32i-as |
Translates .s -> .o |
| Simulator | ./ku-rv32i-sim |
Executes .o and prints register/memory state |
The assembler translates your .s file into a binary .o file.
The simulator executes the binary and shows the final register and memory state.
Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order. After each pass through the array, the largest unsorted element "bubbles" to its final position.
for i = 0 to n-2:
for j = 0 to n-2-i:
if array[j] > array[j+1]:
swap(array[j], array[j+1])
Your program must sort the array in ascending order.
You may use any of the following instructions. No other instructions are available in the provided assembler.
| Format | Instructions |
|---|---|
| R-type | add, sub, and, or |
| I-type | addi, andi, xori, ori, sltiu, slli, srli, lw, jalr |
| S-type | sw |
| B-type | beq, bne, blt, bge |
| J-type | jal |
| U-type | lui, auipc |
| Pseudo | la (expands to auipc + addi) |
Note:
bltandbgeperform signed comparisons. Usesltu/sltiuif you need unsigned comparison.
The assembler imposes the following formatting rules.
- Fields are separated by tabs (not spaces).
- Register names are written as
x0throughx31. - Immediate values may be decimal (e.g.,
4) or hexadecimal (e.g.,0x4). - Labels in the
.textsection must appear on their own line followed by a colon:label:. - Labels in the
.datasection must appear on the same line as their.worddeclaration:array:\t.word\t9. - The
.datasection must come before the.textsection.
# R-type
add xRD, xRS1, xRS2
# I-type (arithmetic)
addi xRD, xRS1, imm
# I-type (load)
lw xRD, offset(xRS1)
# S-type (store)
sw xRS2, offset(xRS1)
# B-type (branch)
blt xRS1, xRS2, label # branch if RS1 < RS2 (signed)
bge xRS1, xRS2, label # branch if RS1 >= RS2 (signed)
beq xRS1, xRS2, label
bne xRS1, xRS2, label
# J-type
jal xRD, label # jump and link
jalr xRD, offset(xRS1) # jump and link register
# Pseudo
la xRD, label # load address (data section label)Implement bubble sort so that after done: is reached, array in memory
is sorted in ascending order.
In addition to sorting, your program must count the total number of swaps performed and store that count in register x20 when the program finishes.
- Initialize
x20 = 0before the loops begin. - Increment
x20by 1 every time you swap two adjacent elements. - The simulator output shows the final register state; the grader will read
x20directly.
Example: for the input
[9, 2, 7, 4, 6], the expected swap count is 6 (stored inx20).
Document every register you use with a short comment explaining its role.
Write these comments inside the existing comment block in main: in
bubble_sort.s (between the two # ------------------------------------------------------- lines),
replacing the TODO text.
Example:
main:
# -------------------------------------------------------
# ...
# x5 : base address of array
# x6 : array length
# ...
# x20 : total number of swaps
# ...
# -------------------------------------------------------The file bubble_sort.s is provided as a starting point.
The .data section is fixed - do not modify it.
Implement the sorting algorithm inside the .text section.
# Name:
# Student ID:
# Description: Bubble sort implementation in RV32I assembly
.data
array: .word 9
.word 2
.word 7
.word 4
.word 6
n: .word 5
.text
main:
# -------------------------------------------------------
# TODO: implement bubble sort
#
# Sort 'array' of 'n' integers in ascending order.
# You may use registers x1-x31 freely.
# Document each register's role with a comment.
# -------------------------------------------------------
done:
jalr x0, 0(x1) # return (do not remove)The
jalr x0, 0(x1)at the end tells the simulator when your program has finished. Do not remove or move it.
ls
# README.md ku-rv32i-as bubble_sort.s ku-rv32i-simIf you get a "Permission denied" error, make the binaries executable:
chmod +x ku-rv32i-as ku-rv32i-simEdit bubble_sort.s and implement the sorting algorithm.
./ku-rv32i-as bubble_sort.sIf successful, bubble_sort.o is created.
Fix any error messages before proceeding.
./ku-rv32i-sim -n 500 bubble_sort.oThe -n 500 flag sets the maximum number of instructions to simulate.
Increase this value if your program needs more instructions to finish.
Inspect the Memory and Registers sections of the simulator output.
Memory (data section starts at 0x10000000):
For the provided input [9, 2, 7, 4, 6], the expected memory content after
sorting is:
[INFO] Memory content [0x10000000..0x1000001f] :
-------------------------------------
0x10000000: 0x00000002 <- array[0]
0x10000004: 0x00000004 <- array[1]
0x10000008: 0x00000006 <- array[2]
0x1000000c: 0x00000007 <- array[3]
0x10000010: 0x00000009 <- array[4]
0x10000014: 0x00000005 <- n (unchanged)
Register x20 (swap count): for [9, 2, 7, 4, 6], the expected value is
0x00000006 (6 swaps total).
Use the -d flag to print the state after every step, which is helpful
for debugging:
./ku-rv32i-sim -d -n 500 bubble_sort.o- Load the array base address with
la xRD, arraybefore accessing elements. - Compute byte offset: element
jis at byte offsetj x 4. Usesllito multiply by 4:slli xOFF, xJ, 2. - Access array[j]:
lw xVAL, 0(xADDR)wherexADDR = base + offset. - Access array[j+1]:
lw xVAL, 4(xADDR)by using the next word. - Swap by storing values in reverse: write
array[j+1]to0(xADDR)andarray[j]to4(xADDR). - Swap counter: initialize
x20to 0 withaddi x20, x0, 0before the loops, then addaddi x20, x20, 1immediately after a swap. - Start with the outer loop and make sure it terminates correctly before adding the inner loop body.
| Item | Points |
|---|---|
| Program assembles without error | 10 |
Part A: Correctly sorts the provided array [9, 2, 7, 4, 6] |
30 |
| Part A: Correctly sorts additional hidden test arrays | 20 |
Part B: Correct swap count in x20 for provided array |
20 |
Part B: Correct swap count in x20 for hidden test arrays |
10 |
| Part C: Comments explaining register usage and logic | 10 |
| Total | 100 |
Hidden test arrays will use the same
.datasection format (same label namesarrayandn) but with different values and sizes (up to 10 elements). Your code must handle any valid input in this format.
Submit only bubble_sort.s to the course submission system.
Do not submit .o files or executables.
- Filename must be exactly
bubble_sort.s - Include your name and student ID as a comment at the top of the file
# Name: Hong Gildong
# Student ID: 2026xxxxxx
# Description: Bubble sort implementation in RV32I assembly