spimB-1

.data
open_bracket: .asciiz "["
close_bracket: .asciiz "] = "
str1: .asciiz "Enter int-number:"
str2: .asciiz "Print"
str3: .asciiz "Enter Array size:"
error: .asciiz "!negative number has entered!"
eol: .asciiz "\n"
.text
.globl main

main:
jal Enter
jal Sort
jal Print

Enter:

# "Enter Array size:"を表示
li $v0, 4
la $a0, str3
syscall

#配列のサイズを指定
li $v0, 5
syscall

bgt $v0, $zero, Enter_num

li $v0, 4
la $a0, error
syscall
la $a0, eol
syscall

j Enter

Enter_num:
move $s1, $v0
sll $a0, $v0, 2
li $v0, 9
syscall

move $s0, $v0
move $t0, $zero


Enter_loop:

#"Enter int-number:" を表示
li $v0, 4
la $a0, str1
syscall

# "[n]= "と出力して、入力された整数を格納
la $a0, open_bracket
syscall
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, close_bracket
syscall
li $v0, 5
syscall


sll $t1, $t0, 2 # $t0 * 4
add $t1, $t1, $s0 # ベースレジスタを追加
sw $v0, 0($t1)


addi $t0, $t0, 1 #オフセットを更新
bne $t0, $s1, Enter_loop #配列が埋まっていなければ入力を続ける

#改行
li $v0, 4
la $a0, eol

syscall

jr $ra


Print:
move $t0, $zero
li $v0, 4
la $a0, str2
syscall
la $a0, eol
syscall

Print_loop:
la $a0, open_bracket
syscall
li $v0, 1
move $a0, $t0
syscall
li $v0, 4
la $a0, close_bracket
syscall

sll $t1, $t0, 2
add $t1, $t1, $s0
li $v0, 1
lw $a0, 0($t1)
syscall
li $v0, 4
la $a0, eol
syscall

addi $t0, $t0, 1

bne $t0, $s1, Print_loop

la $a0, eol
syscall

jr $ra


Sort:
addi $sp, $sp, -20
sw $ra, 16($sp)
sw $s3, 12($sp)
sw $s2, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)

move $s2, $s0
move $s3, $s1

move $s0, $zero
for1tst:
slt $t0, $s0, $s3
beq $t0, $zero, exit1

addi $s1, $s0, -1
for2tst:
slti $t0, $s1, 0
bne $t0, $zero, exit2
sll $t1, $s1, 2
add $t2, $s2, $t1
lw $t3, 0($t2)
lw $t4, 4($t2)
slt $t0, $t4, $t3
beq $t0, $zero, exit2

move $a0, $s2
move $a1, $s1
jal swap

addi $s1, $s1, -1
j for2tst

exit2:
addi $s0, $s0, 1
j for1tst

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:
sll $t1, $a1, 2
add $t1, $a0, $t1

lw $t0, 0($t1) #
lw $t2, 4($t1) #

sw $t2, 0($t1) #
sw $t0, 4($t1) #

jr $ra