Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7712481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:38:33+00:00 2026-06-01T01:38:33+00:00

So I have been working on this for a few days now, and I

  • 0

So I have been working on this for a few days now, and I have managed to get through all of it except one part is getting me.

int mystery(int a0)
{
    if (a0 == 0)
    {
        return 0;
    }
    else
    {
        return mystery(a0 - 1) + a0;
    }
}

I have this recursive function and I have some MIPS code. The C code works but I have a problem somewhere in my MIPS code that is making it not come out correctly beyond putting in 2.

.text

main:

li $a0, 2
jal mystery
move $a0, $v0
jal putDec
li $a0, '\n'
li $v0, 11
syscall

li $a0, 3
jal mystery
move $a0, $v0
jal putDec
li $a0, '\n'
li $v0, 11
syscall

li  $v0, 10     
syscall

putDec: 
    li $v0, 1
    syscall 
    jr  $ra     

mystery: 
 bne $0, $a0, recur 
 li $v0, 0      
 jr $ra             

  recur: 
sub $sp, $sp, 8     
sw $ra, 4($sp)  
sub $a0, $a0, 1
jal mystery         
sw $v0, 0($sp)  
jal mystery         
lw $t0, 0($sp)  
addu $v0, $v0, $t0  
addu $v0, $v0, 1    
add $a0, $a0, 1     
lw $ra, 4($sp)  
add $sp, $sp, 8     
jr $ra      

Everything up to the label ‘mystery’ is fine it is just there as a formality to actually put in arguments and print after. The problem I am having is getting values above 3 to print out the right numbers. Help would be greatly appreciated if someone could see where I am making my mistake. Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:38:35+00:00Added an answer on June 1, 2026 at 1:38 am

    Whoo after extensive work and tracking the code one step at a time I think I finally got it, had to do quite a bit of changing but actually seems easier now. Here is the code in case you want to look over it again, if you can see a problem that could arise with any certain values I did not test. Thanks for the help all.

    New recursive mystery function:

    mystery: 
     bne $0, $a0, recur     
     li $v0, 0      
     jr $ra             
    
     recur: 
      sub $sp, $sp, 8   
      sw $ra, 4($sp)    
      sw $a0, 0($sp)
      sub $a0, $a0, 1   
      jal mystery       
      lw $t0, 0($sp)    #
      addu $v0, $v0, $t0    
      lw $ra, 4($sp)    
      add $sp, $sp, 8   
      jr $ra        
    

    Thanks again. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this frustrating bug for a few days now and
I've been trying to get this working for a few days now.. flabbergasted. I'm
I have been working on this issue for a few days now and can't
I have been working on this for a few days now and I have
I have been working on this problem for 2 days now and it's an
I have been working on this for days but I don't get it so
I have been working on a project for a few days now and have
I have been struggling with this problem for a few days now and I
I have been searching about this subject now for quite a few days. And
I've been working on this for a few days now, and I've found several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.