MIPS, куда помещать рекурсивные вызовы в проблеме палиндрома - PullRequest
0 голосов
/ 07 мая 2020

Я пишу программу, которая принимает данные пользователем строки и выводит 1, если это палиндром, или 0, если это не так. Он должен использовать рекурсию, однако мне трудно понять, как отформатировать операторы jal и jr, чтобы рекурсия работала правильно.

main:
    la $a0, askForString        #simple text asking for user input
    li $v0, 4                  
    syscall

    li $v0, 8                 
    la $a0, userInput          #points a0 to space allocated to get user input
    li $a1, 41                 #allows the user to enter input up to 40 characters
    syscall

    la $a0, userInput        
    li $v0, 4               
    syscall

    lw $s0, userInput
    j palindrome

palindrome:
    la $a1, userInput
    addu $a1,$a1,$s1   
    lbu $a0,($a1)      # read the character in $s1 spot of the user inputted word

    addi $sp, $sp, -4
    sw $a0, 0($sp)      #add the specific character to the stack so it can be looked at later

    addi $s1, $s1, 1    #advance the count to look at the next character

    jal palindrome

    lw $t0, 0($sp)      #load the last letter of the user given word from the stack
    addi $sp, $sp, 4

    la $a1, userInput
    addu $a1,$a1,$s2   
    lbu $a0,($a1)      # read the character in $s2 spot of the user inputted word

    addi $s2, $s2, 1    #advance the count to look at the next character

    bne $a0, $t0, fail   #if the letters are not equal, print 0, not a palindrome
    jr $ra

success:
    la $a0, '1'
    li $v0, 11
    syscall
    j end

fail:
    la $a0, '0'
    li $v0, 11
    syscall
    j end

end:
    li $v0, 10                 #op code to exit the program
    syscall

Я знаю, что палиндромная часть программы почти наверняка не работает и не имеет возможности узнать, где находится конец слова, но если бы кто-нибудь мог мне просто использовать часть рекурсии, это было бы чрезвычайно полезно

...