Methods (or subroutines) in assembler are nothing more than a series of instructions with a label
In assembler programming, input parameters and return values are symbolic - they are agreements on where the input value are stored.
As a result, in some examples, you will NOT see the names of the input parameters in the assembler code !!!
BSR label Effect: (1) Push the Program Counter (PC) onto the system stack (2) Branch to memory location marked by the label "label" |
Memory Instruction Address: in the memory address: --------- ----------------------- 2244: BSR label 2246: MOVE.L #0, Dummy1 4012: label: MOVE.L #0, Dummy2 4014: RTS |
Therefore, PC is equal to 2246 (address of the NEXT instruction)
Supose at the moment that the CPU is executing the BSR instruction (i.e., before the BSR instruction is executed), the stack stack point A& = 10000, so the stack looks like this:
(PC = 2246) +---------------+ (A7 = 10000) | | +---------------+ | | +---------------+ | | +---------------+ A7 --> 10000 | xxxxxxxx | |
Then:
PC = 4012 +---------------+ A7 = 9996 | | +---------------+ | | +---------------+ A7 --> 9996 | 2246 | <--- PC pushed on stack +---------------+ 10000 | xxxxxxxx | |
In addition:
RTS Effect: (1) Pop the top of the stack into the Program Counter (PC) |
Memory Instruction Address: in the memory address: --------- ----------------------- 2244: BSR label 2246: MOVE.L #0, Dummy1 4012: label: MOVE.L #0, Dummy2 4014: RTS |
PC = 4012 +---------------+ A7 = 9996 | | +---------------+ | | +---------------+ A7 --> 9996 | 2246 | +---------------+ 10000 | xxxxxxxx | |
Suppose the CPU fetched "RTS" and executes it...
AFTER the CPU finishes executing "RTS", the stack will be changed to:
PC = 2246 +---------------+ A7 = 10000 | | +---------------+ | | +---------------+ 9996 | 2246 | <- NOT part of the stack ! +---------------+ A7 -->10000 | xxxxxxxx | |
Note that the value 2246 which was at the top of the stack is now in the PC !!!
Note also that the value 2246 is the location AFTER the BSR instruction !!!
Becasue PC = 2246, the next instruction that the CPU will fetch and execute is the one after the BSR instruction !!
That is exactly the location where you want to be when you return from the called function !!!!
FuncName: ...... ...... (assembler instructions that comprise ...... the body of the function) ...... RTS |