Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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
Recursion 使用堆栈指针(Mars 4.5)在程序集mips中查找根的最大和叶_Recursion_Assembly_Mips_Sql Server Mars - Fatal编程技术网

Recursion 使用堆栈指针(Mars 4.5)在程序集mips中查找根的最大和叶

Recursion 使用堆栈指针(Mars 4.5)在程序集mips中查找根的最大和叶,recursion,assembly,mips,sql-server-mars,Recursion,Assembly,Mips,Sql Server Mars,我有一个关于程序集mips代码中根的最大和叶的问题。我使用火星4,5汇编程序。我有这个树值: .data root: .word 7, n01, n02 n01: .word -4, n03, n04 n02: .word 5, n05, n06 n03: .word 5, n07, n08 n04: .word 2, 0, n09 n05: .word 13, 0, 0 n06: .word -15, n10, n11 n07: .word 1, 0, 0 n08: .word 0, n12,

我有一个关于程序集mips代码中根的最大和叶的问题。我使用火星4,5汇编程序。我有这个树值:

.data
root: .word 7, n01, n02
n01: .word -4, n03, n04
n02: .word 5, n05, n06
n03: .word 5, n07, n08
n04: .word 2, 0, n09
n05: .word 13, 0, 0
n06: .word -15, n10, n11
n07: .word 1, 0, 0
n08: .word 0, n12, n13
n09: .word 27, 0, 0
n10: .word 23, n14, 0
n11: .word 31, 0, n15
n12: .word -8, 0, 0
n13: .word 12, 0, 0
n14: .word 2, 0, 0
n15: .word 2, 0, 0
第一个元素是node的值,第二个是左子树,第三个是右子树。如何使用堆栈指针($SP)编写此问题的ricorsion?非常感谢

C代码:

int maxSumPath (struct node *root) {
if (!root ) return 0;
if(!root.left && !root.right) return root.value;
return max(root.value+maxSumPath(root.left) ,root.value+maxSumPath(root.right)); } 
我解决了我的问题,这是我的解决方案:

.text
main:
    la $a0, root #root's address
    jal sum
    move $a0, $v0 #out of the sum
    li $v0, 1 #command to output
    syscall
    li $v0, 10 #exit();
    syscall
sum:
    bnez $a0, sumric #IF(nodo != null) start the recursion
    li $v0, 0 #else right's sum
    jr $ra
sumric:
    addiu $sp, $sp, -12 #allocate space in the stack pointer
    sw $ra, 0($sp) #save ra
    sw $a0, 4($sp) #save node's address
    lw $a0, 4($a0) #load left subtree node's address
    jal sum
    sw $v0, 8($sp) #save the max sum
    lw $a0, 4($sp) #save node's address
    lw $a0, 8($a0) #load right subtree node's address
    jal sum
    lw $t0, 8($sp) #load left subtree's sum
    bge $v0, $t0, maxsumsx #IF(right subtree's sum > left subtree's sum) go to maxsumsx 
    move $v0, $t0 #else overwrite (change) the max sum
maxsumsx:
    lw $t8, 4($sp) #load node's address
    lw $t3, 0($t8) #load node's value
    bne $t8, 0, maxsumdx #IF(right node's address != null) go to calculate right subtree's sum
    addu $t0, $t0, $t3 #else update left sum
    j turnUP
maxsumdx:
    addu $v0, $v0, $t3 #update right sum
    j turnUP
turnUP:
    lw $ra, 0($sp) #update $ra's value in the stack
    addiu $sp, $sp, 12 #deallocate space in the stack pointer
    jr $ra #return back in recursion

你能为它写伪代码或C代码吗?或者流程图?int-maxSumPath(struct node*root){if(!root)返回0;if(!root.left&&!root.right)返回root.value;return max(root.value+maxSumPath(root.left)、root.value+maxSumPath(root.right));}这不是asm的最佳代码,但无论如何,您能将其转换为asm吗?如果没有,您会被困在哪里?我的问题是递归使用堆栈指针。我发布的C代码就是一个例子来解释我的问题。我不知道如何使用堆栈指针来计算叶到根的最大和。我刚开始在汇编中编程,我不知道如何在mips assembly中翻译这段代码这里是我为mips递归给出的一些答案:顺便说一句,如果您编辑它并将注释中的C代码放入代码块中,您可以为它编写伪代码或C代码吗?或者流程图?int-maxSumPath(struct node*root){if(!root)返回0;if(!root.left&&!root.right)返回root.value;return max(root.value+maxSumPath(root.left)、root.value+maxSumPath(root.right));}这不是asm的最佳代码,但无论如何,您能将其转换为asm吗?如果没有,您会被困在哪里?我的问题是递归使用堆栈指针。我发布的C代码就是一个例子来解释我的问题。我不知道如何使用堆栈指针来计算叶到根的最大和。我刚开始在汇编中编程,我不知道如何在mips assembly中翻译这段代码这里是我为mips递归给出的一些答案:顺便说一句,如果您编辑它并将注释中的C代码放入代码块中,您的问题会变得更好、更完整