MIPS递归字符串长度?

时间:2015-09-18 17:44:34

标签: assembly mips

我目前正在尝试实现一个递归函数来计算MIPS中字符串的长度。这是我现在的代码。我正在努力弄清楚基本情况以及如何让它返回字符串长度。我觉得我很接近,因为当我单步执行程序时,它正确计算并存储输入字符串的长度。任何帮助表示赞赏:

.text
#.align 2


main:
la $a0, input #load string for user input
li $v0, 4 #command to print string in syscall
syscall 

li $v0, 8 #command to take user input 
la $a0, buffer #store string of user input in buffer
li $a1,256 #length of buffer for string
    syscall


move $t0,$a0 #move string address to $t0

addi $t2,$t2,0 #initialize word length counter

addi $sp, $sp, -16 #allocate space in stack
sw $t0,16($sp) #store string address in stack, which will be the working string 
    sw $ra,12($sp) #save return address 
    sw $t2,8($sp) #save word length counter
    jal reclength


    # if we get here then the string is finished 

reclength:
    lw $t0,16($sp) #load word from stack and store address in $t0
    lw $t1,8($sp) #load counter from stack and store in $t1
    lbu $t2,0($t0) #get first bit from word in position 0 of address stored in $t0

    ## BASE CASE ##
    beq $t2,$zero,finisher

    ## RECURSIVE CASE ##
    addi $t1,$t1,1 #incremement length counter
    addi $t0,$t0,1 #point to the next character

    addi $sp, $sp, -16
    sw $t1,8($sp) #store the counter in the stack
    sw $ra,12($sp) #store the return address in the stack
    sw $t0,16($sp) #store new address in stack
    jal reclength



    #jr $ra
    #nop


finisher:
lw $t1,8($sp) #load counter from stack
addi $t1,$t1,-1 #decrement counter because null counter was counted
la $a0, ending #load string for user input
    li $v0, 4 #command to print string in syscall
    syscall 
jr $ra







.data
#.align 2
input:   .asciiz "Enter a string : " 
buffer:  .space 256
ending:  .asciiz "Finished"

1 个答案:

答案 0 :(得分:1)

您为每个函数调用继续向下移动堆栈指针,但在返回时不会向上调整它。

我发现整个设计在堆栈上的返回值有点奇怪。通常会使用$v0来返回函数的值。所以我会改写这样的函数:

reclength:
    lw $t0,12($sp)  # load string address from stack and place it in $t0
    lbu $t2,0($t0)  # read one byte from the string and place it in $t2

    bne $t2,$zero,recurse
    li $v0, 0       # base case : length = 0
    j return

recurse:
    # recursive case : return reclength(addr+1) + 1
    addi $t0,$t0,1 # point to the next character

    addi $sp, $sp, -16
    sw $ra,8($sp)  # store the return address in the stack
    sw $t0,12($sp) # store new address in stack
    jal reclength
    addi $v0,$v0,1 

return:
    lw $ra, 8($sp)  # restore the return address
    addiu $sp,$sp,16 # restore the stack pointer
    jr $ra

你会这样称呼:

addi $sp, $sp, -16 #allocate space in stack
sw $t0,12($sp) #store string address in stack, which will be the working string 
sw $ra,8($sp) #save return address 
jal reclength

您也可以摆脱堆栈上的字符串地址,并使用$a0代替。但是我会把它作为锻炼给你。

您可能还希望在reclength返回main后正确退出该计划:

li $v0,10  # syscall 10 = exit
syscall