Question

Write a MIPS assembly program to calculate nCr. The program should accept the numbers n and...

Write a MIPS assembly program to calculate nCr.

The program should accept the numbers n and r from the user. The program should call the factorial function.

nCr = n! / ((n-r)! * r!)

If n<r print an error message "n should not be less than r: re-enter the values." and get the inputs n and r from the user.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
        .file   1 ""
        .section .mdebug.abi32
        .previous
        .nan    legacy
        .module fp=32
        .module nooddspreg
        .abicalls
        .rdata
        .align  2
$LC0:
        .ascii  "\012 Enter the value for N and R \000"
        .align  2
$LC1:
        .ascii  "%d%d\000"
        .align  2
$LC2:
        .ascii  "n should not be less than r: re-enter the values\000"
        .align  2
$LC3:
        .ascii  "\012 The value of ncr is: %d\000"
        .text
        .align  2
        .globl  main
        .set    nomips16
        .set    nomicromips
        .ent    main
        .type   main, @function
main:
        .frame  $fp,56,$31              # vars= 16, regs= 4/0, args= 16, gp= 8
        .mask   0xc0030000,-4
        .fmask  0x00000000,0
        .set    noreorder
        .cpload $25
        .set    nomacro
        addiu   $sp,$sp,-56
        sw      $31,52($sp)
        sw      $fp,48($sp)
        sw      $17,44($sp)
        sw      $16,40($sp)
        movz    $31,$31,$0
        move    $fp,$sp
        .cprestore      16
        lw      $2,%got($LC0)($28)
        nop
        addiu   $4,$2,%lo($LC0)
        lw      $2,%call16(puts)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,puts
1:      jalr    $25
        nop

        lw      $28,16($fp)
        addiu   $3,$fp,32
        addiu   $2,$fp,28
        move    $6,$3
        move    $5,$2
        lw      $2,%got($LC1)($28)
        nop
        addiu   $4,$2,%lo($LC1)
        lw      $2,%call16(__isoc99_scanf)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,__isoc99_scanf
1:      jalr    $25
        nop

        lw      $28,16($fp)
$L5:
        lw      $3,28($fp)
        lw      $2,32($fp)
        nop
        slt     $2,$3,$2
        beq     $2,$0,$L6
        nop

        lw      $2,%got($LC2)($28)
        nop
        addiu   $4,$2,%lo($LC2)
        lw      $2,%call16(printf)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,printf
1:      jalr    $25
        nop

        lw      $28,16($fp)
        addiu   $3,$fp,32
        addiu   $2,$fp,28
        move    $6,$3
        move    $5,$2
        lw      $2,%got($LC1)($28)
        nop
        addiu   $4,$2,%lo($LC1)
        lw      $2,%call16(__isoc99_scanf)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,__isoc99_scanf
1:      jalr    $25
        nop

        lw      $28,16($fp)
        b       $L5
        nop

$L6:
        nop
        addiu   $3,$fp,32
        addiu   $2,$fp,28
        move    $6,$3
        move    $5,$2
        lw      $2,%got($LC1)($28)
        nop
        addiu   $4,$2,%lo($LC1)
        lw      $2,%call16(__isoc99_scanf)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,__isoc99_scanf
1:      jalr    $25
        nop

        lw      $28,16($fp)
        lw      $2,28($fp)
        nop
        move    $4,$2
        lw      $2,%got(fact)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,fact
1:      jalr    $25
        nop

        lw      $28,16($fp)
        move    $16,$2
        lw      $2,32($fp)
        nop
        move    $4,$2
        lw      $2,%got(fact)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,fact
1:      jalr    $25
        nop

        lw      $28,16($fp)
        move    $17,$2
        lw      $3,28($fp)
        lw      $2,32($fp)
        nop
        subu    $2,$3,$2
        move    $4,$2
        lw      $2,%got(fact)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,fact
1:      jalr    $25
        nop

        lw      $28,16($fp)
        mult    $17,$2
        mflo    $2
        nop
        nop
        bne     $2,$0,1f
        div     $0,$16,$2
        break   7
1:
        mfhi    $2
        mflo    $2
        sw      $2,24($fp)
        lw      $5,24($fp)
        lw      $2,%got($LC3)($28)
        nop
        addiu   $4,$2,%lo($LC3)
        lw      $2,%call16(printf)($28)
        nop
        move    $25,$2
        .reloc  1f,R_MIPS_JALR,printf
