Is it possible to have an array of addresses/labels?
Moderator: Moderators
- Kitty_Space_Program
- Posts: 50
- Joined: Mon Sep 21, 2020 7:42 am
Is it possible to have an array of addresses/labels?
So, I'm trying to make an overworked map, and I would like to store the map data's locations in rom in a string of data, in nesasm, is there a way to have a table of labels and let the assembler swap in the memory addresses? I have a feeling that [label,x](idexed indirect) might be what I'm looking for here, but I can't figure out what exactly it is from the descriptions I've found except that I would need to put all the addresses onto zero page (which would be a little annoying).
Re: Is it possible to have an array of addresses/labels?
Indexed indirect addressing is indeed meant for lists of pointers, but it's probably not what you're looking for. In fact, this addressing mode is very rarely used in NES programs, because it's just not very versatile. What will probably work for you is a list of addresses in ROM, that you can use an index register (X or Y) to read from and copy one of the addresses to ZP, where you can use indirect indexed addressing ([zp], y). It goes something like this:
This is how you implement multiple level maps and other similar thing that change during the course of a game... Instead of using indexed addressing with hardcoded addresses, you copy the addresses from ROM to RAM and have all the subroutines that access this kind of data use indirect indexed addressing instead.
Code: Select all
;declare a pointer somewhere in ZP
MapAddress = $10
;copy the address of level 4's map to RAM
lda #$04
jsr LoadMapAddress
;get the first address of the selected map
ldy #$00
lda (MapAddress), y
;(...)
LoadMapAddress:
asl ;multiply by 2 because each address is 2 bytes
tax
lda MapAddresses+0, x
sta MapAddress+0
lda MapAddresses+1, x
sta MapAddress+1
rts
MapAddresses:
.word Level0Map, Level1Map, Level2Map, Level3Map, Level4Map, Level5Map ;(...)
- Kitty_Space_Program
- Posts: 50
- Joined: Mon Sep 21, 2020 7:42 am
Re: Is it possible to have an array of addresses/labels?
ow ok, cool. I wasn't sure if you could use labels in data.
- Kitty_Space_Program
- Posts: 50
- Joined: Mon Sep 21, 2020 7:42 am
Re: Is it possible to have an array of addresses/labels?
I've come across a weird problem with it now, so I have
and nesasm gives me the error "Overflow error!" for every row of .db map1, etc. and for the "loadnextmap:" I got "Internal error [1]". I tried to add [] around each term and that gave me a "syntax error in expression", but resolved the internal error. not sure whats going on, i think it has something to do with the way the table is formatted, maybe I need special way to notate 16 bit values in NESASM? Not sure. Any ideas anybody? Thanks.
Code: Select all
mapaddresses:
.db map1,map2,map3,map4
.db map5,map6,map7,map8
.db map9,mapa,mapb,mapc
.db mapd,mape,mape,map0
;each adress is 2 bytes long, so each row is
;-------------------------------------overworldsubroutines-------------------------
loadnextmap:
lda #$20
sta <namtablehighbyte
clc
lda <overworldlocation
asl a
tax
lda mapadresses+0,x
sta <world+0
clc
adc #$10
sta <worldoffset+0
lda mapadresses+1,x
sta <world+1
adc #$00
sta <worldoffset+1;
- rainwarrior
- Posts: 8004
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Is it possible to have an array of addresses/labels?
.db (data byte) is a byte, 8-bits. Addresses are 16-bit. I think you want .dw (data word)?
Re: Is it possible to have an array of addresses/labels?
Yeah, you can't use .db with 16-bit values. You need .dw or .word, depends on what the assembler supports (look up its documentation).
There are people who prefer to split tables of 16-bit values into two tables of 8-bit values. They are faster to use (no need to multiply the index by 2), but slightly more annoying to maintain since there are 2 tables instead of one:
There are people who prefer to split tables of 16-bit values into two tables of 8-bit values. They are faster to use (no need to multiply the index by 2), but slightly more annoying to maintain since there are 2 tables instead of one:
Code: Select all
AddressesLow:
.db LOW(map1), LOW(map2), LOW(map3)
AddressesHigh:
.db HIGH(map1), HIGH(map2), HIGH(map3)
;-------------------------------
;sets up a pointer to map2
ldx #1
lda AddressesLow, x
sta Pointer+0
lda AddressesHigh, x
sta Pointer+1
- Kitty_Space_Program
- Posts: 50
- Joined: Mon Sep 21, 2020 7:42 am
Re: Is it possible to have an array of addresses/labels?
yeah i figured it was something like that.rainwarrior wrote: ↑Wed Dec 23, 2020 2:02 pm.db (data byte) is a byte, 8-bits. Addresses are 16-bit. I think you want .dw (data word)?
yeah, thats been the only problem I've ha with NESASM. the only documentation i have for it is the cpu instructions and addressing types

