2012-02-26 61 views
2

输入后这是我的代码:保存输出数据段

.data 
.ascii "r", "o", "r", "y", "\n" 
key: .word 2 
.text 
.globl main 

main: la $t0, string 
     move $t5, $t0   # preserve original data 
     la $t1, key   # load cypher into t1 reg 
     lw $a1, 0($t1)  # key loaded into a1 reg 

Loop: lb $a0, 0($t5)  # ith element of array loaded into a0  
     beq $a0, 10, exit_all # if ith character is \n, get out of loop 
     jal sub1    # otherwise, call sub 

     addi $t5, $t5,  # increment index of array 
     j Loop 

exit_all: syscall 

sub1: 
    some code 
    move $v0, $t0    # what i want to return to main 
    jr $ra     # exit iteration 

我有在它的子例程循环。每次'jr $ ra'命令将流程返回到我的主函数时,sub返回(在$ v0 reg中)我想要保存的输出。我需要在输入数据段后直接保存这些输出。我该怎么做呢?如果它只是一个输出,我可以说:

sb $v0, 4($t1) 

它会直接保存后。但是有多种输出,所以我如何以一般方式来做到这一点?

回答

2

您需要为所有的输出值的数据区预留空间,因此,例如对于32个的输出值增加:

results: .byte 32 

您的数据部分。然后将寄存器设置为结果地址,并在每次循环时递增寄存器:

 la $t7, result 
... 
Loop: ... 
     jal sub1 
     sb $v0,0($t7) 
     addiu $t7,1 
     j loop 

上述代码未经测试。