Looping strings

Posts about Z80 programming that do not relate to a particular computer
Post Reply
Nephesh
Posts: 8
Joined: Wed Dec 02, 2020 2:16 pm

Looping strings

Post by Nephesh » Fri Jan 15, 2021 9:59 am

Hi there,
I've wanted to know how to loop strings. I've tried using the register B as what "carries" the loop. I wrote my PrintString correctly, but I have no idea how to make the word "Test" constantly repeating.

Code: Select all

org &8100
PrintChar equ &BB5A
ld hl,Message 
ld b, 10
call Loop 
Message: db 'Test',0
PrintString:
    ld a,(hl)
    cp 0
    ret z
    inc hl
    call PrintChar
    jr PrintString
Loop:
    dec b
    call PrintString
    cp 255
    djnz Loop
Thank you.

User avatar
akuyou
Posts: 563
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Looping strings

Post by akuyou » Fri Jan 15, 2021 10:20 am

I think this does what you're after.

A few changes!

1. You need a return at the end of your printstring function.
2. you don't need "dec b" or "cp 255"... djnz does this for you.
3. Printstring changes HL, so the next run of it won't show 'Test'... I put the call in the middle of a "Push Pop" combo, this backs up and restores the value of HL, so it's the same each time PrintString runs

Code: Select all

	org &8100
	PrintChar equ &BB5A
	ld hl,Message 
	ld b, 10
	call Loop 
	ret

Message: db 'Test',0

PrintString:
	ld a,(hl)
	cp 0
	ret z
	inc hl
	call PrintChar
	jr PrintString
Loop:
	push hl
		call PrintString
	pop hl
	djnz Loop
    ret
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

Nephesh
Posts: 8
Joined: Wed Dec 02, 2020 2:16 pm

Re: Looping strings

Post by Nephesh » Fri Jan 15, 2021 10:24 am

Thank you so much.

Post Reply

Return to “General Z80 Programming”