Pointers and references: Difference between revisions

m
Line 165:
<lang 68000devpac>LEA myPointers,A1
LEA (4,A1),A1</lang>
 
===Pointers on the stack===
Like any other address register, the stack pointer can be dereferenced and loaded from with a <code>MOVE</code> instruction. The 68000 uses the full stack convention, which means that <code>MOVE.L (SP),D0</code> will load the 4 bytes most recently pushed onto the stack into D0, rather than 4 bytes of "empty space" (read: garbage data). If you execute <code>MOVE.L (SP),D0</code> immediately after calling a subroutine with <code>JSR/BSR</code>, you will load D0 with the value of the program counter prior to the call.
 
If you maintain the stack 32-bit aligned at all times, you can offset the stack by multiples of 4 to get the desired parameters, assuming you pushed them in the reverse order they were declared in your function prototype.
 
<lang 68000devpac>;implements:
;uint64_t foo (uint16_t a,uint16_t b,uint16_t c){return a+b+c;}
MOVE.L #arg2,-(SP)
MOVE.L #arg1,-(SP)
MOVE.L #arg0,-(SP)
JSR foo
LEA (12,SP),SP ;discard the three values pushed prior to the call.
RTS
 
foo:
;outputs to D0
MOVE.L (4,SP),D0
ADD.L (8,SP),D0
ADD.L (12,SP),D0
RTS</lang>
 
===References===
1,489

edits