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 8854959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:00:37+00:00 2026-06-14T14:00:37+00:00

I have created a code generator for my coursework in a module compilers. It

  • 0

I have created a code generator for my coursework in a module compilers. It generates code in MIPS assembly code and seems to be working ok (ive tested very simple programs and expressions).
I tested the recursive Fibonacci program and it currently loops forever. The base cases, 0 and 1, work ok. But when I try fib(2) or more it keeps looping.
Not sure what the problem is, can anyone help me find it?

Here is the code:

main: 
move $fp $sp 
sw $ra 0($sp)
addiu $sp $sp -4 
sw $fp 0($sp) 
addiu $sp $sp -4 
li $a0 2 #testing comment
sw $a0 0($sp) 
addiu $sp $sp -4 
jal fib_entry 
lw $ra 4($sp) 
addiu $sp $sp 8 
lw $fp 0($sp) 
li $v0, 10 
syscall 
fib_entry: 
move $fp $sp 
sw $ra 0($sp)
addiu $sp $sp -4 
lw $a0 4($fp) 
sw $a0 0($sp) 
addiu $sp $sp -4 
li $a0 0 
lw $t1 4($sp) 
addiu $sp $sp 4 
beq $a0 $t1 thenBranch1
elseBranch1: 
lw $a0 4($fp) 
sw $a0 0($sp) 
addiu $sp $sp -4 
li $a0 1 
lw $t1 4($sp) 
addiu $sp $sp 4 
beq $a0 $t1 thenBranch2
elseBranch2: 
sw $fp 0($sp) 
addiu $sp $sp -4 
lw $a0 4($fp) 
sw $a0 0($sp) 
addiu $sp $sp -4 
li $a0 1 
lw $t1 4($sp) 
sub $a0 $t1 $a0 
addiu $sp $sp 4 
sw $a0 0($sp) 
addiu $sp $sp -4 
jal fib_entry 
sw $a0 0($sp) 
addiu $sp $sp -4 
sw $fp 0($sp) 
addiu $sp $sp -4 
lw $a0 4($fp) 
sw $a0 0($sp) 
addiu $sp $sp -4 
li $a0 2 
lw $t1 4($sp) 
sub $a0 $t1 $a0 
addiu $sp $sp 4 
sw $a0 0($sp) 
addiu $sp $sp -4 
jal fib_entry 
lw $t1 4($sp) 
add $a0 $a0 $t1 
addiu $sp $sp 4 
b endIf2 
thenBranch2: 
li $a0 1 
endIf2: 
b endIf1 
thenBranch1: 
li $a0 0 
endIf1: 
lw $ra 4($sp) 
addiu $sp $sp 12 
lw $fp 0($sp) 
jr $ra 
  • 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-14T14:00:39+00:00Added an answer on June 14, 2026 at 2:00 pm

    Various problems with that code.

    1. you should be decrementing $sp before you write to ($sp) (although this is only by convention)
    2. you should be saving $fp before you modify it (this is by common sense ;))
    3. MIPS typically uses registers to pass arguments (but you can of
      course make up your own convention)
    4. your stack handling is horribly overcomplicated, and at least in main it’s unbalanced
    5. it’s recommended to return from main and not use exit syscall

    You should of course be able to write and debug asm code before you try to generate some.

    Given an input structured something like this C implementation:

    unsigned fib_entry(unsigned n) {
        unsigned ret;
        unsigned n1;
        unsigned f1;
        unsigned n2;
        unsigned f2;
    
        if (n <= 1) goto fib_small;
    
        n1 = n - 1;
        f1 = fib_entry(n1);
        n2 = n - 2;
        f2 = fib_entry(n2);
        ret = f1 + f2;
        goto fib_done;
    
    fib_small:
        ret = n;
    
    fib_done:
        return ret;
    }
    

    I would expect a not-too-clever compiler to produce code like this:

    fib_entry:
        addiu $sp $sp -28   # we need room for $ra, n, ret, n1, n2, f1, f2
        sw $ra, 24($sp)     # store $ra since not leaf function
        sw $a0, 20($sp)     # store n
    
        lw $t0, 20($sp)     # load n
        ble $t0 1 fib_small
    
        lw $t0, 20($sp)     # load n
        addi $t0 $t0 -1     # n - 1
        sw $t0, 12($sp)     # store as n1
    
        lw $a0, 12($sp)     # pass n1 as argument
        jal fib_entry
        sw $v0 4($sp)       # store into f1
    
        lw $t0, 20($sp)     # load n
        addi $t0 $t0 -2     # n - 2
        sw $t0, 8($sp)      # store as n2
    
        lw $a0, 8($sp)      # pass n2 as argument
        jal fib_entry
        sw $v0 ($sp)        # store into f2
    
        lw $t0 4($sp)       # f1
        lw $t1 ($sp)        # f2
        addu $t0 $t0 $t1    # f1 + f2
        sw $t0 16($sp)      # store into ret
    
        b fib_done
    
    fib_small:
        lw $t0, 20($sp)     # load n
        sw $t0 16($sp)      # store into ret
    
    fib_done:
        lw $v0 16($sp)      # load return value
        lw $ra 24($sp)      # restore $ra
        addiu $sp $sp 28    # restore stack
        jr $ra              # return
    

    Note I have deliberately left it unoptimized. This kind of code should be simple enough to generate.

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

Sidebar

Related Questions

I have created an invite friends code, which is properly working if an FB
I have created a code generator for our project to generate a lot of
I have created a UML diagram and generated a class by using Generate Code
I have a dialog box, created with the code javascript and HTML generated below.
I have created ajax code for my city tabs to display respective city data
Hello StackOverflow :) I have created some code inside my onStart(); method to ensure
In my project, I have created a code snippet which can be copied and
I have created my android code wherein I fetch all the data from facebook
I have created some python code which creates an object in a loop, and
I have created a PHP code to echo the following div <div id=main_catsel> <select

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.