我一直在研究从 HLL 到 MIPS AL 的翻译,但我无法弄清楚为什么会出现这些语法错误。我将发布我正在翻译的 HLL 以及希望我接近完整的 MIPS AL 代码。
我目前的 MIPS AL:
.data
array: .word 8, 2, 7, 10, 5, 6, 3, 4, 9, 3, 1
n: .word 11
.text
.globl main
main:
la $a0, array # array
li $a1, 0 # 0
addi $a2, $a1, -1 # n-1
jal quickSort
li $v0, 10
syscall
quickSort:
addi $sp, $sp, -20
sw $ra, 4($sp)
sw $s0, 8($sp)
sw $s1, 12($sp)
sw $s2, 16($sp)
sw $s3, 20($sp)
move $s0, $a0 # array
move $s1, $a1 # low from 0
move $s2, $a2 # high from n-1
move $a3, $v0 # result from partition into $a3
if_init:
if_compare:
bge $a1, $a2, end_if
jal partition # pivotPostion = partition(array, low, high)
move $s3, $a3 # saves $a3 (results of partition) to s register 3
lw $a0, $s0 # Reloading array before first recursive call
lw $a1, $s1 # Reloading low before first recursive call
addi $a2, $s3, -1 # reloading pivotPosition - 1 before first recursive call
jal quickSort
lw $a0, $s0 # reloading array before second recursive call
addi $a1, $s1, 1 # reloading pivotPosition + 1 before second recursive call
lw $a2, $s2 # reloading high before second recurvise call
jal quickSort
end_if:
end_quickSort:
lw $ra, 4($sp)
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $s3, 20($sp)
addi $sp, $sp, 20
swap:
lw $t0, 0($a0) # array[left] into $t0
lw $t1, 0($a1) # array[right] into $t1
sw $t0, 0($a1)
sw $t1, 0($a0)
jr $ra
end_swap:
partition:
addi $sp, $sp, -28
sw $ra, 4($sp)
sw $s0, 8($sp)
sw $s1, 12($sp)
sw $s2, 16($sp)
sw $s3, 20($sp)
sw $s4, 24($sp)
sw $s5, 28($sp)
move $s0, $a0 # array from main = array
move $s1, $a1 # low from 0 in main = left
move $s2, $a2 # high from n-1 in main = high
mul $t0, $s1, 4
add $t0, $t0, $s0
lw $s3, 0($t0) # array[left] into $s3
mul $t1, $s2, 4
add $t1, $t1, $s0
lw $s4, 0($t1) #array [right] into $s4
mul $t2, $s1, 4
add $t2, $t2, $s0
lw $s5, 0($t2) # array[low] = pivot into $s5
while_0:
bge $s1, $s2, end_while_0
while_1:
ble $s4, $s3, end_while_1
addi $t3, $s2, -1 # right - 1
sw $s2, $t3 # right = right - 1
end_while_1:
while_2:
bge $s1, $s2, end_while_2
bgt $s3, $s5, end_while_2
addi $t4, $s1, -1 # left - 1
sw $s1, $t4 # left = left - 1
end_while_2:
if_init:
if_compare:
bge $s1, $s2, end_if
sw $a0, $s3 # array[left] into $a0 for swap
sw $a1, $s4 # array[right] into $a1 for swap
jal swap
end_if:
end_while_0:
lw $t5, $s4
lw $t6, $s5
sw $s5, $t5 # array[low] = array[right]
sw $s4, $t6 # array[right] = pivot
sw $v0, $s2
end_partition:
lw $ra, 4($sp)
lw $s0, 8($sp)
lw $s1, 12($sp)
lw $s2, 16($sp)
lw $s3, 20($sp)
lw $s4, 24($sp)
lw $s5, 28($sp)
addi $sp, $sp, 28
jr $ra