indirect addressing

Programming the 6502 microprocessor and its relatives in assembly and other languages.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: indirect addressing

Post by barrym95838 »

Going back a tiny bit to the subject of "safe" zero-page locations. One possible technique is to simply save and restore the locations you wish to use on a per-subroutine basis. Sure, it costs several bytes and several cycles, but if it's done outside any high frequency loops, it can make your code a bit safer and more portable without a significant impact on performance (as long as you don't get tangled up in any interrupt clashes, which is possible but would probably require a big dose of bad luck).

Code: Select all

subroutine:
    lda  $02
    pha
    lda  $03
    pha
;   ...
; the meat of your subroutine, which uses $02 and $03 as a temporary pointer
;   ...
    pla
    sta  $03
    pla
    sta  $02
    rts
Last edited by barrym95838 on Fri Aug 21, 2020 9:22 pm, edited 1 time in total.
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

barrym95838 wrote:
Going back a tiny bit to the subject of "safe" zero-page locations. One possible technique is to simply save and restore the locations you wish to use on a per-subroutine basis. Sure, it costs several bytes and several cycles, but if it's done outside any high frequency loops, it can make your code a bit safer and more portable (as long as you don't get tangled up in any interrupt clashes, which is possible but would probably require a big dose of bad luck).

Code: Select all

subroutine:
    lda  $02
    pha
    lda  $03
    pha
;   ...
; the meat of your subroutine, which uses $02 and $03 as a temporary pointer
;   ...
    pla
    sta  $03
    pla
    sta  $02
    rts
Sure thing. That's exactly what I'm doing (minus the stack part).
I'm learning this by developing a simple tac tac toe game. So far I have it drawing the game screen and I can move a selection box around the playing area (to select a square for an X or O). Coming from a C++ background, this C64 assembly slaps me in the face every little bit. haha
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

I'm using CBM prg Studio (just for its project functions and debugger integration). It's frustrating that it doesn't allow external assemblers though. I like 64tass. The CBM prg assembler leaves a little to be desired. I don't like its screen or sprite editors though so I use separate apps for those.

I'm open to suggestions for other editors. I like Sublime but I haven't got it set up for C64 assembler. Relaunch64 is ok but it seems a bit buggy at times.
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

Re: indirect addressing

Post by teamtempest »

6502 indirect address mode has many practical uses, the most important of which is that you can use it to get around the limits of having only 8-bit index registers (instead of 16-bit ones which could reach the whole address space). However if you don't need to index any farther than 255 bytes "above" a base address and you know that base address won't change while your program is running, then you can use "absolute" (if you don't need indexing at all) or "absolute,x" or "absolute,y" (if you do) address modes.

(yes, if your program is in RAM and not ROM, you could also merrily patch an absolute base address at run time to point anywhere you want. But that's pretty advanced stuff for someone just starting out. From there it's only a short step to writing a linker).
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

teamtempest wrote:
6502 indirect address mode has many practical uses, the most important of which is that you can use it to get around the limits of having only 8-bit index registers (instead of 16-bit ones which could reach the whole address space). However if you don't need to index any farther than 255 bytes "above" a base address and you know that base address won't change while your program is running, then you can use "absolute" (if you don't need indexing at all) or "absolute,x" or "absolute,y" (if you do) address modes.

(yes, if your program is in RAM and not ROM, you could also merrily patch an absolute base address at run time to point anywhere you want. But that's pretty advanced stuff for someone just starting out. From there it's only a short step to writing a linker).
I'm glow in the dark green. :lol:
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: indirect addressing

Post by BigEd »

One insight here is the question of when do you know what a value will be: you might know the value at the time you type the code in; the value might be known during assembly; the value might become known at the time your program is loaded; the value might become fixed only while the program is running.

Another way to look at it is if you need a pointer, you'll need to store two bytes in zero page and used an indexed addressing mode.

I think quite a lot of beginner C programmers have trouble getting the idea of a pointer. In assembly there's a similar bridge to cross: you have values, and you have addresses, and they get used in certain ways.

In both cases, I think, the beginner will probably start by picking up some idiomatic usages which seem to work, and eventually the light bulb will go on and they really understand.

It's normal to have to go through these stages!
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

Coming from a C++ background, pointers are not new to me. Although I get what you're saying. It took me a while to get a handle on the concept.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: indirect addressing

Post by BigEd »

Yes, sure, I wasn't saying you had that particular thing!
Post Reply