Question

Write MIPS code for the highlighted part # Assume that factorial is defined as factorial(int x)...

  1. Write MIPS code for the highlighted part

# Assume that factorial is defined as factorial(int x)
.data

num: .word 8

large_str: .asciiz “Factorial result is large”

small_str: .asciiz
.text
# int y = factorial(num);
lw $t0, num # push this onto the stack before calling factorial(int)
____________
____________
____________
# clean up the stack after wards
____________
# You can use the print string syscall in lieu of a call to printf
# if(y > 200) { printf(large_str); }
addi $t0, $0, 200
bgt $v0, $t0, large
# else { printf(small_str);}
else:
____________

____________
syscall
____________
large:
____________

____________

syscall
end:

li $v0, 10

syscall


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

PROGRAM:

.data
   num: .word 8
   large_str: .asciiz "Factorial result is large"
   small_str: .asciiz "Factorial result is small"
.text
.globl main
main:
   # int y = factorial(num);
   lw $t0, num
   # push num onto the stack before calling factorial(int)
   addiu $sp,$sp,-4       # Adjusting the stack pointer to store num on the stack
   sw $t0,0($sp)           # Storing num on the stack
   jal factorial           # Calling factorial(num)
   # clean up the stack after wards
   addiu $sp,$sp,4           # Adjusting the stack pointer to its original position
   # You can use the print string syscall in lieu of a call to printf
   # if(y > 200) { printf(large_str); }
   addi $t0, $0, 200
   bgt $v0, $t0, large       # If $v0 is greater than $t0 jump to large
   # else { printf(small_str);}
else:
   li $v0,4           # Call ID to print a string
   la $a0,small_str       # Loading the address of the string to print
   syscall               # call to print the string
   b end               # Branch ti end
large:
   li $v0,4           # Call ID to print a string
   la $a0,large_str       # Loading the address of the string to print
   syscall               # call to print the string
end:
   li $v0, 10           # Call ID to exit the program
   syscall               # Call to exit the program

# Definition of factorial function
factorial:
   addi $sp,$sp,-4       # Adjusting the stack pointer to store return address on the stack
   sw $ra,0($sp)       # Storing return address on to the stack
  
   lw $t0,4($sp)       # Loading number form the stack
   li $t1,1       # Loop variable (i)
   li $t2,1       # Factorial value (fact)
for:   bgt $t1,$t0,return   # If i>num exit
   mul $t2,$t2,$t1       # Else fact = fact * i
   addi $t1,$t1,1       # Increment loop counter (i = i+1)
   b for           # repeat the loop
return:   move $v0,$t2       # Store the value to returned in $v0
   lw $ra,0($sp)       # Loading the return address from stack
   addi $sp,$sp,4       # Adjust stack pointer to its original position
   jr $ra           # Jump back to the calling function
  

Please refer to the following screenshot of the program for indentation of the code:

OUTPUT of the program:

Add a comment
Know the answer?
Add Answer to:
Write MIPS code for the highlighted part # Assume that factorial is defined as factorial(int x)...
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
  • .data prompt: .asciiz "Input an integer x:\n" result: .asciiz "Fact(x) = " .text main: # show prompt li $v0, 4 la $a0, prompt syscall # read x li $v0, 5 syscall # function call move $a...

    .data prompt: .asciiz "Input an integer x:\n" result: .asciiz "Fact(x) = " .text main: # show prompt li $v0, 4 la $a0, prompt syscall # read x li $v0, 5 syscall # function call move $a0, $v0 jal factorial # jump factorial and save position to $ra move $t0, $v0 # $t0 = $v0 # show prompt li $v0, 4 la $a0, result syscall # print the result li $v0, 1 # system call #1 - print int move $a0,...

  • WRITE THE FOLLOWING CODE IN FLOATING POINT NUMBERS IN ASSEMBLY LANGUAGE USING MIPS IN MARS .data...

    WRITE THE FOLLOWING CODE IN FLOATING POINT NUMBERS IN ASSEMBLY LANGUAGE USING MIPS IN MARS .data prompt: .asciiz "\nMaximum number is : " prompt1: .asciiz "\nMinimum number is : " prompt2: .asciiz "\nRange of the array is : " size: .word 10 #load array array: .word 23, -12, 45, -32, 52, -72, 8, 13,22,876 .text #load address of array and size la $s4,array #load address of A lw $t0,size #load i to t0 jal getArrayRange li $v0, 4    la...

  • The task will be to write a program in assembler to enter a number and calculate...

    The task will be to write a program in assembler to enter a number and calculate its associated Fibonacci number using a procedure (subroutine) that is called recursively. Factorial and Fibonacci(outline Programs) # MIPS assembly assembly assemblyassemblycode .data n: .word 4 .text main: la $s0,n lw $a0, 0($s0) jal factorial # move what ever is returned into $a0 move $a0, $v0 li $v0,1 syscall b finished factorial: add $sp , $sp , -8 # make room sw $a0, 4($sp )...

  • ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra,...

    ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra, 4($sp) # save the return address on stack beqz $a0, terminate # test for termination subu $sp, $sp, 4 # do not terminate yet sw $a0, 4($sp) # save the parameter sub $a0, $a0, 1 # will call with a smaller argument jal Factorial # after the termination condition is reached these lines # will be executed lw $t0, 4($sp) # the argument I...

  • CSE/EEE230 Assignment11 Due Date Thursday, April 25th, 5pm Note: the lowest scored assignment wil...

    CSE/EEE230 Assignment11 Due Date Thursday, April 25th, 5pm Note: the lowest scored assignment will be dropped. Important: This is an individual assignment. Please do not collaborate. It must be submitted on-line (Blackboard). No late assignment will be accepted Minimal Submitted Files You are required to turn in the following source file: assignment11.s Objectives: -write assembly language programs to:             -perform arithmetic on floating point numbers             -use syscall operations to display floating point numbers and strings on the console window             -use syscall...

  • CSE/EEE230 Assignment11 Due Date Thursday, April 25th, 5pm Note: the lowest scored assignment will be dropped....

    CSE/EEE230 Assignment11 Due Date Thursday, April 25th, 5pm Note: the lowest scored assignment will be dropped. Important: This is an individual assignment. Please do not collaborate. It must be submitted on-line (Blackboard). No late assignment will be accepted Minimal Submitted Files You are required to turn in the following source file: assignment11.s Objectives: -write assembly language programs to:             -perform arithmetic on floating point numbers             -use syscall operations to display floating point numbers and strings on the console window...

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