Is it possible to have an array of addresses/labels?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
Kitty_Space_Program
Posts: 50
Joined: Mon Sep 21, 2020 7:42 am

Is it possible to have an array of addresses/labels?

Post by Kitty_Space_Program » Sun Dec 20, 2020 4:57 pm

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

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

Re: Is it possible to have an array of addresses/labels?

Post by tokumaru » Sun Dec 20, 2020 5:50 pm

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:

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

User avatar
Kitty_Space_Program
Posts: 50
Joined: Mon Sep 21, 2020 7:42 am

Re: Is it possible to have an array of addresses/labels?

Post by Kitty_Space_Program » Sun Dec 20, 2020 5:54 pm

ow ok, cool. I wasn't sure if you could use labels in data.

User avatar
Kitty_Space_Program
Posts: 50
Joined: Mon Sep 21, 2020 7:42 am

Re: Is it possible to have an array of addresses/labels?

Post by Kitty_Space_Program » Wed Dec 23, 2020 1:39 pm

I've come across a weird problem with it now, so I have

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

User avatar
rainwarrior
Posts: 8000
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Is it possible to have an array of addresses/labels?

Post by rainwarrior » 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)?

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

Re: Is it possible to have an array of addresses/labels?

Post by tokumaru » Wed Dec 23, 2020 3:49 pm

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:

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

User avatar
Kitty_Space_Program
Posts: 50
Joined: Mon Sep 21, 2020 7:42 am

Re: Is it possible to have an array of addresses/labels?

Post by Kitty_Space_Program » Wed Dec 23, 2020 6:30 pm

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 i figured it was something like that.
tokumaru wrote:
Wed Dec 23, 2020 3:49 pm
look up its documentation
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 :|

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

Re: Is it possible to have an array of addresses/labels?

Post by tokumaru » Wed Dec 23, 2020 8:44 pm

There is definitely a document somewhere explaining the basic syntax and directives. I found this:

https://raw.githubusercontent.com/camsa ... /usage.txt

sdm2
Posts: 46
Joined: Wed Feb 05, 2020 12:59 pm
Location: Poland

Re: Is it possible to have an array of addresses/labels?

Post by sdm2 » Thu Dec 24, 2020 2:28 am

NESASM3:

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

Pokun
Posts: 1746
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Is it possible to have an array of addresses/labels?

Post by Pokun » Fri Dec 25, 2020 5:03 am

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.

turboxray
Posts: 142
Joined: Thu Oct 31, 2019 12:56 am

Re: Is it possible to have an array of addresses/labels?

Post by turboxray » Thu Dec 31, 2020 11:49 am

tokumaru wrote:
Wed Dec 23, 2020 3:49 pm
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:

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

calima
Posts: 1299
Joined: Tue Oct 06, 2015 10:16 am

Re: Is it possible to have an array of addresses/labels?

Post by calima » Thu Dec 31, 2020 11:53 am

Confusing naming there, I would interpret it dwl as "data word low", meaning it takes 16 bits but the upper 8 bits are zero.

turboxray
Posts: 142
Joined: Thu Oct 31, 2019 12:56 am

Re: Is it possible to have an array of addresses/labels?

Post by turboxray » Thu Dec 31, 2020 3:53 pm

calima wrote:
Thu Dec 31, 2020 11:53 am
Confusing naming there, I would interpret it dwl as "data word low", meaning it takes 16 bits but the upper 8 bits are zero.
Yeah. I should have used .dbl and .dbh haha. I might have added .dbb too (data byte bank) - I'll have to check.

Post Reply