Skip to content

Commit 58673de

Browse files
Bubble sort - partial
1 parent b3f1df8 commit 58673de

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

bubble_sort.asm

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# implement bubble sort using the given C code as template
2+
3+
# int main()
4+
# {
5+
# int Sz = 10;
6+
# int List[10] = {17, 5, 92, 87,41, 10, 23, 55, 72, 36} ;
7+
# int Stop, Curr, Next, Temp;
8+
# for (Stop = Sz-1; Stop > 0; Stop){
9+
# for (Curr = 0; Curr < Stop; Curr++){
10+
# Next = Curr + 1;
11+
# if (List[Curr] > List[Next]){
12+
# Temp = List[Curr];
13+
# List[Curr] = List[Next];
14+
# List[Next] = Temp;
15+
# }
16+
# }
17+
# }
18+
# printf("Sorted list in ascending order:\n");
19+
# for (Curr = 0; Curr < Stop; Curr++)
20+
# printf("%d\n", List[Curr]);
21+
# }
22+
# NOTE: Incomplete code. There's some bug in this.
23+
24+
.data
25+
26+
list: .word 17, 5, 92, 87, 41, 10, 23, 55, 72, 36
27+
listSize: .word 10
28+
msg: .asciiz "Sorted list is: "
29+
welcomeMsg: .asciiz "Original list is: "
30+
space: .asciiz " "
31+
endline: .asciiz "\n"
32+
33+
.text
34+
35+
li $v0, 4
36+
la $a0, welcomeMsg
37+
syscall
38+
39+
jal print_array
40+
41+
li $v0, 4
42+
la $a0, endline
43+
syscall
44+
45+
la $a0, list
46+
lw $t0, listSize
47+
addi $t0, $t0, -1 # Stop
48+
49+
loop1:
50+
ble $t0, $zero, out1
51+
li $t1, 0 # curr
52+
loop2:
53+
bge $t1, $t0, out2
54+
addi $t2, $t1, 1 # next
55+
sll $t3, $t1, 2
56+
add $t3, $t3, $a0 # &list[curr]
57+
sll $t4, $t2, 2
58+
add $t4, $t4, $a0 # &list[next]
59+
lw $t5, ($t3) # list[curr]
60+
lw $t6, ($t4) # list[next]
61+
ble $t5, $t6, endif
62+
sw $t5, ($t4)
63+
sw $t6, ($t3)
64+
endif: addi $t1, $t1, 1
65+
addi $t1, $t1, 1
66+
j loop2
67+
out2:
68+
addi $t0, $t0, -1
69+
j loop1
70+
out1:
71+
72+
li $v0, 4
73+
la $a0, msg
74+
syscall
75+
jal print_array
76+
exit:
77+
li $v0, 10
78+
syscall
79+
80+
print_array:
81+
82+
lw $t0, listSize
83+
la $a2, list
84+
li $t1, 0
85+
86+
print_sorted:
87+
beq $t1, $t0, return
88+
lw $a0, ($a2)
89+
li $v0, 1
90+
syscall
91+
li $v0, 4
92+
la $a0, space
93+
syscall
94+
addi $a2, $a2, 4
95+
addi $t1, $t1, 1
96+
j print_sorted
97+
98+
return:
99+
jr $ra

0 commit comments

Comments
 (0)