Question
ASSEMBLY LANGUAGE (Mars MIPS)

Step 1 (3 points) Based on lab6_Factorial.asm create the program lab6_1.asm as follows: in main prompt the user to enter an

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 have saved on stack
mul $v0, $v0, $t0 # do the multiplication
lw $ra, 8($sp) # prepare to return
addu $sp, $sp, 8 # I’ve popped 2 words (an address and
jr $ra # .. an argument)

terminate:
li $v0, 1 # 0! = 1 is the return value
lw $ra, 4($sp) # get the return address
addu $sp, $sp, 4 # adjust the stack pointer
jr $ra # return
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

.data
   input: .asciiz "Enter a number: "
   output: .asciiz "The factorial is "
.text
  
   la $a0, input
   li $v0, 4 #print string
   syscall

   li $v0, 5    #read int
   syscall
   move $t0, $v0

  
   move $a0, $t0 #pass user input into $a0
   jal Factorial
   move $t1, $v0
  
   la $a0, output
   li $v0, 4 #print string
   syscall
  
   move $a0, $t1
   li $v0, 1 #print int
   syscall
  
   li $v0, 10 #exit
   syscall

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 have saved on stack
mul $v0, $v0, $t0 # do the multiplication
lw $ra, 8($sp) # prepare to return
addu $sp, $sp, 8 # I’ve popped 2 words (an address and
jr $ra # .. an argument)

terminate:
li $v0, 1 # 0! = 1 is the return value
lw $ra, 4($sp) # get the return address
addu $sp, $sp, 4 # adjust the stack pointer
jr $ra # return

output
Enter a number: 6
The factorial is 720
-- program is finished running --

Add a comment
Know the answer?
Add Answer to:
ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra,...
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,...

  • The following MIPS assembly code contains a mistake that violates the MIPS convention in terms of...

    The following MIPS assembly code contains a mistake that violates the MIPS convention in terms of using stack for storing/protecting registers. What is the mistake and how should it be fixed? Correct the corresponding lines in the code. For the corrected code, sketch the stack frame contents at the time when the instruction ‘move $s1, $a1’ is being executed. f:   addi $sp, $sp, 12 sw   $ra, 8($sp) sw   $s1, 4($sp) sw   $s0, 0($sp) move $s0, $a0 move $s1, $a1              #...

  • 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 )...

  • 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...

  • Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how...

    Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language like C or Java, most of the details of subroutine calling are hidden from the programmer. MIPS has special registers to send information to and from a subroutine. The registers $a0,...

  • Write a MIPS program that prints(displays) "Hello World" using MMIO (NOT syscall!) Here is a scre...

    Write a MIPS program that prints(displays) "Hello World" using MMIO (NOT syscall!) Here is a screenshot of a MIPS program that prints user input to the screen. And I need something that displays "Hello World" without taking any input. Please help! Thanks. The output should be something like this but without the input .data 7 strl:.asciiz "\nStart entering characters in the MMIO Simulator" .text 10 .globl echo 12 13 14 15 16 17 echo: al Read # single print statement...

  • QT Spim question. Program and answer already given please explaine it. Please explain the reason why...

    QT Spim question. Program and answer already given please explaine it. Please explain the reason why each instruction was used throughout the program step by step given the prompt, what is its purpose to achive the goal asked in the prompt, why is written in that order. Please not just write to the side what each instruction means in words like load $t0 to $t1. I want to understand the program thoroughly. Thanks in advance Previous information needed to solve...

  • (USING THE PROGRAM MARS) Using the MemoryAccess program we wrote in class as a starting point,...

    (USING THE PROGRAM MARS) Using the MemoryAccess program we wrote in class as a starting point, write a program called LoadStore # Title : Memory access.asm #Desc: Practice initially memory, #in val1 = 0x0a; #int val2 = 0x0b; #int result; #string resultstring = " final answer : "; #string returnchar = "\n"; #void main() { #   result = val1 + val2; #   cout<<< resultstring << returnchar; #} .data val1: .word 0x0a   #store 0xa into variable val1 val2: .word 0x0b   #store...

  • Q-1: Write a program in Assembly language using MIPS instruction set that reads 15 integer numbers...

    Q-1: Write a program in Assembly language using MIPS instruction set that reads 15 integer numbers from user and stores all the numbers in the array intArray. Now, read another integer number N from the user, find the total number of array elements that are greater or equal to the number N, and the total number of array elements that are lower than the number N You must have two procedures: i. ReadIntegerArray: this procedure should read integer array elements...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

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