The program is supposed to do the following:
- Add up the first 6 data items ($1 to $6) stored at address label DATA.
- Store the sum of the first 6 data items to address label SUM1.
- Multiply the sum stored at address label SUM1 by 8, and store the result at address label MUL8. (loop then add)
- Add up the last 6 data items ($7 to $C) stored at address label DATA.
- Store the sum of the last 6 data items to address label SUM2.
- Divide the sum stored at address label SUM2 by 4, and store the result at address label DIV4.
You can't solve this task without consulting the Programmer's Reference Manual
There's really nothing more to do for the
BSR SUBRinstruction that already does a 'Branch to Subroutine' (BSR). Defining the SUBR part is just a matter of writing down the instructions that will perform the six steps that were outlined in your task description and then execute a 'Return from Subroutine' (RTS).To get you on your way, here's a detailed explanation for step 1
In order to sum up 6 bytes from the array, we can load the first byte in a data register and then add the next 5 bytes from a loop.
Before the loop we:
movea.l #DATA, a1instruction does that.moveq.l #5, d1instruction does that. To load small numbers in the range [-128,+127] always prefermoveqovermovebecause it is both faster and has a smaller encoding .move.b (a1)+, d0instruction does that. Because this instruction uses the post-increment addressing mode and because the size attribute is byte, the address held in the A1 address register will automatically increment by 1. This way we can step through the array.In the loop we:
add.b (a1)+, d0instruction does that.subq.l #1, d1instruction does that. To subtract small numbers in the range [1,8] always prefersubqoversub/subibecause it has a smaller encoding and is much faster thensubi.bne.s loop1instruction does that.I'll throw in the next step since it is rather trivial
Step 1 left the sum in the D0 data register. Just move it to the SUM1 variable but be sure to use the correct size tag which is
.bin accordance with how the SUM1 variable was defined:Good luck with steps 3 to 6...