Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mips 递归过程树的高度_Mips - Fatal编程技术网

Mips 递归过程树的高度

Mips 递归过程树的高度,mips,Mips,我正在尝试编写一个函数,使用以下算法计算树的高度: height(root) if root == nil then return 0 h1 = height(root.left) h2 = height(root.right) answer = max(h1,h2) +1 return answer 首先我调用传递根的函数,在基本情况下,如果树为null,高度为零,否则调用根的左儿子上的递归并存储值,然后调用根的右儿子上的递归并存储值,那么高度就是左儿

我正在尝试编写一个函数,使用以下算法计算树的高度:

height(root)
    if root == nil then return 0
    h1 = height(root.left)
    h2 = height(root.right)
    answer = max(h1,h2) +1
    return answer
首先我调用传递根的函数,在基本情况下,如果树为null,高度为零,否则调用根的左儿子上的递归并存储值,然后调用根的右儿子上的递归并存储值,那么高度就是左儿子和右儿子加根之间的最大值

我已经在mars中实现了这一点,但在/Users/mips1.asm第47行中收到了错误:0x0040004c处的运行时异常:存储地址未在字边界0x00000007上对齐

.data
tree: .word a
a: .word 3, y, c
y: .word 21, d, e 
c: .word 4, 0, 0
d: .word 5, f, g 
e: .word -3, 0, h 
f: .word 6, 0, 0 
g: .word 9, i, 0 
h: .word 18, 0, w 
i: .word 22, 0, 0 
w: .word 1, 0, 0 
answer: .word 0

.text
.globl main

main:

    la $v0, a
    lw $a0, 0($v0)
    jal height
    sw $v0, answer


    #print
    li $v0,1
    lw $a0, answer
    syscall

    #end
    li $v0, 10
    syscall

#________________________________________
#height
.globl height
height:
    addi $sp, $sp, -12
    sw $ra, 0($sp)
    sw $s0, 4($sp)

    #base case
    li $v0, 0
    beq $a0, 0, leaf

    move $s0, $a0

    sw $a0, 4($a0) #x.left
    jal height

    sw $a0, 8($a0) #x.right
    jal height

    #answer = max(heigtLeft, heigtRight)+1


leaf:
       lw $ra, 0($sp)
       lw $s0, 4($sp)
       addi $sp, $sp, 8
       jr $ra

那么,您将
a
(即3)处的第一个值加载到
$a0
,然后尝试将某些内容存储到
4($a0)
(即地址7),这显然是错误的。MARS有很多调试功能,可以让你设置断点、单步执行代码、查看寄存器和内存内容等。利用它们。那么我要做什么呢?很抱歉,这是我第一次使用mips,我想帮助您一步一步地思考您想要实现的目标。您对该
sw
的意图是什么?你想把它储存在什么地方?一旦确定了这一点,请考虑如何在该点将适当的值放入寄存器,以此类推。