-4
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.

  • 5
    Please post code, not pictures of code. – Wilson Mar 27 at 8:25
4

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!

  • 2
    "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 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. – Mark Williams Mar 27 at 21:40
  • 1
    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. – Misha Lavrov Mar 28 at 2:01
  • 1
    @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 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.