Parameter Passing: Difference between revisions

Content added Content deleted
Line 89: Line 89:
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.
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:
<syntaxhighlight 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 #$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 D0,-(SP) ;push that value onto the stack.
Line 102: Line 103:
MOVE.L (A0),D1 ;dereference the pointer (we're treating it as a pointer to an int)
MOVE.L (A0),D1 ;dereference the pointer (we're treating it as a pointer to an int)
ADD.L D1,D0 ;add the values.
ADD.L D1,D0 ;add the values.
RTS ;and return</lang>
RTS ;and return
</syntaxhighlight>



===Example [[Ada]]===
===Example [[Ada]]===