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:
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.
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.
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!