我需要在 MIPS 中的二叉树中搜索术语项目。我有的代码。他有一个研究生打字所以我不知道它有多好,远是:
text
.globl __start
__start:
la $a0, tree
li $a1, 0
jal search
jal print_path
li $v0, 10
syscall
store_path:
mul $t0, $a1 4
sw $a0, path($t0)
addi $t0, $t0, 4
sw $a0, path($t0)
jr $ra
print_path:
li $t0, 0
mul $t1, $t0, 4
lw $a0, path($t1)
next: li $v0,4
syscall
addi $t0, $t0, 1 #i++
mul $t1, $t0, 4
lw $a0, path($t1)
beqz $a0, done
move $t1, $a0
la $a0, arrow
li $v0, 4
syscall
move $a0, $t1
b next
done: la $a0, endl
li $v0, 4
syscall
jr $ra
#---------------------------------------- code I need goes here.
search: j store_path
jr $ra
#---------------------------------------- /code
.data
path: .space 40
tree: .word name0, node1, node2, 0
node1: .word name1, node3, node4, 0
node2: .word name2, node5, node6, 0
node3: .word name3, node7, 0, 0
node4: .word name4, node8, node9, 0
node5: .word name5, 0, 0, 0
node6: .word name6, node10, node11, 0
node7: .word name7, 0, 0, 0
node8: .word name8, 0, 0, 0
node9: .word name9, node12, node13, 0
node10: .word name10, 0, 0, 0
node11: .word name11, 0, 0, 0
node12: .word name12, node14, node15, 0
node13: .word name13, 0, 0, 0
node14: .word name14, 0, 0, 1
node15: .word name15, node16, node17, 0
node16: .word name16, 0, 0, 0
node17: .word name17, 0, 0, 0
name0: .asciiz "apple"
name1: .asciiz "orange"
name2: .asciiz "banana"
name3: .asciiz "pear"
name4: .asciiz "plum"
name5: .asciiz "peach"
name6: .asciiz "nectarine"
name7: .asciiz "pineapple"
name8: .asciiz "grapefruit"
name9: .asciiz "grape"
name10: .asciiz "melon"
name11: .asciiz "avacado"
name12: .asciiz "star"
name13: .asciiz "mange"
name14: .asciiz "passion"
name15: .asciiz "cantaloupe"
name16: .asciiz "watermelon"
name17: .asciiz "apricot"
endl: .asciiz "\n"
arrow: .asciiz "-->"
他给了我们伪代码来尝试和工作:
call store_path
if(value == 1) //not sure what value is I'm guessing ether $a0 or $a1
return 1
if(left tree exists)
if(search(left tree, depth+1)
return 1
if(right tree exists)
return search(right tree, depth +1)
return 0
他从未教过我们如何做 return 或 if 语句。我知道 jal 和 jr $ra 用于返回,但不确定如何准确返回 1,或者返回搜索,如果到目前为止的语句之类的东西是 break 语句,我唯一能做的事情是 break 语句,但最后一次得到了被他标记错了。我向他寻求帮助,他说看那本书,我没有,因为那本书要花大约 800 美元,它已经绝版了,上课时我买不起。
有人可以帮忙吗?我不知道我需要做什么,我在 MIPS 上很烂。