我正在尝试在汇编中使用bubble sort
对六个数组进行排序
数组可以正确排序,但是我在使用堆栈指针时遇到了麻烦
我使用堆栈指针来存储原始数组,因为我需要在排序之前知道原始位置。但是当我尝试在循环函数中加载lw $ t0,0($ sp)时, $ t0 给出了 0 ,有什么问题>?,以及如何访问堆栈指针中的每个元素
但是,更改后,我可以正确加载$ sp
lw $ t0,-24($ sp)到此循环函数,为什么我不能使用0($ sp)检索值?
loops: ##this part causing trouble
lw $t0,0($sp) ##but when jump to this, the stack pointer give 0 in $t0 why?
addi $sp,$sp,4
addi $t7,$t7,1
beq $t8,$t0,koko
j loops
.text
.globl main
main:
la $t1,array ##copy base address of array to t1
lw $t0,num ##copy num to t0
lw $s3, num ##stop =6
##lw $t2,0($t1)
li $v0,4
la $a0,prompt
syscall
li $v0,5
syscall
move $s0,$v0 ##user input
addi $t6,$0,0
sub $t9,$s3,$s0 ## 6-3
addi $t3,$t3,1
addiu $sp,$sp,-32 ## here im made stack,
jal shokika
j loops ##go to here after return from end:
shokika: ##use to store original array
lw $t2,0($t1)
bge $t6,6,outer
sw $t2,0($sp)
addi $t1,$t1,4
addi $sp,$sp,4
addi $t6,$t6,1
j shokika
loops:
lw $t0,0($sp) ##but when jump to this, the stack pointer give 0 in $t0 why?
addi $sp,$sp,4
addi $t7,$t7,1
beq $t8,$t0,koko
j loops
outer:
la $t1,array
la $sp,0($sp)
addi $s3,$s3,-1 ##5
ble $s3,$zero,end
li $s0,0 ##inner loop counter
li $s1,0 ##outer counter
inner:
bge $s0,$s3,inner_end
lw $t3,0($t1) ##cur elem
lw $t4,4($t1) ## next el
addi $s0,$s0,1
ble $t4,$t3, swap
addi $t1,$t1,4
j inner
inner_end:
la $t1,array
j outer
swap:
sw $t3,4($t1)
sw $t4,0($t1)
addi $t1,$t1,4
j inner
end:
addi $t5,$0,4
mul $t5,$t5,$t9 ##4*3
add $t9,$t5,$t1 ##t1 base address arr
lw $t8,0($t9) ##sort
jr $ra
koko:
li $v0,4
la $a0,result
syscall
li $v0,1
move $a0,$t7 ##position
syscall
li $v0,4
la $a0,sort
syscall
li $v0,1
move $a0,$t8 ##psort
syscall
li $v0, 10
syscall
.data
array:
.word 10,5,3,15,21,14
array2:
.space 400
num:
.word 6
sort: .asciiz "$s2->"
result: .asciiz "$s1->"
prompt: .asciiz "$s0->"