1:      jalr    $25
        nop

        lw      $28,16($fp)
        nop
        move    $sp,$fp
        lw      $31,52($sp)
        lw      $fp,48($sp)
        lw      $17,44($sp)
        lw      $16,40($sp)
        addiu   $sp,$sp,56
        j       $31
        nop

        .set    macro
        .set    reorder
        .end    main
        .size   main, .-main
        .align  2
        .globl  fact
        .set    nomips16
        .set    nomicromips
        .ent    fact
        .type   fact, @function
fact:
        .frame  $fp,24,$31              # vars= 8, regs= 1/0, args= 0, gp= 8
        .mask   0x40000000,-4
        .fmask  0x00000000,0
        .set    noreorder
        .set    nomacro
        addiu   $sp,$sp,-24
        sw      $fp,20($sp)
        move    $fp,$sp
        sw      $4,24($fp)
        li      $2,1                    # 0x1
        movz    $31,$31,$0
        sw      $2,8($fp)
        lw      $2,24($fp)
        nop
        bne     $2,$0,$L8
        nop

        lw      $2,8($fp)
        b       $L9
        nop

$L8:
        li      $2,1                    # 0x1
        sw      $2,12($fp)
        b       $L10
        nop

$L11:
        lw      $3,8($fp)
        lw      $2,12($fp)
        nop
        mult    $3,$2
        mflo    $2
        sw      $2,8($fp)
        lw      $2,12($fp)
        nop
        addiu   $2,$2,1
        sw      $2,12($fp)
$L10:
        lw      $3,12($fp)
        lw      $2,24($fp)
        nop
        slt     $2,$2,$3
        beq     $2,$0,$L11
        nop

        lw      $2,8($fp)
$L9:
        move    $sp,$fp
        lw      $fp,20($sp)
        addiu   $sp,$sp,24
        j       $31
        nop

        .set    macro
        .set    reorder
        .end    fact
        .size   fact, .-fact
        .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609"
Add a comment
Know the answer?
Add Answer to:
Write a MIPS assembly program to calculate nCr. The program should accept the numbers n and...
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
  • Write a MIPS Assembly program to accept two numbers A and B from the user. Print...

    Write a MIPS Assembly program to accept two numbers A and B from the user. Print all the prime numbers between A and B. Also, check if A and B are prime. If no prime number exits print the string "No prime number between numbers A and B"

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

  • mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would...

    mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would get displayed with "+" between them. The user should enter the sum of the two numbers. The program should print "Incorrect" if the user enters a wrong answer and re-prompts the user to re-enter the answer again. Limit the number of incorrect tries to 3. After 3 incorrect tries print the correct answer. The program should print a message like "Correct!" if the user...

  • mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would...

    mathTutor Write a program that selects two random numbers -20 to 20. The two numbers would get displayed with "+" between them. The user should enter the sum of the two numbers. The program should print "Incorrect" if the user enters a wrong answer and re-prompts the user to re-enter the answer again. Limit the number of incorrect tries to 3. After 3 incorrect tries print the correct answer. The program should print a message like "Correct!" if the user...

  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • Write a program in MIPS to accept a string from a user, reverse that string and...

    Write a program in MIPS to accept a string from a user, reverse that string and print it out to the user. You must write a procedure that performs the reversal task. Example: Please Enter a String: Hello How Are You Reversed String: uoY erA woH olleH

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • Introduction: In this lab, you will write a MIPS program to read in (up to) 50...

    Introduction: In this lab, you will write a MIPS program to read in (up to) 50 integer values from the user, store them in an array, print out the amay, one number per line, reverse the elements in the array and finally print out the elements in the just-reversed) array. Feel free to do this lab and all assembly programming labs) in Windows. You must use MARS Getting started: l. In MARS, create a new assembly file with the name...

  • Write a MIPS program that will ask the user to enter two floating point numbers at...

    Write a MIPS program that will ask the user to enter two floating point numbers at the console and pass the values to a function that does multiplication

  • MIPS CODE required to write an assembly program to find the maximum of an array of integers by...

    required to write an assembly program to find the maximum of anarray of integers by doing the following:1. Prompt user to input array size n (n <= 10)2. Prompt user to input element values of array A one by one3. Display the result on the console.This program must at least include one function. The main program will read the valuesof the array (as user inputs the element values) and stores them in the memory (datasegments section) and at the end...

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