0
ThinkPositive equ 1
PrintChar equ &BB5A

org &8100
    ld hl,Introduction
    call PrintString
    call NewLine
    ld hl,Message
    call PrintString
ret

PrintString:
    ld a,(hl)
    cp 255
    ret z
    inc hl
    call PrintChar
jr PrintString

Introduction:
    db 'Thought of the day...',255

ifdef ThinkPositive
    Message:    db 'Z80 is Awesome!',255
else
    Message:    db '6510 sucks!',255
endif

Newline:
    ld a,13
    call PrintChar
    ld a,10
    call PrintChar
ret

For your information, this piece of z80 asm prints 2 strings.

2
  • The code is not very efficient. As already pointed out using 00 as string terminator would make more sense. Using or a to test for 00 is only a 1 byte instruction (note that ld a,(hl) when (hl) is 00 would not set the Z-flag). Furthermore it would make sense to add the 0dh and 0ah at the end of the string when a CR/LF needs to be printed instead of adding a subroutine call for it. Nov 4, 2019 at 21:16
  • 1
    &BB5A - Amstrad CPC? Nov 5, 2019 at 3:56

1 Answer 1

9

ld a, (hl) copies the byte in the address that hl points to into register a. I assume the printchar routine expects a character byte in this register, probably in ASCII encoding.

Your code loads hl with the start address of the string and then iterates through it, printing each character until it reaches 0xFF, when it stops. The increment moves hl to 'point' to the next byte (character).

It's slightly unusual to use 0xFF (255 in decimal) as the end of string marker, but in assembler you can make your own rules!

4
  • 4
    "It's slightly unusual to use 0xFF (255 in decimal) as the end of string marker, but in assembler you can make your own rules!" For the Z80 there's no difference, as a test is needed anyway. Then again, considering the shown love got 6500 CPUs, where using Zero would save an instruction, it makes sense to use an encoding where the 6500 can't gain an advantage :)) SCNR
    – Raffzahn
    Mar 27, 2019 at 10:46
  • I was thinking about 'C' style zero-terminated strings. It didn't occur to me that CP 0 might be faster on other processors. Mar 27, 2019 at 21:40
  • 4
    Here, using zero as the end of string marker would let us use or a instead of the cp 0 that would replace cp 255, which saves one byte. or a computes the bitwise OR of a with itself, which does nothing, but afterwards, the z flag is set if a is zero. Mar 28, 2019 at 2:01
  • 4
    @MarkWilliams It's faster, because it isn't needed. When loading a value, Motorola style CPUs set the flags accordingly, so loading Zero will set the Zero flag without any test needed. Same for DEC - you see the picture?
    – Raffzahn
    Mar 28, 2019 at 7:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .