我正在尝试在MIPS中实现二叉搜索树。为了实现,我迭代一个循环,要求用户输入一个数字并将数字插入BST。迭代继续,直到用户没有更多要插入的数字。在第一次迭代之后,会发生这种情况:
这是代码:
.data # Put Global Data here
ask: .asciiz "Enter the list of numbers to be sorted one by one."
input: .asciiz "Enter a number:"
ques: .asciiz "Do you want to enter more? (input 1 if you want to enter
more, 0 if you are done)"
root: .word 0
# Code section
.text # Put program here
.globl main # globally define 'main'
main:
li $v0,4
la $a0,ask
syscall
add $s0,$s0,$zero
loop:
li $v0,4
la $a0,input
syscall
li $v0,5
syscall
lw $t0,root
add $a1,$t0,$zero
add $a0,$v0,$zero
jal insert
beq $t0,$zero,first
first: sw $v0,root
j cont
cont: li $v0,4
la $a0,ques
syscall
li $v0,5
syscall
bne $v0,$zero,loop
# terminate the program with system call
li $v0, 10 # syscall to exit cleanly from main only
syscall # this ends execution
.end
insert:
addi $sp,$sp,-12
sw $a0,0($sp)
sw $a1,4($sp)
sw $ra,8($sp)
add $s0,$a1,$zero
beq $s0,$zero,start
j c1
start:
jal newnode
add $t1,$t1,$v0
sw $a0,0($t1)
sw $zero,4($t1)
sw $zero,8($t1)
j last
c1: lw $t6,0($s0)
add $t2,$t6,$zero
slt $t3,$a0,$t2
bne $t3,$zero,left
j right
left: lw $t4,8($s0)
add $a1,$t4,$zero
jal insert
add $t5,$v0,$zero
sw $t5,8($s0)
j last
right: lw $t4,4($s0)
add $a1,$t4,$zero
jal insert
add $t5,$v0,$zero
sw $t5,4($s0)
j last
last: lw $a0,0($sp)
lw $a1,4($sp)
lw $ra,8($sp)
addi $sp,$sp,12
jr $ra
newnode: addi $sp,$sp,-12
sw $a0,0($sp)
sw $a1,4($sp)
sw $ra,8($sp)
li $v0,9
li $a0,12
syscall
lw $a0,0($sp)
lw $a1,4($sp)
lw $ra,8($sp)
addi $sp,$sp,12
jr $ra
任何帮助将不胜感激。谢谢。