Pointers and the LD instruction

"You've missed the point completely, Julia: There were no tigers." - T.S. Eliot, 'The Cocktail Party'

by Ciaran McCreesh
Created: 4th October 1999
Last Modified: 4th December 1999

This page will explain what the LD instruction does and what it is used for. It will also discuss pointers.

The LD Instruction

LD is short for load, and that is what the LD instruction does - it loads values from registers into memory, from memory into registers and from registers to other registers. It has the general form ld [from],[to]. For example:

Sample_LD_Commands:
  ld a,12              ; load a register with value 10
  ld b,a               ; load b register with value of a register
  ld c,$2c             ; load c register with value $2c
  ld de,$fc00          ; load de with $fc00

  ld hl,($fc00)        ; load hl with value at memory location $Fc00
  ld ($fc01),hl        ; load memory location $fc01 with value of hl

  ld hl,$1234          ; load hl register with value $1234
  ld a,0               ; load a register with value 0
  ld (hl),a            ; load memory location pointed to by hl with value of a

Pointers

You may have noticed that ld a,$12 and ld a,($12) appear to do different things. They do - the first loads the a register with the value $12 and the second loads the a register with the value at memory location $12.

Similarly, you can do ld a,(hl) (this only works for some registers). This loads the a register with the value stored in the memory location pointed to by hl. So, if hl was $d748 then the a register would get the value stored in memory location $d748, but if hl was $fc00 then the a register would get the value stored in memory location $fc00.

Note that the value of the source for the LD instruction remains unchanged, for example:

Start_Of_Code_Sample:
  ld a,5               ; a = 5
  ld b,a               ; b = a, so b = 5. Register a remains unchanged (= 5).

Also, in ld (hl),a and similar the value of hl remains unchanged, but the value at memory location hl does change.