6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Sep 22, 2024 3:32 pm

All times are UTC




Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: indirect addressing
PostPosted: Fri Aug 21, 2020 9:13 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1948
Location: Sacramento, CA, USA
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:
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

_________________
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)


Last edited by barrym95838 on Fri Aug 21, 2020 9:22 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: indirect addressing
PostPosted: Fri Aug 21, 2020 9:21 pm 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: indirect addressing
PostPosted: Fri Aug 21, 2020 9:27 pm 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: indirect addressing
PostPosted: Sat Aug 22, 2020 2:18 am 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 395
Location: Minnesota
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).


Top
 Profile  
Reply with quote  
 Post subject: Re: indirect addressing
PostPosted: Sat Aug 22, 2020 2:29 am 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
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:


Top
 Profile  
Reply with quote  
 Post subject: Re: indirect addressing
PostPosted: Sat Aug 22, 2020 6:48 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10938
Location: England
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: indirect addressing
PostPosted: Sat Aug 22, 2020 12:22 pm 
Offline

Joined: Wed Aug 12, 2020 2:30 am
Posts: 43
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: indirect addressing
PostPosted: Sat Aug 22, 2020 12:29 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10938
Location: England
Yes, sure, I wasn't saying you had that particular thing!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 21 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: