Let there be color – explanation

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

In our previous post, we managed to display a black square at the top of the screen with the following assembly lines:

ld HL, (D_FILE)
ld a, 0x80
inc HL
ld (HL), a
RET

Let’s look at it line by line.

ld HL, (D_FILE)

D_FILE contains the address of what, in ZX81 language, is called the display file. It’s an area in the memory of the ZX81, that contains all the values of the characters that are displayed on the screen.

The structure of that area depends on the amount of RAM of your ZX81. To be precise, it depends on the value of RAMTOP – more on this later. For the time being, let’s consider two cases:

  • If your ZX81 has only 1K of RAM, the size of the display file is variable. At init, it contains 25 bytes filed with the value 0x76. If you write something in it, it is your responsibilty to make sure that it always starts with 0x76, with 24 lines containing 32 bytes maximum followed by 0x76. For instance, if you want to write HELLO at the top left of the screen, the display file should look like this:
  • If your ZX81 has 16K of RAM, the size of the display file is fixed. At init, it contains 0x76 in the first byte, followed by 24 lines of 32 bytes filled with 0x00 and one byte filled with 0x76
ld a, 0x80

0x80 is the hex value of the character representing a black square in the ZX81 character set. You can find all the value of all the character set of the ZX81 in Appendix A of your user manual. ld a, 0x80 loads the value 0x80 in register A.

 inc HL

This one is pretty straightforward, it increases the value of the 16-bit register HL by one.

ld (HL), a

Remember, HL contains the address of one location in the display file. This instruction is telling the computer to load the value of register a at the address contained in HL. It happens to be the first top left character of the screen.

RET

This instruction will end execution of our assembly language sequence, and return to the BASIC interpreter. On a ZX81, you boot up to the BASIC interpreter. You can execute some assembly code using the BASIC function USR. USR takes one parameter in entry, which is the address of the assembly code to be executed. For instance:

10 PRINT USR 16514

will execute the assembly code located at memory address 16514. Also note that when you start executing the assembly code, then register BC is loaded with the parameter value. Upon return, the USR function contains the value in the BC register. Let’s assume that our code contains the following lines:

ld b,0
ld c,0
RET

and we call it with

10 PRINT USR 16514

The PRINT line will call the assembly routine at location 16514. It will start by pushing the value 16514 in BC. b is set to 0, c is set to 0 and we return to BASIC. The return value of USR 16514 being the content of BC, the output of this program will be 0.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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