C8 Computer organization · Practice
Branches and loops in assembly
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
With n in a0, put 1 + 2 + … + n into a1.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
a0 and a1 hold two signed numbers. Put the larger of them in a2.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A branch at address 0x080C jumps back to a label at 0x07F4. What offset, in bytes, is stored in the instruction?
Check
How many times does the body of this loop run?
li t0, 18
loop:
... # the body
addi t0, t0, -4
bgtz t0, loopCheck
With n in a0, put n × (n − 1) into a1 using only additions in a loop (no mul).
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A bottom-tested loop runs a 2-instruction body 100 times with 2 instructions of overhead per pass and 2 to set it up.
(a)
How many instructions does it execute?
(b)
And unrolled by 4?
Check
With n in a0, put 2 + 4 + … + 2n into a1.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A branch at 0x060C jumps forward to 0x062C. Offset in bytes?
Check
li t0, 17, then a body ending addi t0, t0, -5 and bgtz t0, loop. Passes?
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in C8