Parameter Passing: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) No edit summary |
Puppydrum64 (talk | contribs) |
||
Line 83: | Line 83: | ||
sta ($20),y ; overwrite the old value stored in $0003 with the new one. |
sta ($20),y ; overwrite the old value stored in $0003 with the new one. |
||
rts</lang> |
rts</lang> |
||
===Example [[68000 Assembly]]=== |
|||
[[68000 Assembly]] is very similar to [[6502 Assembly]] in this regard. |
|||
Parameters are typically passed via the stack or through registers. For human-written assembly, the programmer can pass an argument by reference by passing a pointer to the argument rather than the argument itself. Of course, the function will need to dereference that pointer; however whether the function writes back the new value to that memory address is also decided by the programmer. This means that just because a parameter is passed by reference does not mean that it gets altered by the function that received it. This (contrived) example shows this concept in action. |
|||
<lang 68000devpac>start: |
|||
MOVE.L #$00FF0000,D0 ;load D0 with the pointer 0x00FF0000 (I decided this was a pointer just for example's sake.) |
|||
MOVE.L D0,-(SP) ;push that value onto the stack. |
|||
MOVE.L #$12345678,D0 ;load the constant 0x12345678 into D0 |
|||
MOVE.L D0,-(SP) ;push that value onto the stack. |
|||
JSR foo |
|||
;somewhere else far away from start |
|||
foo: |
|||
MOVE.L (4,SP),D0 ;load the pushed parameter 0x12345678 into D0 |
|||
MOVE.L (8,SP),A0 ;load the pushed parameter 0x00FF0000 into A0 |
|||
MOVE.L (A0),D1 ;dereference the pointer (we're treating it as a pointer to an int) |
|||
ADD.L D1,D0 ;add the values. |
|||
RTS ;and return</lang> |
|||
===Example [[Ada]]=== |
===Example [[Ada]]=== |