Use beq and bne,
just as for conditional statements, and add the
j instruction.
(remove background colors)
# GENERAL LOOP PATTERN# Initialize loop variable, e.g., i = 0 loop: slt ... # test loop condition, e.g., i < numVals beq/bne ... # branch based on loop condition# do something (body of loop)# step to make next iteration different, e.g., i++ j loopendLoop: # is there more to do here?
Assume that R is an array of int.
(remove background colors)
C FORTRAN
------- -------
sum = 0; SUM = 0
I = 0
for ( i = 0; i < n; i++ ) LOOP: IF ( I .GE. N ) GOTO END
{ SUM = SUM + R[I]
sum += R[i]; I = I + 1
} GOTO LOOP
END: ...
Notice that the condition is flipped in Fortran -- under what condition do we skip the code block?
Assume that$s0= i,$s1= n,$s2= R,$s3= sum, and that R is an array ofint.
(remove background colors)
move $s3, $zero # sum = 0
move $s0, $zero # i = 0
LOOP: slt $t0, $s0, $s1 # t0 is 1 if i < n; 0 if i >= n
beq $t0, $zero, END # goto END if i >= n
sll $t0, $s0, 2 # t0 = i * 4
add $t0, $t0, $s2 # t0 is address of R[i]
lw $t1, 0 ($t0) # t1 = R[i]
add $s3, $s3, $t1 # sum += R[i]
addi $s0, $s0, 1 # i += 1 (or i++)
j LOOP
END: ...