Parameter Passing: Difference between revisions

Content added Content deleted
Line 23: Line 23:
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.
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.


<syntaxhighlight lang="6502asm">
<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>
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.
</syntaxhighlight>


However, opcodes that use a register as their primary operand are strictly pass-by-value.
However, opcodes that use a register as their primary operand are strictly pass-by-value.


<syntaxhighlight lang="6502asm">
<lang 6502asm>ADC $81 ;add the value stored at memory address $81 to the accumulator. The value at memory address $81 is unchanged.
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.
;If the carry was set prior to this operation, also add an additional 1 to the accumulator.


Line 39: Line 42:


LDA $40 ;load the value stored at memory address $40 into the accumulator
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>
ROR A ;rotate right the accumulator. This is only a copy. The actual value stored at $40 is unchanged.
</syntaxhighlight>


The main takeaway from this is that by default, the use of <code>LDA/LDX/LDY</code> commands to load from memory is pass-by-value. In order to actually update the memory location, you must store the value back into the source memory address.
The main takeaway from this is that by default, the use of <code>LDA/LDX/LDY</code> commands to load from memory is pass-by-value. In order to actually update the memory location, you must store the value back into the source memory address.
Line 60: Line 64:
=====Example of out (result)=====
=====Example of out (result)=====
This routine reads the joystick output on Commodore 64 and stores it in the output variable <code>joystick1</code>.
This routine reads the joystick output on Commodore 64 and stores it in the output variable <code>joystick1</code>.
<lang 6502asm>ReadJoystick:
<syntaxhighlight lang="6502asm">
ReadJoystick:
lda $DC00
lda $DC00
ora #%11100000
ora #%11100000
sta joystick1
sta joystick1
rts</lang>
rts
</syntaxhighlight>

=====Example of in/out (mutable)=====
=====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.
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>
<syntaxhighlight lang="6502asm">
LDA #$03 ;load the low byte of the parameter's address
LDA #$03 ;load the low byte of the parameter's address
STA $20 ;store it in the low byte of our pointer variable.
STA $20 ;store it in the low byte of our pointer variable.
Line 86: Line 93:
adc #$20 ; 0x20 = decimal 32
adc #$20 ; 0x20 = decimal 32
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
</syntaxhighlight>


===Example [[68000 Assembly]]===
===Example [[68000 Assembly]]===