Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 a . Write a function combination which implements the combinatorial function n C r in MIPS assembler. A straightforward Java implementation without checks is

1a. Write a function combination which implements the combinatorial function nCr in MIPS assembler.
A straightforward Java implementation without checks is given here:
int combinations(int n, int r
int c=1;
for(int i=r+1;in;i++)
c**=i;
for(int i=1;i n-r; i++
c?=i;
return c;
}
1b. The assemble instructions for multiplication and division, respectively, are mul and div.
Use the non-recursive factorial example with loops from the class as a starting point.
Write a main function which calls combinations twice with hardcoded n and r as follows:
a.n=10,r=5(The answer is 252.)
b.n=100,r=10(The answer is 17,310,309,456,440 which takes more than 32 bits to represent!)
Exit your program with the proper syscall.
1c. Test and run your program. Provide a screenshot of test runs for the two cases.
(I haven't learned mflo or mfhi.)
data
result: .word 0
.text
main:
Call combinations with n=10,r=5
li $a0,10
li $a1,5
jal combinations
Display result for n=10,r=5
li $v,1
move $a0,$v0
syscall
# Call combinations with n=100,r=10
li $a,100
jal combinations
# Display result for n=100,r=10
li $ve,1
move $a0, $v0
syscall
exit: lisyscall$v0,10,# exit with syscall 10
# subroutine FACTORIAL(n)
# calculates n**(n-1)**(n-2)**dots**1
# IN: ,$ a =n, as unsigned integer
# OUT: ,$ V0= FACTORIAL (n)
# CHANGES:
$to, $t1, $ve
combinations:
#Function proglogue
addiu $sp, $sp,-8
Sw $ra,0($sp)
Sw ,$s0,4($sp)
# Initialize result to 1
li $50,1
# Calculate nCr numerator )
move $t0, $a0
# Save n
# Loop to calculate nCr numerator (n**(n-1)**dots**(n-r+1))
loop_num:
addiu $a0,$a0,-1, # Decrement n
bltu $a0, $a1, end_num #Exit if $v0,$a0nCrrr$s0,$$se$t0,$t0,-1$50,0$s0,4($sp)$sp,$sp,8n
mult $v0,$a0
mflo $v0
jloopnum
endnum:
# Calculate nCr denominator (r!)
move $a0, $a1 #Set a0tor
jal factorial # Call factorial function
div $v0, $v0, $a0 # Divide numerator by denominator
mflo $v0 #Store the result invo
jr $ra #Return
loopdenom:
mult $s0,$ to
mflo $se
addiu $t0,$t0,-1
bltu $t0, $zero, endcombinations
jloopdenom
#Function epilogue
endcombinations:
# Store the result
sw $50,0(result)
# Function epilogue
lw $ra,o($sp)
lw $s0,4($sp)
addiu $sp,$sp,8
jr $ra
Error line 75 column 24: "result": operand isof incorrect type
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago

Question

Discuss the Hawthorne experiments in detail

Answered: 1 week ago

Question

Explain the characteristics of a good system of control

Answered: 1 week ago

Question

State the importance of control

Answered: 1 week ago

Question

1. Identify three communication approaches to identity.

Answered: 1 week ago

Question

d. Who are important leaders and heroes of the group?

Answered: 1 week ago

Question

3. Describe phases of minority identity development.

Answered: 1 week ago