Page 2 of 2
Re: indirect addressing
Posted: Fri Aug 21, 2020 9:13 pm
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
Re: indirect addressing
Posted: Fri Aug 21, 2020 9:21 pm
by DanielS
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
Re: indirect addressing
Posted: Fri Aug 21, 2020 9:27 pm
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.
Re: indirect addressing
Posted: Sat Aug 22, 2020 2:18 am
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).
Re: indirect addressing
Posted: Sat Aug 22, 2020 2:29 am
by DanielS
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.

Re: indirect addressing
Posted: Sat Aug 22, 2020 6:48 am
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!
Re: indirect addressing
Posted: Sat Aug 22, 2020 12:22 pm
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.
Re: indirect addressing
Posted: Sat Aug 22, 2020 12:29 pm
by BigEd
Yes, sure, I wasn't saying you had that particular thing!