Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user Nicron ! (Registered 2024-05-21) You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Want to understand this way of looping through a table
2023-10-13 21:16
Flashback

Registered: Dec 2009
Posts: 4
Want to understand this way of looping through a table

Hi,

I would like some explanation about this way of looping a colour table:

colourcycle.asm

Basically it's this section I can't wrap my head around:
...
txa     ; select next colour index
adc #01
and #07 ; loop colour index
tax
...
colours  .byte 09, 08, 05, 13, 01, 13, 05, 08 ; colour table


As far as I understand, the start color is saved in a ZP address (startcol) which will is changed each frame. This is done by txa/ adc #$01 I guess. (BTW, why is there no clc before adc here?) But why do we need the and #$07?

Regards
 
... 27 posts hidden. Click here to view all posts....
 
2023-10-16 10:33
Bitbreaker

Registered: Oct 2002
Posts: 501
i'd favour using sbx aswell when it is about incrementing x by any number and pplying an and operation.

Another option is to move the end condition of the loop into the table, and wrap the index depending on that. Color-register writes usually only need the lownibble.

        ...
        lda tab,x
        bpl +
        ldx #$ff
+
        inx
        ...

tab     !byte 9,8,5,13,1,13,5,$f8
2023-10-16 12:10
chatGPZ

Registered: Dec 2001
Posts: 11147
Or just count the index backwards :)
2023-10-16 13:06
Gordian

Registered: May 2022
Posts: 36
Another approach:

ldx index
...
lda #7
inx
sax index
2023-10-16 14:46
Copyfault

Registered: Dec 2001
Posts: 467
I'd say it won't get any shorter than doing it via sbx...

Hmm, maybe your table values allow for getting rid of that "lda #$07", but this only works for specific values, just as Bitbreaker mentioned in his post above.
2023-10-16 15:03
Gordian

Registered: May 2022
Posts: 36
Yes, not shorter, just another solution.
If accumulator is utilized before LDA or it's just not needed anymore, there are no cons.
Or LDY is used for getting values.
2023-10-16 15:55
Frostbyte

Registered: Aug 2003
Posts: 174
If you need to persist the value, is Gordian's solution then the shortest?

ldx zp_val	// 3
lda #$07	// 2
sbx #$ff	// 2
stx zp_val	// 3
// ...		// X = 1 to 8 here
// 8b, 10c

vs.
ldx zp_val	// 3
// ...		// X = 0 to 7 here
lda #$07	// 2
inx		// 2
sax zp_val	// 3
// ...		// X = 1 to 8 here
// 7b, 10c
2023-10-16 18:16
Copyfault

Registered: Dec 2001
Posts: 467
Quoting Frostbyte
If you need to persist the value, is Gordian's solution then the shortest?

ldx zp_val	// 3
lda #$07	// 2
sbx #$ff	// 2
stx zp_val	// 3
// ...		// X = 1 to 8 here
// 8b, 10c

vs.
ldx zp_val	// 3
// ...		// X = 0 to 7 here
lda #$07	// 2
inx		// 2
sax zp_val	// 3
// ...		// X = 1 to 8 here
// 7b, 10c
Yes, it's one byte shorter when you take the store commands to some temporary register into account. My thinking was about a loop that does not change X in its inner part, thus without any need for storing it inbetween.
2023-10-16 19:47
Gordian

Registered: May 2022
Posts: 36
Hi!

As I wrote above, lda #$07 can be moved outside inner loop:

lda #$07

ldx zp_val
;ldy table_value,x
;sty somewhere
inx
sax zp_val


So we have 2b and 2c shorter version.
2023-10-16 23:25
Copyfault

Registered: Dec 2001
Posts: 467
Quoting Gordian
Hi!

As I wrote above, lda #$07 can be moved outside inner loop:

lda #$07

ldx zp_val
;ldy table_value,x
;sty somewhere
inx
sax zp_val


So we have 2b and 2c shorter version.
In some rare cases, this might be possible, but usually accu is needed in an inner loop. I still think it'd be more feasible to get rid of the ldx/stx-opcodes (not saying that this is easier than to avoid usage of the accu in the inner loop). In the end, it all depends on what your loop is actually needed for.
2023-10-17 09:04
Rastah Bar

Registered: Oct 2012
Posts: 336
A bit similar to what Bitbreaker and chatGPZ said, but X is not cluttered and it works for values other than colors too:

	dec get_number:+1	;6
	bpl get_number:		;2/3
	lda #max_index		;2
	sta get_number:+1	;4

get_number:
	lda table

;table at page boundary
table
 BYTE number1, number2, …


- Works for any table size < 130;
- Uses ((tabel size-1)*9 + 14)/(table size) cycles on average, which is smaller than 10 cycles for tables with more than 5 elements;
- X (and Y) is not cluttered.
Previous - 1 | 2 | 3 | 4 - Next
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Icon/TRIAD
B.A.
Visage/Lethargy
megasoftargentina
Linus/MSL
dlee
DeeKay/Crest
icon/The Silents, Sp..
lA-sTYLe/Quantum
Genius/Xenon
taper/ΤRIΛD
Guests online: 97
Top Demos
1 Next Level  (9.8)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.7)
5 Edge of Disgrace  (9.6)
6 Comaland 100%  (9.6)
7 Uncensored  (9.6)
8 No Bounds  (9.6)
9 Bromance  (9.5)
10 Wonderland XII  (9.5)
Top onefile Demos
1 Layers  (9.6)
2 It's More Fun to Com..  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 TRSAC, Gabber & Pebe..  (9.5)
7 Rainbow Connection  (9.5)
8 Dawnfall V1.1  (9.5)
9 Quadrants  (9.5)
10 Daah, Those Acid Pil..  (9.5)
Top Groups
1 Oxyron  (9.3)
2 Booze Design  (9.3)
3 Censor Design  (9.3)
4 Crest  (9.3)
5 Performers  (9.3)
Top Logo Graphicians
1 Sander  (9.9)
2 Facet  (9.6)
3 Mermaid  (9.4)
4 Pal  (9.4)
5 Shine  (9.3)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.041 sec.