Question

.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, $t0 # $a0 = $t0 syscall # execute # return 0 li $v0, 10 # $v0 = 10 syscall .text factorial: # base case -- still in parent's stack segment # adjust stack pointer to store return address and argument addi $sp, $sp, -8 # save $s0 and $ra sw $s0, 4($sp) sw $ra, 0($sp) bne $a0, 0, else addi $v0, $zero, 1 # return 1 j fact_return else: # backup $a0 move $s0, $a0 addi $a0, $a0, -1 # x -= 1 jal factorial # when we get here, we already have Fact(x-1) store in $v0 multu $s0, $v0 # return x*Fact(x-1) mflo $v0 fact_return: lw $s0, 4($sp) lw $ra, 0($sp) addi $sp, $sp, 8 jr $ra this code almost works but i need it to only do factorial between 0 and 10 and give Invaled Entry! for everything else

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

fact.asm:

.data
prompt: .asciiz "Input an integer x:\n"
result: .asciiz "Fact(x) = "
error: .asciiz "Invalid Entry!"
.text
main:
# show prompt
   li $v0, 4
   la $a0, prompt
   syscall # read x
   li $v0, 5
   syscall
   move $a0, $v0
   move $t7,$a0       # function call
   bgt $t7,10,exit
   blt $t7,0,exit
   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, $t0 # $a0 = $t0
   syscall
   li $v0, 10 # $v0 = 10
   syscall
exit:    li $v0, 4
   la $a0, error
   syscall # read x
   li $v0, 10 # $v0 = 10
   syscall

factorial:    addi $sp, $sp, -8# base case -- still in parent's stack segment # adjust stack pointer to store return address and argument
           sw $s0, 4($sp) # save $s0 and $ra
           sw $ra, 0($sp)
       bne $t7, 0, else
       addi $v0, $zero, 1 # return 1
       j fact_return
else: # backup $a0
       move $s0, $t7
       addi $t7, $t7, -1 # x -= 1
       jal factorial # when we get here, we already have Fact(x-1) store in $v0
       multu $s0, $v0 # return x*Fact(x-1)
       mflo $v0
fact_return:    lw $s0, 4($sp)
       lw $ra, 0($sp)
       addi $sp, $sp, 8
       jr $ra


Output:

Mars Messages Run I/o Input an integer x: Invalid Entry! - program is finished running - - Input an integer x: Invalid Entry!

Add a comment
Know the answer?
Add Answer to:
.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...
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
  • 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...

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

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

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

  • .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0,...

    .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0, 1l ​li $s0,0 ​la $s1, A ​li $t3, 0 loop: ​slti $t0, $s0, 17   ​beq: $t0, $zero, end ​sll $t1, $s0 2 ​add $t2 $s1 $t1   ​syscall ​addi $s0, $s0, 1 ​j loop end: ​li $v0, 1 ​add $a0, $zero, $t3 ​syscall /// whats the output of above assembly program

  • what is the output of the following assembly code ? .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0, 1l ​li $s0,0 ​la $s1, A ​li $t3, 0 loop: ​slti $t0, $s0, 17   ​be...

    what is the output of the following assembly code ? .data A: .word 84 111 116 97 108 32 105 115 32 . text main:​li $v0, 1l ​li $s0,0 ​la $s1, A ​li $t3, 0 loop: ​slti $t0, $s0, 17   ​beq: $t0, $zero, end ​sll $t1, $s0 2 ​add $t2 $s1 $t1   ​syscall ​addi $s0, $s0, 1 ​j loop end: ​li $v0, 1 ​add $a0, $zero, $t3 ​syscall

  • Refer the program in the next page: Assume the starting address of data segment: 0x10010000. What...

    Refer the program in the next page: Assume the starting address of data segment: 0x10010000. What is the address of label num3? _______________________________ Write the common syscall code to print string for the blank marked as #Question 2 _________________________________ Write proper comments for the lines marked as #Question 3 ________________________________________________________ Write the machine code for the line marked as #Question 4 in both binary and hex. Binary:_______________________________________ Hex:___________________ Write the machine code for the line marked as #Question 5 in...

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

  • im trying to complete mips program code about a calculator program that can calculate integer addition...

    im trying to complete mips program code about a calculator program that can calculate integer addition / subtraction written using the MIPS assembler. im having hard times to debug this. The input is given to the array of Formula char (base address $ s0) in the form of a formula. The null character (\ 0, ASCII code 0) is placed at the end. The calculation result is given to the register $ s1 and the overflow is ignored. For example,...

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

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