Week # 3 – Homework Exercises
Exercise #1: Write mnemonics and machine code (for the Toy CPU) for the following
tasks:
(a) A subroutine, starting at location 10 (hex) that sums memory locations, then
stores the result after them. Assume that the calling routine initialized the register b with
the address of the memory location of the summands, and the register c with the
number of them. Don’t forget the return instruction.
(b) A main routine, starting at location 00, that calls the subroutine twice: once for
summing 4 locations starting at 2A, and once for summing 6 locations starting at 38.
– 1 –
Exercise #2:
Your answer to Ex. 1(a) will have used the a, b and c registers in its own calculations.
Suppose this is inconvenient for the main program and it would be better not to lose the
values that those three registers had when the subroutine was stored.
Rewrite the subroutine so that it pushes the three registers at the start, saving their
values on the stack, and then pops them back at the end so that the calling routine gets
them back unchanged. Make sure you pop them in the right order!
– 2 –
– 3 –