How to create an array with ASM6?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

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

How to create an array with ASM6?

Post by MartsINY »

I need to create an array after my code.

For example my code is

LDA $EF

then I would need to have an array there that contains let's say 2 hex code.

E4 20

However, I have 2 problems:

1-) I would need to be able to give a label to this table so I can use it.

For example:

LDA $EF

array1:
E4 20

LDA array1,Y

2-) The other problem is that the array would need to be after it is used, like this




LDA array1,1

array1:
E4 20

Is there a way to do this?
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to create an array with ASM6?

Post by tokumaru »

In ASM, an array is just an alias to the address of its first element, just like any other variable, so you have to be careful to not declare any other variables before the end of the array, or the overlap will result in buggy code.

To access arrays of length 256 or less, you can use one of the index registers:

Code: Select all

  ;load the third byte of the array
  ldx #$02
  lda MyArray, x
If the index is known at assembly time, you can avoid using the index register and add the constant offset to the base address at assembly time:

Code: Select all

  ;load the 5th element of the array
  lda MyArray+4
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to create an array with ASM6?

Post by tokumaru »

Arrays larger than 256 elements have to be accessed with indirect addressing. This means that the address of the array is stored in RAM (in Zero Page, to be precise), and can be manipulated to point any other part of the array, not just the beginning.

Code: Select all

  ;declare array in RAM
  MyArray = $0400

  ;declare pointer in ZP
  ArrayPointer = $007B

  ;initialize the pointer
  lda #<MyArray
  sta ArrayPointer+0
  lda #>MyArray
  sta ArrayPointer+1

  ;fill 768 bytes (256 *3) with 0
  lda #$00
  ldx #$03
  ldy #$00
Loop:
  sta (ArrayPointer), y
  iny
  bne Loop
  inc ArrayPointer+1 ;after 256 bytes, move the pointer 256 positions ahead
  dex
  bne Loop
User avatar
MartsINY
Posts: 66
Joined: Sun Jun 11, 2017 5:39 pm

Re: How to create an array with ASM6?

Post by MartsINY »

thanks again!!
Post Reply