A loop inside a loop
A nested loop is just two skeletons stacked, where the outer loop's body is the inner loop. The inner counter j resets to 0 at the top of every outer pass, while the outer counter i waits and only moves on once the inner loop has run to completion. Each half is the same pre-tested loop you've seen all chapter.
li r6, 0 # accumulator = 0
li r4, 0 # outer index = 0
b otest
obody:
li r5, 0 # inner index = 0 (reset each outer pass)
b itest
ibody:
mullw r0, r4, r5 # variable product
addi r5, r5, 1 # advance inner index
add r6, r6, r0 # accumulate
itest:
cmpw r5, r3 # inner bound test
blt+ ibody
addi r4, r4, 1 # advance outer index
otest:
cmpw r4, r3 # outer bound test
blt+ obody
mr r3, r6
blr
That mullw is there because the inner body multiplies two variable quantities, with no constant to turn into a shift. And the dead giveaway that you're looking at a nest? The inner-counter reset, that li r5, 0 tucked inside the outer body.
#pragma optimization_level 1 keeps both loops rolled.
Your task
Write grid, returning the sum of i * j over all 0 <= i < n and 0 <= j < n.