Assembly 快速排序程序集MIPS
到目前为止,我已经有了这个,但是它打印出了所有的4个。而且它没有被分类Assembly 快速排序程序集MIPS,assembly,mips,quicksort,Assembly,Mips,Quicksort,到目前为止,我已经有了这个,但是它打印出了所有的4个。而且它没有被分类 # Quicksort.asm .data array: .word 4, 9, 12, -5, 2, 54, 8 , 1 line: .asciiz " " .text main: addi $sp, $sp, -4 # Push the stack pointer down to hold one value sw $ra, 0 ($sp)
# Quicksort.asm
.data
array: .word 4, 9, 12, -5, 2, 54, 8 , 1
line: .asciiz " "
.text
main:
addi $sp, $sp, -4 # Push the stack pointer down to hold one value
sw $ra, 0 ($sp) # Store the return address on the stack
la $a0, array # Array into a0
addi $a1, $zero, 0 # Left most element (p)
addi $a2, $zero, 7 # Right most element (r)
jal quicksort
addi $t0, $zero, 0 # Counter loop
addi $t1, $zero, 8 # where the loop ends
la $t7, array # loads base address of array into t7
topprintloop:
beq $t0, $t1, endprintloop
sll $t3, $t0, 2 # calculates j * 4
addi $t3, $t7, 0 # Calculates the address of array[j]
lw $t4, 0($t3) # load value at array[j]
addi $v0, $zero, 1 # sets v0 to 1 for syscall
addi $a0, $t4, 0 # load int to print into a0
syscall # prints it
addi $v0, $zero, 4 # sets v0 to 4 for syscall
la $a0, line # loads string to print into a0
syscall # prints it
addi $t0, $t0, 1 # increment counter by 1
j topprintloop # jumps to topprintloop
endprintloop:
lw $ra, 0 ($sp) # Restore the return address from the stack
addi $sp, $sp, 4 # Pop the stack
jr $ra
quicksort:
addi $sp, $sp, -20 # Store it to the stack
sw $ra, 0 ($sp)
sw $s0, 4 ($sp)
sw $s1, 8 ($sp)
sw $s2, 12 ($sp)
sw $s3, 16 ($sp)
addi $s0, $a0, 0 # store into s0 array pointer
addi $s1, $a1, 0 # store into s1 left most element (p)
addi $s2, $a2, 0 # store into s2 right most element (r)
# store into s3 q
blt $s1, $s2, prep # jumps and links to prep if p < r
j returnQuicksort # jumps to returnQuicksort when finished
prep:
addi $a0, $s0, 0 # store into s0 array pointer
addi $a1, $s1, 0 # store into s1 left most element (p)
addi $a2, $s2, 0 # store into s2 right most element (r)
jal partition
addi $s3, $v0, 0 # stores the partition value of v0 and puts it into q
# First recursive call
addi $a0, $s0, 0 # store into a0 new left subarray
addi $a1, $s1, 0 # [p]
addi $a2, $s3, -1 # [q-1]
jal quicksort
# Second recursive call
addi $a0, $s0, 0 # store into a0 new right subarray
addi $a1, $s3, 1 # [q+1]
addi $a2, $s2, 0 # [r]
jal quicksort
returnQuicksort:
lw $ra, 0 ($sp) # pop the stack
lw $s0, 4 ($sp)
lw $s1, 8 ($sp)
lw $s2, 12 ($sp)
lw $s3, 16 ($sp)
addi $sp, $sp, 20
jr $ra
partition:
addi $sp, $sp, -28 # Push stack pointer
sw $ra, 0 ($sp)
sw $s0, 4 ($sp)
sw $s1, 8 ($sp)
sw $s2, 12 ($sp)
sw $s3, 16 ($sp)
sw $s4, 20 ($sp)
sw $s5, 24 ($sp)
addi $s0, $a0, 0 # Array pointer
addi $s1, $a1, 0 # P value
addi $s2, $a2, 0 # R value
sll $t0, $s2, 2 # * 4 into t0
add $t1, $t0, $s0
lw $s3, 0($t1) # Pivot value
addi $s4, $s1, -1 # value i
addi $s5, $s1, 0 # value j
beginloop:
bge $s5, $s2, endloop # j < r
sll $t0, $s5, 2 # * 4 into t0
add $t1, $t0, $s0
lw $s3, 0($t1) # Pivot value
sll $t2, $s5, 2
add $t2, $s0, $t2
lw $t2, 0($t2)
ble $t2, $s3, doloop
j loop
doloop:
sll $t0, $s5, 2 # calculates j * 4
addi $t0, $s0, 0 # Calculates the address of array[j]
lw $t1, 0($t0) # load value at array[j]
ble $t1, $s3, partitionswap # branch if array[j] <= pivot
j loop # skip partition swap if condition fails and go to loop
partitionswap:
addi $s4, $s4, 1 #increment i
addi $a0, $s0, 0 # load array into a0
addi $a1, $s4, 0 # load i into a1
addi $a2, $s5, 0 # load j into a2
jal swap # we swap
j loop
loop:
addi $s5, $s5, 1 # increments j (j++)
j beginloop
endloop:
# call swap
addi $t0, $s4, 1 # i+1
addi $a0, $s0, 0
addi $a1, $t0, 0
addi $a2, $s2, 0
jal swap
addi $t0, $s4, 1
addi $v0, $t0, 0
lw $ra, 0 ($sp)
lw $s0, 4 ($sp)
lw $s1, 8 ($sp)
lw $s2, 12 ($sp)
lw $s3, 16 ($sp)
lw $s4, 20 ($sp)
lw $s5, 24 ($sp)
addi $sp, $sp, 28 # Push stack pointer
jr $ra
swap:
addi $sp, $sp, -28 # Push the stack pointer down to hold seven values
sw $ra, 0 ($sp) # Store the return address and s registers on the stack
sw $s0, 4 ($sp)
sw $s1, 8 ($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $s4, 20($sp)
sw $s5, 24($sp)
# Done with protecting registers. Now for the real work:
sll $s0, $a1, 2 # from * 4 into $s0
sll $s1, $a2, 2 # to * 4 into $s1
add $s2, $a0, $s0 # Address of array[from] into $s2
add $s3, $a0, $s1 # Address of array[ to] int9 $s3
lw $s4, 0($s2) # Value of array[from] into $s4
lw $s5, 0($s3) # Value of array[to] into $s5
sw $s4, 0($s3) # Value from $s4 into array[to]
sw $s5, 0($s2) # Value from $s5 into array[from]
# Done with the work. No return value.
lw $ra, 0 ($sp) # Restore the $ra and $s registers from the stack
lw $s0, 4 ($sp)
lw $s1, 8 ($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $s4, 20($sp)
lw $s5, 24($sp)
addi $sp, $sp, 28 # Pop the stack pointer
jr $ra
#Quicksort.asm
.数据
数组:。字4、9、12、-5、2、54、8、1
行:.asciiz“”
.文本
主要内容:
addi$sp,$sp,-4#向下按堆栈指针以保持一个值
sw$ra,0($sp)#将返回地址存储在堆栈上
la$a0,数组#数组为a0
addi$a1,$0,0#最左侧元素(p)
addi$a2,$0,7#最右边的元素(r)
日航快速排序
addi$t0,$0,0#计数器循环
addi$t1,$0,8#循环结束的地方
la$t7,数组#将数组的基址加载到t7中
topprintloop:
beq$t0,$t1,endprintloop
sll$t3,$t0,2#计算j*4
addi$t3,$t7,0#计算数组[j]的地址
lw$t4,0($t3)#阵列处的加载值[j]
addi$v0,$0,1#将系统调用的v0设置为1
addi$a0,$t4,0#加载int以打印到a0
syscall#打印它
addi$v0$0,4#将系统调用的v0设置为4
la$a0,第#行加载要打印到a0中的字符串
syscall#打印它
addi$t0,$t0,1#递增计数器1
j topprintloop#跳到topprintloop
endprintloop:
lw$ra,0($sp)#从堆栈还原返回地址
addi$sp,$sp,4#弹出堆栈
jr$ra
快速排序:
addi$sp,$sp,-20#将其存储到堆栈中
西南$ra,0($sp)
sw$s0,4($sp)
sw$s1,8($sp)
sw$s2,12($sp)
sw$s3,16($sp)
addi$s0,$a0,0#存储到s0数组指针中
addi$s1,$a1,0#存储到s1最左侧的元素(p)
addi$s2,$a2,0#存储到s2最右边的元素(r)
#存储到s3q中
blt$s1,$s2,prep#如果p table$t1,$s3,partitionswap#分支if数组[j]你的问题是什么?我猜您正在使用SPIM/MARS。它们具有调试功能,例如,允许您单步执行代码。我建议您从分析运行时程序中发生的事情开始。