Noob question about .db

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
kikutano
Posts: 115
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Noob question about .db

Post by kikutano »

Hello again!

I've two declarations like this:

Code: Select all

an_player_idle:
  .db $00,$01,$10,$11,$20,$21,$30,$31,$40,$41

an_player_walk:
  .db $04,$05,$14,$15,$24,$25,$34,$35,$44,$45
So I can set the idle animation in this way:

Code: Select all

SetAnIdle:
  LDX #$01
  LDY #$00
SetAnIdleLoop:
  LDA an_player_idle, y
  STA SPRITE_RAM, x
  INY
  INX
  INX 
  INX
  INX
  CPX #$2D
  BNE SetAnIdleLoop
Now, there is a way to put the address of "an_player_idle" in a variable so I can change it at runtime and so doing something like this:

Code: Select all


  LDA an_player_idle
  STA MY_VARIABLE

SetAnIdleLoop:
  LDA MY_VARIABLE, y
  STA SPRITE_RAM, x
Thanks!
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Noob question about .db

Post by tepples »

You can allocate a 2-byte variable in zero page to hold a pointer to the start of the table and then use the indirect indexed addressing mode (dd),Y to read through the pointer.

Code: Select all

  ; Fill the pointer
  lda #<an_player_idle
  sta myvariable+0
  lda #>an_player_idle
  sta myvariable+1
  ; Index from the pointer
  lda (myvariable),y
Operators < and > extract the low byte (bits 7-0) and the high byte (bits 15-8) of the address used with them.

In NESASM (only), the (dd),Y mode is instead spelled [dd],Y with square brackets.
User avatar
kikutano
Posts: 115
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Re: Noob question about .db

Post by kikutano »

Great! It works! :) Thanks

In NESASM

Code: Select all

; Fill the pointer
  LDA #LOW(an_player_walk)
  STA AN_PLAYER_TO_LOAD+0
  LDA #HIGH(an_player_walk)
  STA AN_PLAYER_TO_LOAD+1

  LDA [AN_PLAYER_TO_LOAD],y
Post Reply