Answer:
Here's the code below
Explanation:
# All the comment will start with #
# MIPS is assembly language program.
# we store the all the data inside .data
.data:
k: .asciiz "Enter the kth value:\n"
.text
.globl main
li $s0, 0 # $s0 = 0
li $t0, 0 # $t0 = 0
la $n0, k # $n0 is kth value address
syscall # call print_string()
li $v0, 0 # $v0
syscall
move $n0, $v0 # $a0 = user input or "int n"
addi $sp, $sp, -4 # move the stack pointer down
sw $ra, 0 ($sp) # save the return address for main
jal fncheck # call fncheck(n);
fncheck:
beq $n, 0, fnfirst #if n==0 then fnfirst
beq $n, 0, fnfirst #if n==0 then fnfirst
ble $n0, 5, fnrec # if (n <=5) then 5
bge $n0, 5, fnjoke
#first function for n 0 and 1
fnfirst:
li $t0, 0 # initialize $t0 = 0
addi $t0, $t0, 20 # $t0 = 20
move $v0, $t0 # return 20
jr $ra
#function to execute 5*function1(n-2) + n;
fnrec:
sub $sp, $sp, 12 # store 3 registers
sw $ra, 0($sp) # $ra is the first register
sw $n0, 4($sp) # $n0 is the second register
addi $n0, $n0, -2 # $a0 = n - 2
jal function1
sw $v0, 8($sp) # store $v0, the 3rd register to be stored
lw $n0, 4($sp) # retrieve original value of n
lw $t0, 8($sp) # retrieve first function result (function1 (n-2))
mul $t0, $t0, 5 # $t0 = 5 * function1(n-2)
add $v0, $v0, $t0
lw $ra, 0($sp) # retrieve return address
addi $sp, $sp, 12
jr $ra
#function to print joke
fnjoke:
la "Joke section"