This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

ASM6 - How to have an array pointer position?

ASM6 - How to have an array pointer position?
by on (#239997)
Hello,

I have an array like this

;Array_1
HEX 01 04 07

Those are address. I would need to be able to define each position of this array with a name. For example,

Mario_Sprite_ID takes the value of 01 (because it refers to HEX 01)
Goomba_Sprite_ID takes the value of 02 (because it refers to HEX 02)

So those values (Mario_Sprite_ID and Goomba_Sprite_ID) are pointers to read from Array_1

I really hope it's clear, not sure how to explain it
Re: ASM6 - How to have an array pointer position?
by on (#240003)
Something like this?

Code:
Array_1:
Mario_Sprite_ID:
    HEX 01
Goomba_Sprite_ID:
    HEX 04
Whatever:
    HEX 07
Re: ASM6 - How to have an array pointer position?
by on (#240007)
You can also do an enum if the values are sequential.

And you can define constants with = if they aren't sequential.

Mario_Sprite_ID = 1
Re: ASM6 - How to have an array pointer position?
by on (#240009)
If you use the HEX approach, you'll be using ROM space to define these values. If they are supposed to be used as data in the game, that's fine, but if these are meant to be used as constants in the game's logic, it's better to define these values as assembler symbols, in one of the following ways:

Code:
Mario_Sprite_ID = $01
Goomba_Sprite_ID = $02
Something_Sprite_ID = $03


Code:
Mario_Sprite_ID .equ $01
Goomba_Sprite_ID .equ $02
Something_Sprite_ID .equ $03


Code:
.enum $01
Mario_Sprite_ID .dsb 1
Goomba_Sprite_ID .dsb 1
Something_Sprite_ID .dsb 1
.ende


All of these will create sequential symbols that you can use anywhere in the code, without using ROM space. For example:

Code:
;set the variable to the ID of the goomba sprite
lda #Goomba_Sprite_ID
sta Sprite_ID


Would you mind telling us what this is for? This would possibly help us give more adequate suggestions.
Re: ASM6 - How to have an array pointer position?
by on (#240125)
Thanks to all for your answer!!

What I want is actually to be able to pick a sprite, let's say mario sprite and do

LDA ArrayTable1 + #MarioSpriteID

I think that with DRW's answer I could do it:

MarioAbsoluteSpriteID = Mario_Sprite_DI - Array_1

where Mario_Sprite_DI and Array_1 are taken from DWR answer.
So MarioAbsoluteSpriteID would be dynamic (which is not in tokumaru's answer). I want it to be dynamic in case I add others pointers in the array.
Re: ASM6 - How to have an array pointer position?
by on (#240209)
Ok I was successful in doing what I wanted, but I wondered if there was a better way to do it:

ARRAY__LOW_ASMs:

;here is the low pointer for each ASM location
HEX 00 00

;Following line is used to make calculs later
ASM_PTR_LOCATION__GOOMBA:

DB <ASM__GOOMBA
HEX 00

ARRAY__HIGH_ASMs:

;here is the high pointer for each ASM location
HEX 00 00
DB >ASM__GOOMBA
HEX 00

T=ASM_PTR_LOCATION__GOOMBA - ARRAY__ASMs
DB <T
DB <T

------------------
So here T = #$02, which is what I want, since it is location of high and low byte of RAM address of GOOMBA ASM, which I can now access

LDY T
LDA ARRAY__LOW_ASMs,Y
STA $01
LDA ARRAY__HIGH_ASMs,Y
STA $00

JMP ($00)

----------------------------------

But I fin't it weird the way it's done can I do it better?
Re: ASM6 - How to have an array pointer position?
by on (#240210)
We have an article on the wiki about this: nesdevwiki:Jump table
Re: ASM6 - How to have an array pointer position?
by on (#240211)
I read all of it but unless I don,t see something on the site, it doesn't answer my question?
What I want is every index in the array to be referenced by a variable, and I see nothing to do the equivalent on the page
Re: ASM6 - How to have an array pointer position?
by on (#240212)
Could you express what you want to happen in C or in any other non-assembly programming language in which you are more comfortable, so that others can translate your intent into assembly language?
Re: ASM6 - How to have an array pointer position?
by on (#240213)
Reading more closely, he already has the jump table set up. He wants an "automated" way of converting index within that jump table to a symbol for use in the assembler.

Since this is asm6, I'm out of my depth.
Re: ASM6 - How to have an array pointer position?
by on (#240214)
lidnariq wrote:
Reading more closely, he already has the jump table set up. He wants an "automated" way of converting index within that jump table to a symbol for use in the assembler.

Since this is asm6, I'm out of my depth.


Thanks!! I didn't know how to explain it simply, I did try to explain it twice above but I think it wasn't clear haha
Re: ASM6 - How to have an array pointer position?
by on (#240220)
I don't know how to do this in Asm6, but in C, macros are always quite helpful if you want automated stuff.

For example, if you have enemy arrays with data and then you have one array that stores all the enemy addresses and then you want to use names for the index for this array, you can do this:
Code:
const byte GoombaData[] = { ... };
const byte KoopaData[] = { ... };
const byte BooData[] = { ... };

#define ENEMIES(postfix)\
    Goomba##postfix,
    Koopa##postfix,
    Boo##postfix

/* All enemy IDs that are also the indices for the array below. */
enum
{
    ENEMIES(Id),
    EnemyCount
}

/* Pointers to all enemy arrays. */
const byte *const AllEnemies[] =
{
    ENEMIES(Data)
};

Now you can do enemy = AllEnemies[KoopaId]; or you can do
for (i = 0; i < EnemyCount; ++i)
enemy = AllEnemies[i];


Also, this way, whenever you have a new enemy, just add a value to the macro and the compiler will automatically tell you what array definition you're missing.
And if you shift the enemy order around, the IDs and AllEnemies entries will still match.

If Asm6 supports macros, I assume you can do a similar thing: One macro where all enemy names plus pre- or postfixes are listed. And whenever you need all enemies in order, you simply call this macro with its corresponding pre- and postfix parameters.
Re: ASM6 - How to have an array pointer position?
by on (#240247)
Looks nice, though I have no idea how to do such things in ASM6
Re: ASM6 - How to have an array pointer position?
by on (#240259)
I know that cc65 can do macros, enums and this stuff where I put together a name.
Although in cc65 it's not done as nicely as C because you have to use .string, .concat, .ident etc. Which makes it much uglier than simply putting the names together as with ##.

Whether ASM6 can do this, I don't know, but someone else here can surely tell you.