Filling the screen with colors

[Inspired by this article on Bob’s 8-bit blog]

At this stage, you understood that the ZX81 does not have colors. Only a 21 lines by 32 characters screen, each character filled with a  value representing either graphics or letters, digits, or symbols.

So, what if we wanted to fill the screen with, say, black characters? We could try expand our original program like this:

ld HL, (D_FILE)
ld a, 0x80       ; 0x80 is the value for the black square character
INC HL           ; skip the first 0x76
ld (HL), a
LD (HL), a
LD (HL), a       ; the ld inc sequence is repeated
INC HL           ; 32 times for line one
INC HL           ; skip 0x76 at the end of the line
LD (HL),a

Pretty fastidious right… We know we have 32 characters to fill per line, skip the 0x76 end of line, and repeat for 24 lines. Let’s use loops!

start: ld hl, (D_FILE)
       inc hl 
       ld bc, 792
l1:    ld a, (hl)
       cp 0x76
       jr z,l2
       ld (hl), 0x80
l2:    inc hl
       dec bc
       ld a, b
       or c
       jr nz,l1

We start by loading the address of the display file in hl, and skip the first character, which, as you remember is 0x76:

start: ld hl, (D_FILE)
       inc hl

32 characters plus one 0x76 at the end of the line on 24 lines is 33×24=792. So, we are going to loop over the 792 remaining locations of the display file and , if not an end of line value (0x76), we are going to fill the position with a black square (0x80).

ld a,(hl)

reads the value at the current position.

cp 0x76

compares the value in the accumulator with the value 0x76. If they are identical, the Z flag is set to 1.

jr z, l2

tells the computer to jump to l2 if Z is 1 (in our case, it means that we found 0x76 at the examined location).

ld (hl), 0x80

This instruction is filling the current position with a black square. Note that it can only be executed if we did not find a 0x76 on the previous line.

l2:     inc hl
         dec bc

We are now increasing hl o move to the next location, and decrement our counter.

ld a, b
or c

These two lines are testing if the content of bc is 0!!! This can only be done using a, so, we first load the first part of our counter into a, and we ‘or’ it with the second part of our counter. Only if all the bits in a and c are at 0 will the result of ‘a or c’ will be 0. Pretty smart way of testing if a 16 bits register has reach the value zero.

jr nz, l1

Finally, we test if the result of the previous operation set z to 0. If not, this means that our counter is not at zero yet, we jump to l1 to continue with the next location.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s