Parameter Passing: Difference between revisions

no edit summary
(→‎Example Fortran: (f90 intent))
No edit summary
Line 17:
 
The language's choices of parameter passing modes may vary greatly, as the following examples show.
===Example [[6502 Assembly]]===
As is typical of assembly languages, [[6502 Assembly]] doesn't restrict how parameters are passed in any way. Read-only data is of course immutable, but this is just the nature of read-only memory and not a limitation imposed by the language.
 
====Opcodes====
Certain commands such as <code>INC</code>,<code>DEC</code>,<code>ASL</code>,<code>LSR</code>,<code>ROL</code>, and <code>ROR</code> inherently alter the memory address that is used as an argument. Thus their use on a memory address is pass-by-reference.
 
<lang 6502asm>ROL $81 ;rotate left the bits of the value stored at memory address $0081. The old value of $0081 is discarded.
INC $20 ;add 1 to the value at memory address $20.</lang>
 
However, opcodes that use a register as their primary operand are strictly pass-by-value.
 
<lang 6502asm>ADC $81 ;add the value stored at memory address $81 to the accumulator. The value at memory address $81 is unchanged.
;If the carry was set prior to this operation, also add an additional 1 to the accumulator.
 
LDX $20 ;load the value at memory address $20 into the X register.
DEX ;subtract 1 from the value in the X register. This is only a copy. The actual value stored at $20 is unchanged.
 
BIT $2002 ;Set the flags as if the value stored at address $2002 was bitwise ANDed with the accumulator.
;Neither the accumulator nor the value stored at $2002 is changed.
 
LDA $40 ;load the value stored at memory address $40 into the accumulator
ROR A ;rotate right the accumulator. This is only a copy. The actual value stored at $40 is unchanged.</lang>
====Functions====
Whether a function's parameters are pass-by-value or pass-by-reference ultimately depends on how it is written and which opcodes are used.
Generally, parameters are either passed using the stack or with temporary zero-page memory addresses. While these are both forms of memory that are getting overwritten, the actual sources of the data remain the same.
=====Example of in (immutable)=====
<lang 6502asm> lda sourceData
jsr MultiplyBy4
 
 
MultiplyBy4: ; pretend this routine is somewhere far away.
lsr A
lsr A
rts</lang>
=====Example of out (result)=====
This routine reads the joystick output on Commodore 64 and stores it in the output variable <code>joystick1</code>.
<lang 6502asm>ReadJoystick:
lda $DC00
ora #%11100000
sta joystick1
rts</lang>
=====Example of in/out (mutable)=====
This one's tricky because of the way pointers work on the 6502. Unlike the other architectures of its day there are no "address registers" per se, but we can make our own using the zero page.
 
<lang 6502asm> LDA #$03 ;load the low byte of the parameter's address
STA $20 ;store it in the low byte of our pointer variable.
LDA #$00 ;load the high byte.
STA $21 ;store it in the high byte of our pointer variable.
 
;we have to do it this way because a simple "LDA $03" cannot remember the source address after it is executed.
;now we can LDA ($20),y and STA ($20),y to load and store to/from the same place, regardless of where that place actually is.
 
LDY #0 ;6502 requires Y to get involved, set it to 0 to have no offset.
jsr Add32
 
 
Add32: ; pretend this subroutine is somewhere far away.
LDA ($20),y ; the input parameter is actually address $0003
clc
adc #$20 ; 0x20 = decimal 32
sta ($20),y ; overwrite the old value stored in $0003 with the new one.
rts</lang>
 
 
===Example [[Ada]]===
1,489

edits