.text
.globl main
main:
la $a0, str_1
li $v0, 4
syscall
li $v0, 5
syscall
move $s0, $v0
la $a0, str_2
li $v0, 4
syscall
move $a0, $gp
move $a1, $s0
jal read
li $v0, 4
la $a0, str_5
syscall
move $a0, $gp
move $a1, $s0
jal prinf
move $a0, $gp
move $a1, $s0
jal sort
li $v0, 4
la $a0, str_3
syscall
move $a0, $gp
move $a1, $s0
jal prinf
read:
addi $sp, $sp, -4
sw $s0, 0($sp)
li $s0, 0
read_1:
sltu $t0, $s0, $a1
beq $t0, $zero, exit_1
sll $t0, $s0, 2
add $t1, $a0, $t0
move $t2, $a0
li $v0, 5
syscall
sw $v0, 0($t1)
move $a0, $t2
addi $s0, $s0, 1 # s0++
j read_1
exit_1:
lw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra
sort:
addi $sp, $sp, -20
sw $ra, 16($sp)
sw $s3, 12($sp)
sw $s2, 8($sp)
sw $s1, 4($sp) # j
sw $s0, 0($sp) # i
move $s2, $a0
move $s3, $a1
move $s0, $zero
forOut:
slt $t0, $s0, $s3
beq $t0, $zero, exit1
addi $s1, $s0, -1 # j = i – 1
forIn:
slti $t0, $s1, 0
bne $t0, $zero, exit2
sll $t1, $s1, 2 # $tl = j*4
add $t2, $s2, $t1
lw $t3, 0($t2)
lw $t4, 4($t2)
slt $t0, $t3, $t4
beq $t0, $zero, exit2
move $a0, $s2
move $a1, $s1
jal swap
addi $s1, $s1, -1 # j–
j forIn
exit2:
addi $s0, $s0, 1 # i++
j forOut
exit1:
lw $s0, 0($sp)
lw $s1, 4($sp)
lw $s2, 8($sp)
lw $s3, 12($sp)
lw $ra, 16($sp)
addi $sp, $sp, 20
jr $ra
swap:
srl $t0, $a1, 2
add $t0, $a0, $t0
lw $t1, 0($t0)
lw $t2, 4($t0)
sw $t1, 4($t0) # arr[j]=arr[j+1]
sw $t2, 0($t0) # arr[j+1]=arr[j]
jr $ra
prinf:
addi $sp, $sp, -4
sw $s0, 0($sp)
li $s0, 0
prinf_1:
sltu $t0, $s0, $a1
beq $t0, $zero, exit_2
sll $t0, $s0, 2
add $t1, $a0, $t0
move $t2, $a0
lw $a0, 0($t1)
li $v0, 1
syscall
li $a0, ‘,’
li $v0, 11
syscall
move $a0, $t2
addi $s0, $s0, 1
j prinf_1
exit_2:
lw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra
.data
str_1:
.asciiz “How many number that you want to enter:\n”
str_2:
.asciiz “Enter the numbers:\n”
str_3:
.asciiz “\nAfter sort:\n”
str_5:
.asciiz “You entered:\n”