It is currently Mon Dec 11, 2017 9:43 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Sun Jun 11, 2017 6:32 pm 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 39
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?


Top
 Profile  
 
PostPosted: Sun Jun 11, 2017 6:46 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10164
Location: Rio de Janeiro - Brazil
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


Top
 Profile  
 
PostPosted: Sun Jun 11, 2017 7:04 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10164
Location: Rio de Janeiro - Brazil
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


Top
 Profile  
 
PostPosted: Mon Jun 12, 2017 5:31 pm 
Offline
User avatar

Joined: Sun Jun 11, 2017 5:39 pm
Posts: 39
thanks again!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group