Pointers and references: Difference between revisions

Content added Content deleted
(→‎Double Pointers: Fixed incorrect example - double pointers can only work from zero page memory)
Line 18: Line 18:


<lang 6502>LDA #$30
<lang 6502>LDA #$30
STA $2000
STA $00
LDA #$50
LDA #$50
STA $2001
STA $01


LDA #$80
LDA #$80
STA $2005
STA $05
LDA #$90
LDA #$90
STA $2006
STA $06




Line 31: Line 31:
LDY #$07
LDY #$07


LDA ($2000),y ;the values of $2000 and $2001 are looked up and swapped (this is a little-endian cpu after all)
LDA ($00),y ;the values at $00 and $01 are looked up and swapped (this is a little-endian cpu after all)
;and the value at (that address plus Y) is loaded. In this example, this equates to LDA $5037
;and the value at (that address plus Y) is loaded. In this example, this equates to LDA $5037
LDA ($2000,x) ;x is added to $2000 and the values at that address and the one after are looked up and swapped, and the
LDA ($00,x) ;x is added to $00 and the values at that address and the one after are looked up and swapped, and the
;value at the resulting address is loaded. In this example, this equates to LDA $9080</lang>
;value at the resulting address is loaded. In this example, this equates to LDA $9080</lang>


The 65c02 doesn't need to use X or Y when doing the above method. It can do <code>LDA ($00)</code> which has the same effect as <code>LDA ($00),y</code> when Y = 0.


===Indirect Jumps===
Similar to the above, the 6502 can perform an indirect jump. The byte at the specified address is read, and so is the byte at the address after that. Then they are swapped and concatenated into a new address which is then jumped to.
<lang 6502asm>LDA #$40
STA $2000 ;store the low byte into $2000

LDA #$41
STA $2001 ;store the high byte into $2001

JMP ($2000) ;the resulting command jumps to $4140</lang>

The 65c02 has <code>JMP ($####,X)</code> which adds X to the address before the dereference. This can be used as a "computed goto," selecting a piece of code to jump to from a table of memory locations.

The 65816 has <code>JSR ($####,X)</code> which adds X to the address before the dereference. This can be used as a "computed gosub," selecting a function to call from a table of functions.


=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==