-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.asm
60 lines (48 loc) · 1.34 KB
/
bubble.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
.data
arr: .word 10, 60, 40, 70, 20, 30, 90, 100, 0, 80, 50
space: .asciiz " "
.text
.globl main
main:
la $s0, arr # arr[0]
addi $t0, $zero, 0 # i = 0
addi $t1, $zero, 0 # j = 0
addi $s1, $zero, 11 # n = 11
addi $s2, $zero, 11 # n-i for inner loop
add $t2, $zero, $s0 # for iterating addr by i
add $t3, $zero, $s0 # for iterating addr by j
addi $s1, $s1, -1
outer_loop:
addi $t1, $zero, 0 # j = 0
addi $s2, $s2, -1 # decreasing size for inner_loop
add $t3, $zero, $s0 # resetting addr itr j
inner_loop:
lw $s3, 0($t3) # arr[j]
addi $t3, $t3, 4 # addr itr j += 4
lw $s4, 0($t3) # arr[j+1]
addi $t1, $t1, 1 # j++
slt $t4, $s3, $s4 # set $t4 = 1 if $s3 < $s4
bne $t4, $zero, cond
swap:
sw $s3, 0($t3)
sw $s4, -4($t3)
lw $s4, 0($t3)
cond:
bne $t1, $s2, inner_loop #j != n-i
addi $t0, $t0, 1 # i++
bne $t0, $s1, outer_loop # i != n
addi $t0, $zero, 0
addi $s1, $s1, 1
print_loop:
li $v0, 1
lw $a0, 0($t2)
syscall
li $v0, 4
la $a0, space
syscall
addi $t2, $t2, 4 # addr itr i += 4
addi $t0, $t0, 1 # i++
bne $t0, $s1, print_loop # i != n
exit:
li $v0, 10
syscall