Question
# Define the main function .text main: li $v0, 5 # read integer n syscall move $a0, $v0 li $v0, 5 # read integer k
# Define the main function .text main: li $v0, 5 # read integer n syscall move $a0, $v0
li $v0, 5 # read integer k syscall move $a1, $v0
jal choose # call choose function move $a0, $v0
li $v0, 1 # print result syscall
li $v0, 10 # exit program syscall# Define the choose function choose: addi $sp, $sp, -16 # allocate space on stack for 4 registers sw $ra, 0($sp) # save the return address on the stack sw $s0, 4($sp) # save s0 on the stack sw $a0, 8($sp) # save n on the stack sw $a1, 12($sp) # save k on the stack
slt $t0, $a0, $a1 # check if n < k beq $t0, $1, choose_end beq $a0, $0, choose_one # check if n == 0 or k == 0 beq $a1, $0, choose_one
addi $a0, $a0, -1 # calculate choose(n-1, k-1) addi $a1, $a1, -1 jal choose move $s0, $v0
addi $a0, $a0, 1 # calculate choose(n-1, k) jal choose add $v0, $s0, $v0 # add val1 and val2
j choose_end
choose_one: li $v0, 1 # return 1 j choose_end
choose_end: lw $ra, 0($sp) # restore the return address lw $s0, 4($sp) # restore s0 addi $sp, $sp, 16 # deallocate space from stack jr $ra # return to caller
Idk what's wrong in this code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started