Page 1 of 2

indirect addressing

Posted: Thu Aug 20, 2020 4:57 am
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. :)

Re: indirect addressing

Posted: Thu Aug 20, 2020 5:21 am
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.

Re: indirect addressing

Posted: Thu Aug 20, 2020 5:23 am
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).

Re: indirect addressing

Posted: Thu Aug 20, 2020 5:29 am
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.

Re: indirect addressing

Posted: Thu Aug 20, 2020 6:00 am
by DerTrueForce
Oh. I had a braino moment... I'll see myself out...

Re: indirect addressing

Posted: Thu Aug 20, 2020 6:03 am
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.

Re: indirect addressing

Posted: Thu Aug 20, 2020 12:19 pm
by IamRob
Sorry! Posted to the wrong forum.

Totally unrelated!

Re: indirect addressing

Posted: Thu Aug 20, 2020 12:47 pm
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.

Re: indirect addressing

Posted: Thu Aug 20, 2020 1:54 pm
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.

Re: indirect addressing

Posted: Thu Aug 20, 2020 2:12 pm
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?

Re: indirect addressing

Posted: Thu Aug 20, 2020 2:28 pm
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.

Re: indirect addressing

Posted: Thu Aug 20, 2020 2:30 pm
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.)

Re: indirect addressing

Posted: Thu Aug 20, 2020 2:32 pm
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!

Re: indirect addressing

Posted: Thu Aug 20, 2020 2:39 pm
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.

Re: indirect addressing

Posted: Thu Aug 20, 2020 2:52 pm
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!