Anurag
Anurag

Reputation: 441

While loop in MIPS (Beginner in MIPS)

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

Upvotes: 2

Views: 1003

Answers (1)

ziggystar
ziggystar

Reputation: 28676

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.

Upvotes: 4

Related Questions