2a.
2000016700MOV 2040, R0
2002000034
2004030027BIT R0, #1
2006000001
2010001405BEQ 2024
2012070027MUL #3, R0
2014000003
2016062700ADD #1, R0
2020000001
2022000402BR 2030
2024071027DIV #2, R0
2026000002
2030020027CMP R0, #1
2032000001
2034003363BGT 2004
2036000000HALT
2040000005.WORD 5
(you didn't have to write .WORD to constitute a disassembly, but that's what we'd write in assembly language)

2b. This program takes a number from memory location 2040 and performs the following algorithm on it:

while (n > 1) {
    if (n is even)
        n := n / 2
    else
        n := 3 * n + 1
}
The program goes through the sequence 5, 16, 8, 4, 2, 1; it will halt with 1 in R0. The last op affecting R1 will have been the division, dividing 2 by 2; R1 will end up containing the remainder from this operation, which is 0. R7 ends with the value 2040. Memory location 2040 is unchanged (still 5).

(This is a very interesting algorithm and I'd encourage you to write it in a high-level language of your choice, with a print statement each loop iteration, and investigate its behaviour a bit. It is an unsolved problem whether or not this algorithm terminates for all n!)

2c. It will work correctly when loaded at any reasonable address, because all addressing is relative.


[on to problem #3]