Nested Loops in MIPS

 


Pattern for Nested Loops:

(remove background colors)

               # Initialize outer loop variable, e.g., i

   outerloop:  slt ...            # test outer loop condition
               beq/bne ...        # conditionally branch to end of outer loop

               # Initialize inner loop variable, e.g., j

   innerloop:  slt ...            # test inner loop condition
               beq/bne ...        # conditionally branch to end of inner loop

               # do something (body of inner loop)

               # step for inner loop, e.g., j++

               j innerloop

   endInnerLp: # is there more to do here?

               # step for outer loop, e.g., i++

               j outerloop

   endOuterLp: # is there more to do here?


Alyce Brady, Kalamazoo College