Question

Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a high level language of your choice and in Assembly to find a Fibonacci number and check your result. At the end of this assignment a possible solution to this problem is given both in Python and MIPS assembler. Note that it would have been possible to write the Python differently to achieve the same function. . [20 points] Write and debug a MIPS program that computes factorial of a given number iteratively (not recursively. This is a MIPS program with looping Instructions and Hints: Begin by writing the factorial calculation in your favorite high-leveil programming language and including the entire program as a comment in your MIPS program like the Python code as in the practice problem. Your high-level code must work correctly for any value from 0! upward (remember that 0!1 by definition) for answers that will fit in 32 bits. Your high level language implementation and your MIPS implementation must use the SAME algorithm (same type of loop, same number of and location of branches, no try statements etc.) You will want to use an algorithm that will minimize the number of branches necessary to make the assembly version simpler Evaluation: Your code should be appropriately commented, 25% will be deducted if the high-level language code is not in a comment as specified above The output from your run should look similar to (though obviously not identical to) the Fibonacci output. 2. [EXTRA CREDIT 5 points] Implement a program to calculate the 2s complement of a number entered by the user. The program should only use the XOR and ADD operators. Your program should include a proper and useful prompt for input, and print the results in a meaningful manner, similar to above

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1)

##################example high level code for factorial####################
int main(){
int n;
int i,f=1;

printf("Enter a positive number:")
scanf("%d",&n);
if(n!=0 && !(n<0)){
for(i=n;i>0;i--){
f=f*i;
break;
}
}
printf("the value of factorial is %d",f);
return 0;
}


###################MIPS CODE######################################

.globl main
.data
    msgprompt: .asciiz "Enter positive number"
    msg: .asciiz "The value of factorial is"
  
.text
main:

    #printf("Enter a positive number:")
    li $v0,4
    la $a0,msgprompt
    syscall
  
    #scanf("%d",&n)
    li $v0,5
    syscall
    move $t1,$v0
  
    #if(n!=0 && !(n<0)){
    blez $t1,exit
  
    #int i,f=1
    li $t0,1
  
    #for(i=n;i>0;i--){
    loop:
        #break
        blez $t1,print_fact
        #f=f*i;
        mul $t0,$t0,$t1
        addi $t1,$t1,-1
        j loop
      
      
    printfact:
        #printf("the value of factorial is %d",f)
        li $v0,4
        la $a0,msg
        syscall
      
        li $v0,1
        move $a0,$t0
        syscall
    
      
    exit:
        #return
        li $v0,10
        syscall

Add a comment
Know the answer?
Add Answer to:
Practice Problem [no points]: You should read and understand Fibonacci algorithm, write a code in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT