nesdev.com
http://forums.nesdev.com/

How to create an array with ASM6?
http://forums.nesdev.com/viewtopic.php?f=2&t=16069
Page 1 of 1

Author:  MartsINY [ Sun Jun 11, 2017 6:32 pm ]
Post subject:  How to create an array with ASM6?

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?

Author:  tokumaru [ Sun Jun 11, 2017 6:46 pm ]
Post subject:  Re: How to create an array with ASM6?

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:
  ;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:
  ;load the 5th element of the array
  lda MyArray+4

Author:  tokumaru [ Sun Jun 11, 2017 7:04 pm ]
Post subject:  Re: How to create an array with ASM6?

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

Author:  MartsINY [ Mon Jun 12, 2017 5:31 pm ]
Post subject:  Re: How to create an array with ASM6?

thanks again!!

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/