Motorola M68000 User's Guide Page 123

  • Download
  • Add to my manuals
  • Print
  • Page
    / 256
  • Table of contents
  • BOOKMARKS
  • Rated. / 5. Based on customer reviews
Page view 122
Subroutines 109
A unique method that you might come across is to use the LEA instruc
tion:
LEA 12(SP),SP
This method is reported to execute somewhat faster than the ADDA.
An alternate approach is to have the subroutine clean up the stack.
This is complicated by the fact that the return address is on the stack.
You can’t just add a value to SP. You must first move the return address
to a safe place, adjust SP, and finally return the return address to the stack
in preparation for the RTS. For the above example, this can be done as
follows:
MOVEA.L (SP)+,AO GET RETURN ADDRESS
ADDA.L #12,SP ADJUST SP
MOVE.L AO,- (SP) PUT RETURN ADDRESS BACK
RTS
A subtle method would be to substitute a JMP instruction for the RTS.
MOVEA.L (SP)+,A0 GET RETURN ADDRESS
ADDA.L #12,SP ADJUST STACK
JMP (AO) RETURN
These last two methods have the disadvantage in that they use an address
register that can’t be restored to its prior value before the return.
Stack Fram es
There are a number of problems introduced when parameters are
passed on the stack. Unless the subroutine keeps track of the value of
SP when it was called, it will not be able to find its parameters. For
example, if a subroutine needs to push one or more values on the stack
during its operation, the value of SP will change and the parameters
will not be located at the same relative offsets that they were originally.
Also, high-level languages use the concept of
local or automatic variables.
These are special storage locations that only exist during the time interval
that the subroutine, or a subroutine that it calls, is executing. These local
variables are normally allocated from the stack. This allows such nice
features as recursive subroutine calls. Each time a subroutine is called it
will create a new and distinct set of local variables.
All of this greatly increases the difficulty in keeping track of where
things are on the stack. Each time the stack pointer SP changes, all the
Page view 122
1 2 ... 118 119 120 121 122 123 124 125 126 127 128 ... 255 256

Comments to this Manuals

No comments