Today's beginner question - Arrays in Assembly (SOLVED)

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
Post Reply
Spinnetti
Posts: 255
Joined: Sat Sep 12, 2020 11:29 pm

Today's beginner question - Arrays in Assembly (SOLVED)

Post by Spinnetti »

My loading and reading arrays for tracking sprites wasn't working so I broke it down to this simplified example. Thanks to your help, its working now after a couple dumb blunders! Thanks! This is working below now as expected

SPRITETABLE: DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$FF,$FF


INITSPRITES: ;Load up the empty slots in the table. on first run all empty, so don't need to check for filled slots
LD HL,($400C) ;Set the first printable spot on the screen
INC HL
LD BC,SPRITETABLE
LOADLOOP:
LD A,(BC) ;Load first byte of the array
CP $FF ;End of table?
JP Z,PRINTSPRITE ;Table filled, exit
LD A,H ;Save the position pointer. BC pointing at empty entry in table and HL has the randomized starting address for the grunt
LD (BC),A
INC BC
LD A,L
LD (BC),A
INC BC ;Skip to the next pointer
JP LOADLOOP ;Repeat until end of table
PRINTSPRITE: ;Cycle through the sprite table and print ones that exist, ignoring dead ones (empty) and bailing out when reaching table end ($FF)
LD BC,SPRITETABLE ;Pointer to the first sprite
PRINTLOOP:
LD A,(bc) ;Make sure not the end of a table or empty
CP $FF ;End of table
RET Z ;Done printing for this cycle - return
OR A ;sprite is "Dead"
JP NZ,PRINTS ;Valid pointer so print sprite
INC bc ;Nothing there, so move to the next pointer
INC bc
JP PRINTLOOP ;Skip to the next pointer
PRINTS:
LD (HL),$26
inc hl ;move to next spot on screen
INC bc ;Go to the next pointer
INC bc
JP PRINTLOOP
Last edited by Spinnetti on Tue Feb 08, 2022 6:42 pm, edited 3 times in total.
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+,Printer
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
roganjosh
Posts: 100
Joined: Thu Jun 14, 2018 12:59 pm

Re: Today's beginner question - Arrays in Assembly

Post by roganjosh »

Two odd things:

Code: Select all

INC BC ;Skip two bytes to get us to the next pointer
INC BC
JP LOADLOOP ;Repeat until end of table
You're incrementing one too many here as you incremented BC earlier.

Secondly, in the code below, HL is pointing to a sprite table entry but you're then overwriting the first byte of the entry with whatever DE is pointing to. I'm not sure that was your intention. It isn't doing any printing.

Code: Select all

PRINTS:
LD A,(DE) ;Load up the sprite to print (for this example, assume that DE points to a regular chr to print)
LD (HL),A ;And copy it to the print position
INC HL ;Go to the next pointer
INC HL
JP PRINTLOOP
You can also do a bit of tidying e.g. replace jp with jr to save a byte. Similarly, replacing 'cp $00' with 'or a' saves a byte.

HTH

Alan
Spinnetti
Posts: 255
Joined: Sat Sep 12, 2020 11:29 pm

Re: Today's beginner question - Arrays in Assembly

Post by Spinnetti »

Thanks Alan. That second one was a typo. Corrected (reposted corrected code ) and working as intended Lol. was a lot of teeth gnashing for simple mistakes. Thanks for the other tips too. The JP instead of JR was for execution speed at expense of space, and thanks for the reminder on OR A :)
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+,Printer
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
User avatar
mrtinb
Posts: 1916
Joined: Fri Nov 06, 2015 5:44 pm
Location: Denmark
Contact:

Re: Today's beginner question - Arrays in Assembly (SOLVED)

Post by mrtinb »

You might find it useful to use HOT-Z/HI-Z for debugging your programs. It works best with 32k+ RAM and M1NOT modification. You can just step through the code line by line. It's the best debugger I've found for ZX81.

HOT-Z Thread
viewtopic.php?f=11&t=1381

New HOT-Z Thread with 16k version as well
viewtopic.php?f=11&t=3465
Martin
https://zx.rtin.be
ZX81, Lambda 8300, Commodore 64, Mac G4 Cube
Spinnetti
Posts: 255
Joined: Sat Sep 12, 2020 11:29 pm

