indirect addressing

Programming the 6502 microprocessor and its relatives in assembly and other languages.
DanielS
Posts: 43
Joined: 12 Aug 2020

indirect addressing

Post by DanielS »

I'm coming along with my walk into C64 assembly. I've hit another block though.

Take this code...

Code: Select all

position_1 .byte $51, $04 ;Points to an address in screen memory
The target address is $0451. I need to store the contents of the accum into that memory location.
I've tried different examples from online docs but none work.

Code: Select all

lda #$20
sta ??position_1??,y ; should resolve to "sta $0451,y"
Y is the indexer in this case (ie. $0451 + Y).
I can't figure out how to get the correct address that's pointed to by the high/low byte pair.

C64 with 64tass assembler.

Thanks for helping an old guy realize a childhood dream. :)
DerTrueForce
Posts: 483
Joined: 04 Jun 2016
Location: Australia

Re: indirect addressing

Post by DerTrueForce »

What you're doing in that first snippet is putting a label at the assembler's current address, then laying down two bytes starting at that position.
The second snippet is correct, if you remove the question marks.
Your assembler should tell you how to assign a value to a label somewhere in its documentation.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: indirect addressing

Post by barrym95838 »

position_1 and position_1+1 must both be in zero page, then

Code: Select all

    lda  #$20
    sta  (position_1),y
should store a $20 in address $0451+y (assuming 64tass uses classic 65xx assembly notation).
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 »

I can put them in zero page. Can I just pick an arbitrary location or... ?

[Edit] Found something that said I can safely use $02-$7f.
DerTrueForce
Posts: 483
Joined: 04 Jun 2016
Location: Australia

Re: indirect addressing

Post by DerTrueForce »

Oh. I had a braino moment... I'll see myself out...
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: indirect addressing

Post by barrym95838 »

DanielS wrote:
Found something that said I can safely use $02-$7f.
Depends on your definition of "safe".

https://sta.c64.org/cbm64mem.html

$02-$06 and $fb-$fe seem quite safe, by my personal definition.
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)
IamRob
Posts: 357
Joined: 26 Apr 2020

Re: indirect addressing

Post by IamRob »

Sorry! Posted to the wrong forum.

Totally unrelated!
Last edited by IamRob on Fri Aug 21, 2020 5:08 am, edited 1 time in total.
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

Using...

Code: Select all

        lda #$51
        sta position_1
        lda #$04
        sta position_1 + 1
        
        lda     square_data,x
        sta     (position_1),y

        position_1 .byte $02 ;Location in zero page
I get the error
square.asm:26:20: error: not a direct page address address '($18be),y'
sta (position_1),y


That being said... the file that has this code assembles fine by itself. I get the error when I assemble the file that includes (.include "foo.asm") this file.

So I don't know if I'm declaring something wrong or if it's a 64tass thing.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: indirect addressing

Post by BigEd »

You need position_1 to be the value $02 - but that's not what you have. You need a directive more like an EQU to set the value of position_1.

What you have is position_1 as a label, and then you've placed $02 at that location. That's the sort of thing you do when you want a constant, or some constants, embedded in your program.
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

BigEd wrote:
You need position_1 to be the value $02 - but that's not what you have. You need a directive more like an EQU to set the value of position_1.

What you have is position_1 as a label, and then you've placed $02 at that location. That's the sort of thing you do when you want a constant, or some constants, embedded in your program.

Could I bother you for a small example?
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: indirect addressing

Post by BigEd »

If I read the manual right, you'd use

Code: Select all

        position_1 = $02 ;Location in zero page
and it might need to be up at the left edge. And it should be before the first point you use it, I think.
Last edited by BigEd on Thu Aug 20, 2020 2:33 pm, edited 1 time in total.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: indirect addressing

Post by BigEd »

Or, an example from the manual, ahead of all of your code:

Code: Select all

*       = $02
position_1	.addr ?         ;a zero page pointer
(But that's very 64tass specific.)
Last edited by BigEd on Thu Aug 20, 2020 2:33 pm, edited 1 time in total.
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

BigEd wrote:
If I read the manual right, you'd use
position_1 = $02 ;Location in zero page
and it might need to be up at the left edge. And it should be before the first point you use it, I think.
Looks like that did it. Thanks!
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: indirect addressing

Post by BillG »

DanielS wrote:
I'm coming along with my walk into C64 assembly. I've hit another block though.

Take this code...

Code: Select all

position_1 .byte $51, $04 ;Points to an address in screen memory
The target address is $0451. I need to store the contents of the accum into that memory location.
I've tried different examples from online docs but none work.

Code: Select all

lda #$20
sta ??position_1??,y ; should resolve to "sta $0451,y"
Y is the indexer in this case (ie. $0451 + Y).
I can't figure out how to get the correct address that's pointed to by the high/low byte pair.
Is $0451 a constant or will it be different as your program runs?

If constant, you can do

Code: Select all

    sta     $0451
or better,

Code: Select all

Screen = $0451
    sta     Screen
If you will be writing to other locations nearby, you can do

Code: Select all

Screen = $0451
    ldy     #Offset
    sta     Screen,Y
which is saying store A at Screen+Offset provided you stay within the range $0451..$0550.
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: indirect addressing

Post by DanielS »

BillG wrote:
If you will be writing to other locations nearby, you can do

Code: Select all

Screen = $0451
    ldy     #Offset
    sta     Screen,Y
which is saying store A at Screen+Offset provided you stay within the range $0451..$0550.
BOOM! Why didn't I think of it this way? This is exactly what I needed. The high/low byte pairs was just complicating something that should be simple. Thanks!
Post Reply