-3

If I were to increment $a0 from 0 to 10 using a loop. Then, increment memory address 0 from 0 to 10 using a loop...

Would the code roughly look like

Loop:
addi $a0,1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
gatsbyz
  • 1,057
  • 1
  • 10
  • 26

1 Answers1

1

this is how you implement loops in MIPS Assembly:

.globl main

main:
# start of the loop
loop:
    bgt $a0,10,exit # checks if $a0 is greater than 10 loop ending condition
    addi $a0,$a0,1  # adds one to the $a0 the loop variable
    j loop          # jumps to continue loop

exit:
    li $v0,10       # sets the value of $v0 to 10 to terminate the program
    syscall         # terminate

Kindly check this link if you want to learn more about loops in MIPS Assembly

4aRk Kn1gh7
  • 4,259
  • 1
  • 30
  • 41
  • Since you know the loop runs at least once, you'd normally use `loop: addiu $a0, $a0, 1` / `ble $a0, 10, loop`. (Ignoring branch-delay slots here.) 2 instructions are better than 3. [Why are loops always compiled into "do...while" style (tail jump)?](https://stackoverflow.com/q/47783926) – Peter Cordes Dec 05 '18 at 01:16