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

High and low byte indexing question

High and low byte indexing question
by on (#30093)
Right now in my code, I have a simple method of writing strings which are stored under various labels. Here's an example of one I used:

Code:
LDA #low (FirstOrSecond + 9)
STA <StringHolder
LDA #high (FirstOrSecond + 9)
STA <StringHolder + 1
LDX #6
JSR WriteString


That particular one writes the word "Second" on the screen. X is set to 6 as it's characters long. Note that I have +9, which tells it to start at index 9 of that memory location. If not, it would write the string "First ".

My question is, can I use this method but with a variable index? I've tried using Y, but that won't even compile. When I use a variable (something like: (FirstOrSecond + Blah), I get very random output.

Any tips? And no, Blah wouldn't be the real variable name I'd use.

by on (#30095)
Code:
StringHolder = 0  ; in zero page

Message:
        .byte "HELLO",0

main:
        LDA #low (Message)
        STA StringHolder
        LDA #high (Message)
        STA StringHolder+1
        JSR WriteString
        ...

WriteString:
        LDY #0
        JMP first
loop:   JSR WriteChar
        INY
first:  LDA (StringHolder),y
        BNE loop
        RTS

by on (#30097)
if you have a large amount of strings:



Code:
; args: A=string index
; ret:  (none)

StrPtr   = $00
StrLen   = $02


SubWriteString:
   asl
   tax
   lda stringtable, X
   sta StrPtr
   lda stringtable + 1, X
   sta StrPtr + 1

   lda stringtable + 2, X
   sec
   sbc StrPtr
   sta StrLen
   jsr SubWriteData
   rts

   ; ...
   
SubWriteData:

   ; ... use StrLen to loop ...


stringtable:   .dw string1, string2, string3, eos

string1:   .db "HELLO"
string2:   .db "STRING"
string3:   .db "TEST"
eos:

by on (#30098)
never-obsolete wrote:
if you have a large amount of strings:



Code:
stringtable:   .dw string1, string2, string3, eos

string1:   .db "HELLO"
string2:   .db "STRING"
string3:   .db "TEST"
eos:


I never knew you could categorize things like that... Very interesting.