Jump to content

Parameter Passing: Difference between revisions

No edit summary
Line 83:
sta ($20),y ; overwrite the old value stored in $0003 with the new one.
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]]===
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.