ASM6 - How to have an array pointer position?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

ASM6 - How to have an array pointer position?

Post by MartsINY » Thu Jun 20, 2019 6:45 pm

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

User avatar
DRW
Posts: 1913
Joined: Sat Sep 07, 2013 2:59 pm

Re: ASM6 - How to have an array pointer position?

Post by DRW » Fri Jun 21, 2019 12:59 am

Something like this?

Code: Select all

Array_1:
Mario_Sprite_ID:
    HEX 01
Goomba_Sprite_ID:
    HEX 04
Whatever:
    HEX 07
Available now: My game "City Trouble".
Sales website: https://megacatstudios.com/products/city-trouble
Gameplay: https://youtu.be/Eee0yurkIW4
Download website: http://www.denny-r-walter.de/city.htm

User avatar
dougeff
Posts: 2614
Joined: Fri May 08, 2015 7:17 pm
Location: DIGDUG
Contact:

Re: ASM6 - How to have an array pointer position?

Post by dougeff » Fri Jun 21, 2019 5:25 am

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
nesdoug.com -- blog/tutorial on programming for the NES

User avatar
tokumaru
Posts: 11466
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: ASM6 - How to have an array pointer position?

Post by tokumaru » Fri Jun 21, 2019 6:25 am

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: Select all

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

Code: Select all

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

Code: Select all

.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: Select all

;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.

User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: ASM6 - How to have an array pointer position?

Post by MartsINY » Tue Jun 25, 2019 8:40 am

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.

User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: ASM6 - How to have an array pointer position?

Post by MartsINY » Wed Jun 26, 2019 5:23 pm

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?

lidnariq
Posts: 8780
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: ASM6 - How to have an array pointer position?

Post by lidnariq » Wed Jun 26, 2019 5:26 pm

We have an article on the wiki about this: nesdevwiki:Jump table

User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: ASM6 - How to have an array pointer position?

Post by MartsINY » Wed Jun 26, 2019 6:04 pm

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

tepples
Posts: 21750
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: ASM6 - How to have an array pointer position?

Post by tepples » Wed Jun 26, 2019 6:12 pm

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?

lidnariq
Posts: 8780
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: ASM6 - How to have an array pointer position?

Post by lidnariq » Wed Jun 26, 2019 6:19 pm

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.

User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: ASM6 - How to have an array pointer position?

Post by MartsINY » Wed Jun 26, 2019 6:32 pm

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

User avatar
DRW
Posts: 1913
Joined: Sat Sep 07, 2013 2:59 pm

Re: ASM6 - How to have an array pointer position?

Post by DRW » Wed Jun 26, 2019 11:14 pm

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: Select all

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;


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.
Available now: My game "City Trouble".
Sales website: https://megacatstudios.com/products/city-trouble
Gameplay: https://youtu.be/Eee0yurkIW4
Download website: http://www.denny-r-walter.de/city.htm

User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: ASM6 - How to have an array pointer position?

Post by MartsINY » Thu Jun 27, 2019 5:53 pm

Looks nice, though I have no idea how to do such things in ASM6

User avatar
DRW
Posts: 1913
Joined: Sat Sep 07, 2013 2:59 pm

Re: ASM6 - How to have an array pointer position?

Post by DRW » Thu Jun 27, 2019 11:56 pm

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.
Available now: My game "City Trouble".
Sales website: https://megacatstudios.com/products/city-trouble
Gameplay: https://youtu.be/Eee0yurkIW4
Download website: http://www.denny-r-walter.de/city.htm

Post Reply