2

I am trying to write a program to calculate nCr where n and r are input by the user. My approach is following:

  1. I ask to user to enter value of n.

  2. Then I ask the user to enter value of r.

  3. Then I calculate value of n-r.

  4. Then using three while loops I calculate the value of n! r! and (n-r)!

  5. Then I divide by n! by r! and (n-r)!

  6. Then I display it to the user.

  7. Following is my code:

    .data 
         prompt1: .asciiz "Enter the value of n:\n"
         prompt2: .asciiz "Enter the value of r:\n"
         message: .asciiz "The value of nCr is: "
    
     .text
          #Prompt the user to enter value of n
          li $v0,4
          la $a0,prompt1
          syscall
    
          #Get the value of n
          li $v0,5
          syscall
    
          #Store the value of n in $t0
          move $t0,$v0
    
          #Prompt the user to enter value of r
          li $v0,4
          la $a0,prompt1
          syscall
    
          #Get the value of r
          li $v0,5
          syscall
    
          #Store the value of r in $t2
    
    
          move $t2,$v0
    
          #Getting the value of (n-r)
          sub $t4,$t0,$t2
    
          #Calculating value of n!
          addi $t1,$zero,1
    
          while:
                blt $t0,1,exit
                mul $t1,$t1,$t0
                sub $t0,$t0,1
                j while
          exit:
                li $v0,10
                syscall
    
         #Calculating the value of r!
         addi $t3,$zero,1
    
         while:
               blt $t2,1,exit
               mul $t3,$t3,$t2
               sub $t2,$t2,1
               j while
    
          exit:
               li $v0,10
               syscall
    
         #Calculating the value of (n-r)!
         addi $t5,$zero,1
    
         while:
               blt $t4,1,exit
               mul $t5,$t5,$t4
               sub $t4,$t4,1
               j while
    
        exit:
              li $v0,10
              syscall
    
        #Getting the final value
        div $s0,$t1,$t3
        div $s1,$s0,$t5
    
        #Displaying the message
        li $v0,4
        la $a0,message
        syscall
    
        #Display the answer
        addi $a0,$s1,0
        syscall
    

The Error which I am getting is this:

  1. line 46 column 6: label "while" already defined.
  2. line 46 column 6: label "exit" already defined.

What I am doing wrong here? Is it not the correct way to use three while loops in a mips

2
  • 5
    You can't have three global symbols with the same name. How is the assembler going to know which one you're referring to? Aside from that, once you leave the first loop, you're executing system call 10 which will terminate the program. Nothing else in your program will be executed after that. Commented Apr 28, 2021 at 10:56
  • 2
    Thanks for the suggestion. Program is working now:) Commented Apr 28, 2021 at 11:11

1 Answer 1

4

If you write while: in your program, you are creating a label named while, which is an alias for the line number where you place it. Within one file, you can only create one label with a certain name.

A solution for your problem would be to name the labels while1, while2 and while3. Same holds for your exit labels.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. It is working now:)
Another strategy is to try to come up with meaningful names for your labels, like n_fac: or r_loop:, instead of just a mess of numbers. @anurag1905: Also note that x! / y! is prod(i=y..x); instead of calculating two huge numbers and then dividing them, just skip the multiplies that cancel out.
Great Suggestion. I will keep them in mind. Thankyou:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.