2000 | 016700 | MOV 2040, R0 |
2002 | 000034 | |
2004 | 030027 | BIT R0, #1 |
2006 | 000001 | |
2010 | 001405 | BEQ 2024 |
2012 | 070027 | MUL #3, R0 |
2014 | 000003 | |
2016 | 062700 | ADD #1, R0 |
2020 | 000001 | |
2022 | 000402 | BR 2030 |
2024 | 071027 | DIV #2, R0 |
2026 | 000002 | |
2030 | 020027 | CMP R0, #1 |
2032 | 000001 | |
2034 | 003363 | BGT 2004 |
2036 | 000000 | HALT |
2040 | 000005 | .WORD 5 |
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.