5.118 · exercise

Predict: loops

Trace counting loops, a post-test loop, a loop inside a loop, and an unsigned test, and say what a register holds when each one ends.

0 of 5 correct

Predict the result

    mov w19, 0  // sum
    mov w20, 1  // i
    b test
body:
    add w19, w19, w20
    add w20, w20, 2
test:
    cmp w20, 9
    b.le body

Predict the result

    mov w19, 0  // passes
    mov w20, 12 // i
again:
    add w19, w19, 1
    add w20, w20, 1
    cmp w20, 10
    b.lt again

Predict the result

// for (i = 0; i < 10; i += 4)
    mov w20, 0  // i
    b test
body:
    add w20, w20, 4
test:
    cmp w20, 10
    b.lt body

Predict the result

    mov w19, 0  // count
    mov w20, 0  // row
row_top:
    mov w21, 0  // col
col_top:
    add w19, w19, 1
    add w21, w21, 1
    cmp w21, w20
    b.le col_top
    add w20, w20, 1
    cmp w20, 4
    b.lt row_top

Predict the result

    mov w19, 0  // passes
    mov w20, -2 // i
loop:
    add w19, w19, 1
    add w20, w20, 1
    cmp w20, 2
    b.lo loop