How do I get the low byte of a data table address?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Venutech
Posts: 11
Joined: Mon Jan 30, 2023 12:13 pm

How do I get the low byte of a data table address?

Post by Venutech »

For the purposes of disassembling a rom;
I am able to reference a table and create a jump table using .word and the label for the table.
However, sometimes, only the low byte (and separately the high byte) is referenced in code.
How do I get the bytes separately using labels?

For example

some_table: ; starting at $8050

Lda #$50 ; low byte of address I want to refernce
Sta $00 ; store low byte in $00
Lda #80 ; high byte of address I want to reference
Sta $01 ; store high byte in $01
Lda ($00),y ; get stuff from the table

I want to remove the hard coded address, ultimately.
Can I do something like this:

Lda #<some_table ; to get the low byte of tbl addr
Sta $00
Lda #>some_table ; for the high byte
Sta $01
Lda ($00),y
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How do I get the low byte of a data table address?

Post by tokumaru »

You can have a table of words where elements are accessed using even indices (0 = first element, 2 = second element, 4 = third element, etc.):

Code: Select all

AddressTable:
  .word Address0, Address1, Address2, Address3

  ldx #$02 ;prepares to access the second element
  lda AddressTable+0, x
  sta $00
  lda AddressTable+1, x
  sta $01
Using even indices can be inconvenient sometimes, so programmers may choose to use 2 separate tables, one for the low bytes and one for the high bytes:

Code: Select all

AddressTableLow:
  .byte <Address0, <Address1, <Address2, <Address3

AddressTableHigh:
  .byte >Address0, >Address1, >Address2, >Address3

  ldx #$01 ;prepares to access the second element
  lda AddressTableLow, x
  sta $00
  lda AddressTableHigh, x
  sta $01
Venutech
Posts: 11
Joined: Mon Jan 30, 2023 12:13 pm

Re: How do I get the low byte of a data table address?

Post by Venutech »

tokumaru wrote: Tue Feb 07, 2023 11:45 am programmers may choose to use 2 separate tables...
I've seen this used in other games, thanks for explaining why thats used.
My problem is the addresses are sometimes hard coded, I was wondering if there was a shorthand to address them directly without a table in the source code.
I should've mentioned that I'm using cl65.
I'll check the documentation
Venutech
Posts: 11
Joined: Mon Jan 30, 2023 12:13 pm

Re: How do I get the low byte of a data table address?

Post by Venutech »

I checked the documentation for cc65:
10.9 .HIBYTE
The function returns the high byte (that is, bits 8-15) of its argument. It works identical to the '>' operator.
See: .LOBYTE, .BANKBYTE

Although, using .hibyte (and .lobytr) sprung an error on compiling, using <some_table and >some_table worked exactly like I expected it to.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How do I get the low byte of a data table address?

Post by tokumaru »

Being functions, .hibyte and .lobyte probably require the argument to be placed between parentheses (e.g. .lobyte(Label)). I personally don't see much reason to use these functions, seeing as < and > are so much more compact.
Post Reply