High and low byte indexing question

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

Moderator: Moderators

Post Reply
User avatar
Sivak
Posts: 316
Joined: Tue Jul 17, 2007 9:04 am
Location: Somewhere
Contact:

High and low byte indexing question

Post by Sivak »

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: Select all

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.
User avatar
blargg
Posts: 3717
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg »

Code: Select all

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
User avatar
never-obsolete
Posts: 403
Joined: Wed Sep 07, 2005 9:55 am
Location: Phoenix, AZ
Contact:

Post by never-obsolete »

if you have a large amount of strings:


Code: Select all

; 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:
. That's just like, your opinion, man .
User avatar
Sivak
Posts: 316
Joined: Tue Jul 17, 2007 9:04 am
Location: Somewhere
Contact:

Post by Sivak »

never-obsolete wrote:if you have a large amount of strings:


Code: Select all

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.
Post Reply