Page 1 of 1

Looping strings

Posted: Fri Jan 15, 2021 9:59 am
by Nephesh
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.

Re: Looping strings

Posted: Fri Jan 15, 2021 10:20 am
by akuyou
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

Re: Looping strings

Posted: Fri Jan 15, 2021 10:24 am
by Nephesh
Thank you so much.