Re: Is it possible to have an array of addresses/labels?
There is definitely a document somewhere explaining the basic syntax and directives. I found this:
https://raw.githubusercontent.com/camsa ... /usage.txt
https://raw.githubusercontent.com/camsa ... /usage.txt
Re: Is it possible to have an array of addresses/labels?
NESASM3:
you have to be careful about the length of the name "LoadLEVxxx" and the number per line in NESASM3, how too much "syntax error" will pop out.
Code: Select all
LevelJumper .ds 2
LEV_Number .ds 1
;-------------------------
LDA LEV_Number
ASL A
TAX
LDA LEV_TBL+0,x
STA LevelJumper+0
LDA LEV_TBL+1,x
STA LevelJumper+1
JMP [LevelJumper]
;################################################################################
LEV_TBL:
.dw LoadLEV080,LoadLEV001,LoadLEV002,LoadLEV003,LoadLEV004,LoadLEV005,LoadLEV006,LoadLEV007 ;I start level one with 1, not 0
.dw LoadLEV008,LoadLEV009,LoadLEV010,LoadLEV011,LoadLEV012,LoadLEV013,LoadLEV014,LoadLEV015
.dw LoadLEV016,LoadLEV017,LoadLEV018,LoadLEV019,LoadLEV020,LoadLEV021,LoadLEV022,LoadLEV023
.dw LoadLEV024,LoadLEV025,LoadLEV026,LoadLEV027,LoadLEV028,LoadLEV029,LoadLEV030,LoadLEV031
.dw LoadLEV032,LoadLEV033,LoadLEV034,LoadLEV035,LoadLEV036,LoadLEV037,LoadLEV038,LoadLEV039
.dw LoadLEV040,LoadLEV041,LoadLEV042,LoadLEV043,LoadLEV044,LoadLEV045,LoadLEV046,LoadLEV047
.dw LoadLEV048,LoadLEV049,LoadLEV050,LoadLEV051,LoadLEV052,LoadLEV053,LoadLEV054,LoadLEV055
.dw LoadLEV056,LoadLEV057,LoadLEV058,LoadLEV059,LoadLEV060,LoadLEV061,LoadLEV062,LoadLEV063
.dw LoadLEV064,LoadLEV065,LoadLEV066,LoadLEV067,LoadLEV068,LoadLEV069,LoadLEV070,LoadLEV071
.dw LoadLEV072,LoadLEV073,LoadLEV074,LoadLEV075,LoadLEV076,LoadLEV077,LoadLEV078,LoadLEV079
.dw LoadLEV080
Re: Is it possible to have an array of addresses/labels?
Here is NESASM source and all documentation it usually comes with ever since the MagicKit, although the names of some files have changed.
They include: "usage.txt", "history.txt", "cpu_instructions.txt", "ines_header_format.txt" and "nes_technical_documentation.txt".
The usage documentation is brief and incomplete, but it at least explains basic usage and syntax.
They include: "usage.txt", "history.txt", "cpu_instructions.txt", "ines_header_format.txt" and "nes_technical_documentation.txt".
The usage documentation is brief and incomplete, but it at least explains basic usage and syntax.
Re: Is it possible to have an array of addresses/labels?
Which is why I added .dwl and .dwh to PCEAS (and by extension NESASM). It's the same as .db, but stores just the low or high byte of a word.tokumaru wrote: ↑Wed Dec 23, 2020 3:49 pmYeah, you can't use .db with 16-bit values. You need .dw or .word, depends on what the assembler supports (look up its documentation).
There are people who prefer to split tables of 16-bit values into two tables of 8-bit values. They are faster to use (no need to multiply the index by 2), but slightly more annoying to maintain since there are 2 tables instead of one:
Code: Select all
AddressesLow: .db LOW(map1), LOW(map2), LOW(map3) AddressesHigh: .db HIGH(map1), HIGH(map2), HIGH(map3) ;------------------------------- ;sets up a pointer to map2 ldx #1 lda AddressesLow, x sta Pointer+0 lda AddressesHigh, x sta Pointer+1
Re: Is it possible to have an array of addresses/labels?
Confusing naming there, I would interpret it dwl as "data word low", meaning it takes 16 bits but the upper 8 bits are zero.