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

How to create an array with ASM6?

How to create an array with ASM6?
by on (#197991)
I need to create an array after my code.

For example my code is


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:


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

E4 20

Is there a way to do this?
Re: How to create an array with ASM6?
by on (#197993)
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:

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

  ;load the 5th element of the array
  lda MyArray+4
Re: How to create an array with ASM6?
by on (#197994)
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.

  ;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
  sta (ArrayPointer), y
  bne Loop
  inc ArrayPointer+1 ;after 256 bytes, move the pointer 256 positions ahead
  bne Loop
Re: How to create an array with ASM6?
by on (#198067)
thanks again!!