Re: Today's beginner question - Arrays in Assembly (SOLVED)

Post by Spinnetti »

Thanks Martin. Debugging has been a bit blind - was just taking values and dropping them on the screen to see what was going on. I guess I should join the modern world :). I think my macro logic is generally ok, but making lots of dumb little mistakes that are hard to track down.
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+,Printer
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Today's beginner question - Arrays in Assembly (SOLVED)

Post by XavSnap »

Hi,

The TASM code.
Just one change in the 'PRINTS' ROUTINE.

Code: Select all


.org 16514

INITSPRITES: ;Load up the empty slots in the table. on first run all empty, so don't need to check for filled slots
	LD HL,($400C) ;Set the first printable spot on the screen
	INC HL
	LD BC,SPRITETABLE
LOADLOOP:
	LD A,(BC) ;Load first byte of the array
	CP $FF ;End of table?
	JP Z,PRINTSPRITE ;Table filled, exit
	LD A,H ;Save the position pointer. BC pointing at empty entry in table and HL has the randomized starting address for the grunt
	LD (BC),A
	INC BC
	LD A,L
	LD (BC),A
	INC BC ;Skip to the next pointer
	JP LOADLOOP ;Repeat until end of table

PRINTSPRITE: ;Cycle through the sprite table and print ones that exist, ignoring dead ones (empty) and bailing out when reaching table end ($FF)
	LD BC,SPRITETABLE ;Pointer to the first sprite

PRINTLOOP:
	LD A,(bc) ;Make sure not the end of a table or empty
	CP $FF ;End of table
	RET Z ;Done printing for this cycle - return
	CP $00 ;sprite is "Dead"
	JP Z,PRINTS ;Valid pointer so print sprite
	LD (HL),$26
PRINTS: ; "Dead" jump.
	inc hl ;move to next spot on screen
	INC bc ;Go to the next pointer
	INC bc ;Nothing there, so move to the next pointer
	JP PRINTLOOP ;Skip to the next pointer

SPRITETABLE: 
.db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
.db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$FF,$FF
.end

Code: Select all

    10  REM [HEX:\
2A,0C,40,23,01,AC,40,0A,\
FE,FF,CA,98,40,7C,02,03,\
7D,02,03,C3,89,40,01,AC,\
40,0A,FE,FF,C8,FE,00,CA,\
A6,40,36,26,23,03,03,C3,\
9B,40,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,00,00,\
00,00,00,00,00,00,FF,FF ]

    20 RAND USR 16514
ARRAY1.P
(1015 Bytes) Downloaded 73 times
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
XavSnap
Posts: 1941
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: Today's beginner question - Arrays in Assembly (SOLVED)

Post by XavSnap »

Simple array copy:

Code: Select all

.org 16514

Start:
	LD DE,($400C) ;Set the first printable spot on the screen
	INC DE ; Target
	LD HL,ARRAYTABLE ; From data array.

LOOP:
	LD A,(HL)
	CP $FF
	RET Z
	LD BC,$1 ; Just one char
	LDIR ; Copy HL to DE Step=1 (HL++;DE++;BC--)
	JR LOOP

ARRAYTABLE: 
.db $2D,$2A,$31,$31,$34,$00,$3C,$34,$37,$31,$29,$FF
.end

Code: Select all

    10  REM [HEX:\
ED,5B,0C,40,13,21,95,40,\
7E,FE,FF,C8,01,01,00,ED,\
B0,18,F5,2D,2A,31,31,34,\
00,3C,34,37,31,29,FF ]

    20 RAND USR 16514
ARRAY2.P
(966 Bytes) Downloaded 70 times
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
Spinnetti
Posts: 255
Joined: Sat Sep 12, 2020 11:29 pm

Re: Today's beginner question - Arrays in Assembly (SOLVED)

Post by Spinnetti »

Thanks for the additional feedback. I'll work on it some more this weekend!
Zeddy: ZX80, ZX81/ZXpand, TS1000/ZXpand, TS1500/Zxpand+,Printer
Speccy: 48k, +, +2, +3, TS2068, "Bare Metal" Pi, Next KS2, IF1/Microdrives/Vdrive/Light Gun/VGA-Joy
QL: Minerva/QL-VGA/Custom PSU
C5: 24v, LiFE battery, Disc brakes
Post Reply