What is a register?
The CPU has to work with some memory, otherwise not much can be achieved. However, external memory is rather slow to access (as we'll see later), thus, the CPU has a tiny amount of memory embedded within it. Tiny as in, a handful of bytes. This memory is split in units called registers.
The GBz80 has eight registers:
l (plus some others we'll discuss later). The odd ordering I might have given isn't random, it'll be explained below.
All of these registers are 1 byte, which, if you remember, means they can hold each an integer between either 0 and 255, or -128 and 127. Also, all of them besides
f are "general-purpose", meaning they can roughly be freely swapped (we'll see that
a is a bit special).
f, on the other hand, cannot be used at all directly (except in one case explained much later).
f will be discussed more in the Flags lesson.
Our first instruction:
Here comes our first ASM instruction! Get hyped for
ld! Like all ASM instructions,
ld stands for something else: in this case, it's LoaD.
ld loads a value into a register. Here's the syntax:
ld dest, source.
As described above,
f cannot be used as the source or destination. Other than that,
dest can be any register, and
source can be any register or an immediate value. An immediate value is simply a number that is provided directly.
ld l, a ; Copy the value in A into L. A's value is preserved. ld h, 0 ; Put 0 into H ("0" is an immediate value, here) ld c, $2A ; Put 42 into C ("$2A" is also an immediate)
While 7 registers is a nice amount, only being able to hold 8-bit numbers is kinda limiting. However, the GBz80 has a trick up its sleeves: these registers can be paired together to form 16-bit registers! Values are thus in range 0 - 65,535, or -32,768 - 32,767. Registers
c form register pair
a is paired with
f in exactly one case, which is specific enough that we will consider
a a standalone register.
When paired up, the individual registers can still be accessed. What's interesting is, the value held by the pair is simply a combination of the values held by each register.
ld hl, $1245 ld l, $34 ; HL = $1234 now ld c, $FE ld b, $CA ; BC = $CAFE now