Explanation – Let there be colour!

So, if you’ve got that first program working, you’re probably now wondering just how it does what it does?

Here’s the program again:

org 32768
ld a, 32
ld (22528), a
ret

END 32768

Lets take this line by line:

org 32768

This line tells the compiler where in memory to store your program, and is short for ‘Origin’.  The ZX Spectrum is an 8-bit machine, which means it can readily use 64K of data – that’s 65536 separate pieces of information – each of which is a BYTE – an 8 bit piece of data which can hold any number from 0 to 255.  The first 16K (or 16384 bytes) is usually used by the ROM – and we can’t touch that.  On a 16K Spectrum only the next 16K can be used, but on a 48K machine the next 48K can be used, hence their names.  Some of that is used by the screen, the BASIC program, and various other things, so to avoid all that by a healthy margin we’ll start our program at address 32768 – which gives us 32K to hold our game code.

ld a, 32

As mentioned, every piece of information on an 8-bit machine is stored in 8 bits, which can hold a number from 0 to 255.  Just about everything you will code will be working with numbers – setting them to values, adding to them, subtracting from them, putting them in memory, getting them back from memory, and so on.  To help us with that task, Z80 gives us 7 boxes, called ‘registers’, to store them in – named A, B, C, D, E, H, and L.  It also gives us another two, IX & IY, which are special and I’ll come to later, and also R & F, which also have special uses.  If you think back to your high-school Algebra – X = 10, Y = 15 – and all that stuff, this is the same.

So “ld a, 32‘ tells the machine to put, or ‘Load’ – hence ‘ld‘ – the number 32 into the register A.

ld (22528), a

It’s that ‘ld’ again – so we’re loading, or putting something somewhere.  In this case it’s storing whatever is in register A at the address in memory 22528 – having the 22528 enclosed in braces means that it’s an address in that 64K of memory. “ld 22528, a” wouldn’t make any sense, you just can’t put one number into another.

ret

This is short for ‘Return’, and simply tells the program to stop, and return to whatever called it.  In our case, that was the Spectrum itself, so it just comes back to BASIC.

END 32768

This isn’t Z80 – but a special command to the compiler to tell it stop compiling, and that the start of our program is at address 32768. You might have thought you did that with the ‘org’ instruction, but that just tells the compiler where to put the code, not where to start running it from.

So, putting that all together, it puts the number 32 at the address 22528 in the Spectrum’s 64K of memory.

Now, why should that draw a green square in the top-left corner?

I mentioned earlier that the screen is stored in that 64K of memory.  It starts at address 16384, and continues on for 6912 bytes.  The last 768 of those, from 22528 onwards, store the colours for the screen – 24 rows of 32 columns – starting from the top-left.  So when we store a number at 22528 the Spectrum knows that it’s a colour at the top-left of the screen.  If a different number is used, it will store A in a different place in memory.  If it’s in that 768 of colour data, it will be an on-screen colour, if it’s in the first 6144 bytes from 16384, it’ll be an on-screen graphic, if it’s a number less than 16384 then it will be in the ROM and so ignored. If it was at 32768 it would change the first byte of our program.

The number we put there is 32 – the value we stored in A – and the Spectrum knows this to be green.  More exactly, it’s a green background, or PAPER if you’ve used BASIC.  If a different number is used, a different colour will be shown.  Try it!

Advertisements

8 thoughts on “Explanation – Let there be colour!

  1. So far, so interesting.

    Right, so I now know that 32 is green, and not ASCII for the space character. What I’m finding is that 33 won’t give me another colour. So my guess is that there will be a range of bits that set PAPER for that byte, and another set of bits that set INK. Somewhere in the 256 values for a byte INK, PAPer and BRIGHTNESS will be represented. Is this correct? Can you explain a bit more about why 32 is green?

    Like

    1. That’s correct. I’ll cover this in more detail in the next post tomorrow, but as you appear to know your way around the makeup of a byte… the first 3 bits are foreground (INK), the next three background (PAPER), then a bit for brightness, and the final one for flashing between foreground and background.

      Liked by 1 person

    2. I think it’s the paper colour x 8 plus the ink colour (plus 64 and 128 for bright and flash …although not sure which way round). Green is colour 4 so 4×8=32. I’m thinking a cyan square would be 40 (5×8). Not played with this for a while so might be wrong.

      Like

      1. Cool, I do know assembly (have been using it for 6 years in various processors in the past z80, 6502, arm and x86, yet not recently) but speccy was always intriguing me so I wanted to skip a bit on that part. Awaiting your next blog post soon 🙂

        Like

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