我在使用mips的课堂学习集会中。我正在研究一组数字,我认为我的方法正常,但只是有点麻烦。我不知道怎样检查我什么时候完全排序。我使用一种非常基本的方法进行排序,但这就是我们迄今为止学到的所有内容。此外,我不知道如何输出数字来检查它是否已排序。我习惯了Java,所以这样的组装有点让我旋转。这是我到目前为止的代码:
.text
.globl main
main: la $a0, Array # sets the base address of the array to $a0
loop: lw $t0, 0($a0) # sets $t0 to the current element in array
lw $t1, 4($a0) # sets $t1 to the next element in array
blt $t1, $t0, swap # if the following value is greater, swap them
addi $a0, $a0, 4 # advance the array to start at the next location from last time
j loop # jump back to loop so we can compare next two elements
swap: sw $t0, 4($a0) # store the greater numbers contents in the higher position in array (swap)
sw $t1, 0($a0) # store the lesser numbers contents in the lower position in array (swap)
li $a0, 0 # resets the value of $a0 back to zero so we can start from beginning of array
j loop # jump back to the loop so we can go through and find next swap
.data
Array: .word 14, 12, 13, 5, 9, 11, 3, 6, 7, 10, 2, 4, 8, 1
感谢任何帮助人员!
答案 0 :(得分:3)
This link说明如何在QTSPIM或MARS等MIPS模拟器中打印到屏幕。
至于代码,有一些错误。第li $a0, 0
行正在覆盖初始la $a0, Array
指令所完成的工作,因为li
正在将数组的基址设置为0.相反,您应该移动la
指令进入循环,以便在迭代整个数组后$a0
正确地重置为Array
的基址,并删除li
指令。您还需要在程序完成排序时添加条件。我建议进行以下修订(使用SPIM测试):
main:
la $t0, Array # Copy the base address of your array into $t1
add $t0, $t0, 40 # 4 bytes per int * 10 ints = 40 bytes
outterLoop: # Used to determine when we are done iterating over the Array
add $t1, $0, $0 # $t1 holds a flag to determine when the list is sorted
la $a0, Array # Set $a0 to the base address of the Array
innerLoop: # The inner loop will iterate over the Array checking if a swap is needed
lw $t2, 0($a0) # sets $t0 to the current element in array
lw $t3, 4($a0) # sets $t1 to the next element in array
slt $t5, $t2, $t3 # $t5 = 1 if $t0 < $t1
beq $t5, $0, continue # if $t5 = 1, then swap them
add $t1, $0, 1 # if we need to swap, we need to check the list again
sw $t2, 4($a0) # store the greater numbers contents in the higher position in array (swap)
sw $t3, 0($a0) # store the lesser numbers contents in the lower position in array (swap)
continue:
addi $a0, $a0, 4 # advance the array to start at the next location from last time
bne $a0, $t0, innerLoop # If $a0 != the end of Array, jump back to innerLoop
bne $t1, $0, outterLoop # $t1 = 1, another pass is needed, jump back to outterLoop
请务必查看this link以获取有关每条MIPS指令的其他示例和解释。
答案 1 :(得分:1)
直接编组是一种痛苦。我所做的是从一个算法(伪代码或实际代码)开始,然后系统地翻译,就好像我是一个编译器。我会忽略输入和输出的东西,并专注于一个排序的函数。
你会像C一样打电话给高级别的语言:
insertionsort(data, N);
其中data
是整数数组,N
是元素数(在机器级别没有大小属性)。
由于该函数不调用任何内容,因此不需要堆栈帧。遵守使用$t
寄存器的标准MIPS约定,这样您就不会破坏任何其他人依赖的内容,并按顺序在$a0
和$a1
中传递参数。
第1步:获取算法。这是来自[Wikipedia] [1]的插入排序:
i ← 1
while i < length(A)
x ← A[i]
j ← i - 1
while j >= 0 and A[j] > x
A[j+1] ← A[j]
j ← j - 1
end while
A[j+1] ← x
i ← i + 1
end while
第2步:粘贴到文本文件中,将所有行转换为注释并系统地转换为汇编。我使用模板来完成诸如循环之类的事情,这些模型可以帮助我完成编码(参见我的free book示例)。我会在这里给出成品;要调用它,您需要将数组的起始地址放入$a0
,其大小放在$a1
中,然后jal insertionsort
:
# algorithm for insertion sort
# from https://en.wikipedia.org/wiki/Insertion_sort
# usage: insertionsort (a,N)
# pass array start in $a0, size in elements, N in $a1
# Philip Machanick
# 30 April 2018
.globl insertionsort
.text
# leaf function, no stack frame needed
# Registers:
# $a0: base address; $a1: N
# $t0: i
# $t1: j
# $t2: value of A[i] or A[j]
# $t3: value of x (current A[i])
# $t4: current offset of A[i] or A[j] as needed
insertionsort:
# i ← 1
li $t0, 1
# while i < N
j Wnext001 # test before 1st iteration
Wbody001: # body of loop here
sll $t4, $t0, 2 # scale index i to offset
add $t4, $a0, $t4 # address of a[i]
# x ← A[i]
lw $t3, 0($t4)
# j ← i - 1
addi $t1, $t0, -1
# while j >= 0 and A[j] > x
j Wnext002 # test before 1st iteration
Wbody002: # body of loop here
# A[j+1] ← A[j]
sll $t4, $t1, 2 # scale index j to offset
add $t4, $a0, $t4 # address of a[j]
lw $t2, 0($t4) # get value of A[j]
addi $t4, $t4, 4 # offset of A[j+1]
sw $t2, 0($t4) # assign to A[j+1]
# j ← j - 1
addi $t1, $t1, -1
# end while
Wnext002: # construct condition, j >= 0 and A[j] > x
blt $t1, $zero Wdone002 # convert to: if j < 0 break from loop #####
sll $t4, $t1, 2 # scale index j to offset
add $t4, $a0, $t4 # address of a[j]
lw $t2, 0($t4) # A[j]
bgt $t2, $t3, Wbody002 # no need to test j >= 0, broke from loop already if false
Wdone002: # branch here to short-circuit and
# A[j+1] ← x
add $t4, $t1, 1 # scale index j+1 to offset
sll $t4, $t4, 2 # scale index j to offset
add $t4, $a0, $t4 # address of a[j+1]
sw $t3, 0($t4) # A[j+1] becomes x
# i ← i + 1
addi $t0, $t0, 1
# end while
Wnext001: blt $t0,$a1, Wbody001 # i < N easy this time
jr $ra # return to caller
比其他示例更长 - 但如果从算法开始并翻译,则不太可能出错。如果汇编程序遵守.globl
指令,这可以放在一个单独的文件中,这使得该名称在其他文件中可见。
[1]:https://en.wikipedia.org/wiki/Insertion_sort - 实际上来自Cormen,Thomas H。; Leiserson,Charles E。; Rivest,Ronald L。; Stein,Clifford(2001)。 &#34;第2.1节:插入排序&#34;。算法简介(第2版)。麻省理工学院出版社和McGraw-Hill。第15-21页
答案 2 :(得分:-1)
。数据
值:.word 0x3
数组:.word 0x14,0x12,0x13,0x05
的.text
.globl main
main:la $ a0,Array
lw $t3,value
l1:lw $ t0,0($ a0)
lw $t1, 4($a0)
blt $t1, $t0, swap
addi $a0, $a0, 4
addi $t3,$t3,-1
bne $t3,$zero,l1
jr $ra
交换:sw $ t0,4($ a0)
sw $t1, 0($a0)
j l1