我正在尝试解决/理解MIPS汇编编程任务中可能出现的问题。这是相当基本的,我们必须实现一个递归调用,更新“计数器”并在计数器达到0时退出循环。根据PCSpim,我的错误在运行程序时将计数器值显示为NULL。
.data
string: .asciiz "Recursive call counter "
.text
.globl main
main:
subu $sp, $sp, 32 # create stack frame
sw $ra, 28($sp) # save ($fp, $ra) registers
sw $fp, 24($sp)
addu $fp, $sp, 28 # set up the new frame pointer
sw $s0, 20($sp) # save other GPR’s
li $s1, 10 # load counter to $s1
li $v0, 4 # print counter message and counter
la $a0, string
syscall
li $v0, 4
la $a0, ($s1)
syscall
jal recursive_call # jump to recursive call
lw $s0, 20($sp) # restore registers
lw $fp, 24($sp) # restore SP, FP, $ra for the caller
lw $ra, 28($sp)
addu $sp, $sp, 32 # restore the caller's stack pointer
jr $31 #end program
recursive_call:
subu $sp, $sp, 32 # create stack frame
sw $ra, 28($sp) # save ($fp, $ra) registers
sw $fp, 24($sp)
addu $fp, $sp, 28 # set up the new frame pointer
sw $s0, 20($sp) # save other GPR’s
sub $s1, $s1, 1 # subtract one from counter
li $v0, 4 #print counter number
la $a0, ($s1)
syscall
beq $s0, $zero, SKIP # if counter = 0, go to skip
jal recursive_call # jump to recursive call
SKIP:
lw $s0, 20($sp) # restore registers
lw $fp, 24($sp) # restore SP, FP, $ra for the caller
lw $ra, 28($sp)
addu $sp, $sp, 32 # restore the caller's stack pointer
jr $ra
非常感谢任何帮助。我将继续进行故障排除,看看我是否想出任何东西。
答案 0 :(得分:1)
我不确定这应该是什么:
li $v0, 4
la $a0, ($s1)
syscall
你可能想要:
li $v0,1 # print_int
mov $a0,$s1
syscall
然后就是这样:
beq $s0, $zero, SKIP # if counter = 0, go to skip
您的计数器位于$s1
,但您正在比较$s0
。