我想传递一个列表以在mips中起作用,以便我可以对此进行处理,但是我的分配禁止移动指令
试图传递地址,但是它们都出错了
addi $fp, $sp, 0
addi $sp, $sp, -4
la $t0, thelist
sw $t0, ($sp)
jal bubblesort
bubblesort:
#call function
addi $sp, $sp, -8 #push 8 bytes to store $fp and $ra
sw $ra, 4($sp)
sw $fp, ($sp)
#copy $sp to $fp
addi $fp, $sp, 0
#allocate local variable
addi $sp, $sp, -16 #i, j, listaddress, size
#initialize i j n
lw $t0, 8($fp)
sw $t0, -12($fp) # listaddress
lw $t1, size
sw $0, -8($fp) #i = 0
sw $0, -4($fp) #j = 0
loop1:
#condition
lw $t0, -8($fp) #load i
lw $t1, size #the actual n
addi $t1, $t1, -1 #n = n - 1
beq $t0, $t1, end #if i == n-1, jump to end
loop2:
lw $t0, -4($fp) #load j
lw $t1, size #load n
addi $t1, $t1, -1 #n = n - 1
beq $t0, $t1, exit1 #if j == n-1, jump to exit1
#swap section
lw $t1, thelist #$t1 = list address
sll $t0, $t0, 2 #j = j*4
add $t3, $t1, $t0 #address of list[j]
lw $t4, ($t3) #list[j]
lw $t5, 4($t3) #list[j+1]
slt $t2, $t5, $t4
beq $t2, $0, exit2 #if list[j] < list[j+1], jump to loop2
#swap
add $t2, $t5, 0 #temp = list[j+1]
add $t5, $t4, 0 #list[j+1] = list[j]
add $t4, $t2, 0 #list[j] = temp
sw $t5, ($t3)
sw $t6, 4($t3)
lw $t0, -4($fp)
addi $t0, $t0, 1 #j = j+1
sw $t0, -4($fp)
j loop2
exit2:
lw $t0, -4($fp)
addi $t0, $t0, 1 #j = j+1
sw $t0, -4($fp)
j loop2
exit1:
lw $t0, -8($fp)
addi $t0, $t0, 1
sw $t0, -8($fp)
j loop1
end:
lw $v0, -12($fp)
addi $sp, $sp 12
lw $fp, ($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
答案 0 :(得分:0)
实际上,您可以在函数内访问$ t寄存器。之前无需将其存储在堆栈中。例如,如果将列表加载到$ t10,则可以在函数内部轻松访问它。另外:您可以始终使用添加来代替移动。我无法运行您的代码,因为它不是完整的示例。您的列表如何存储?