使用冒泡排序进行测试:
.org 0x0
.global _start
.set noat
_start:
ori $t1,$zero,0x0010 # 起始地址为16 a
ori $6,$0,0x0050 # 用来标志获取数据的地址
ori $t2,$zero,0x0002 # 源操作数,20210627
sw $t2,0x0($t1)
ori $t2,$zero,0x0000
sw $t2,0x4($t1)
ori $t2,$zero,0x0002
sw $t2,0x8($t1)
ori $t2,$zero,0x0001
sw $t2,0xc($t1)
ori $t2,$zero,0x0000
sw $t2,0x10($t1)
ori $t2,$zero,0x0006
sw $t2,0x14($t1)
ori $t2,$zero,0x0002
sw $t2,0x18($t1)
ori $t2,$zero,0x0007
sw $t2,0x1c($t1)
ori $s2,$zero,0x0001 # 用于相减的常量
ori $t2,$zero,0x0008 # 数组大小 $t2 为 n
ori $1,$zero,0x0000 # 赋初值 i = 0
loop_i:
ori $2,$zero,0x0000 # 赋值为 j = 0
subu $s1,$t2,$1 # 每次j的范围为 n - i - 1
subu $s1,$s1,$s2 # $a1 = n-i-1
loop_j:
sync
sll $t5,$2,2 # 使 i 左移两位,得 $t5 = j * 4
add $t3,$t5,$t1 # 得 $t3 为 相对$t1 偏移后的地址,为第 j 个数据的地址
lw $3,0x0($t3) # $3 = a[i]
lw $4,0x4($t3) # $4 = a[j+1]
sub $t7,$3,$4 # $t7 = a[j] - a[j+1] ,当$t7大于0时,说明a>b,则转移
bgtz $t7,ifs # 当a<b时,$t7=0(默认的),a>b时则转移
sync
back:
ori $5,$0,0x0000
sw $5,0x0($6) # 当地址为 0x0050时,表示完成了一轮运算
addi $2,$2,0x0001 # j = j + 1
subu $t5,$s1,$2 # $t5存放的是 n-i-1-j
bgtz $t5,loop_j # n > j 则回到loop_j
sync
addi $1,$1,0x0001 # i = i + 1
subu $t5,$t2,$1 # $t5存放是 n - i
bgtz $t5,loop_i # n > i 则回到loop_i
sync
j resualt
sync
ifs:
sw $3,0x4($t3) # 将a[j]存放到原本a[j+1]的地址中
sw $4,0x0($t3)
ori $t7,$0,0x0000 # 将$t7恢复为0
ori $5,$0,0x0001 # 当$5为1时,则表示进行了转移
j back # 返回到循环继续执行
sync
resualt:
j resualt
sync
输入数据,依次读入20210627,sort.data文件
34090010
34060050
340a0002
ad2a0000
340a0000
ad2a0004
340a0002
ad2a0008
340a0001
ad2a000c
340a0000
ad2a0010
340a0006
ad2a0014
340a0002
ad2a0018
340a0007
ad2a001c
34120001
340a0008
34010000
34020000
01418823
02328823
0000000f
00026880
01a95820
8d630000
8d640004
00647822
1de00011
00000000
0000000f
34050000
acc50000
20420001
02226823
1da0fff2
00000000
0000000f
20210001
01416823
1da0ffea
00000000
0000000f
08000036
00000000
0000000f
ad630004
ad640000
340f0000
08000021
34050001
0000000f
08000036
00000000
0000000f
排序结果保存到数据存储器里