Z80 Home

By James Moxham

 ZINT Z80 INTERPRETER

                     Copyright 1996 James Moxham


Chapter 1                LD group of instructions
Chapter 2                Exchange group
Chapter 3                The stack, POP and PUSH
Chapter 4                Arithmetic
Chapter 5                Jumps calls and returns
Chapter 6                And or and xor
Chapter 7                Bit set reset and test
Chapter 8                Rotate and shift
Chapter 9                General arithmetic and control
Chapter 10               Block transfer and search
Chapter 11               Input and Output instructions
Chapter 12               Additional useful commands
Chapter 13               Number bases
Chapter 14               The flags

Appendix 1               Binary, hex ascii decimal TC conversion

*****************************************************************

                  CHAPTER 1 The LD instruction


     The LD instruction is perhaps the most useful instruction in
the Z80.  It is used to transfer data between registers,  and  to
and  from  memory.  The most simple type of LD instruction is  to
fill a register with a number: (Use F1)

LD A,6

     This loads the A register with the number 6, so the register
display now appears as

A  CZPSNH  BC   DE   HL   IX   IY  A' CZPSNH' BC'  DE'  HL'  SP
06 000000 0000 0000 0000 0000 0000 00 000000 0000 0000 0000 0000

     You can transfer this data to other registers

LD H,A

A  CZPSNH  BC   DE   HL   IX   IY  A' CZPSNH' BC'  DE'  HL'  SP
06 000000 0000 0000 0600 0000 0000 00 000000 0000 0000 0000 0000

copies  the data in the A register to the H register.  In fact  H
could  have been any one of A B C D E H or L.  The data in the  A
register remaines unchanged.

                 Transferring data: Number bases

     As  most  programmers  of BASIC will  know  numbers  can  be
represented in several forms,  binary octal and hexadecimal being
common bases. The registers in the above example display the data
in hexadecimal form. Thus

LD A,255 gives

A  CZPSNH  BC   DE   HL   IX   IY  A' CZPSNH' BC'  DE'  HL'  SP
FF 000000 0000 0000 0000 0000 0000 00 000000 0000 0000 0000 0000

     FF  is  255 in hexadecimal.  The equivalent statement  using
hexadecimal directly is

LD A,0FFH

     The H at the end signifies that the number is a hex  number.
The  0  at the front is necessary because the first digit in  any
number should always be between 0 and 9.

     You can also use binary

LD A,11111111B

     where  the  B at the end specifies the number  is  a  binary
number.

     The  reason  for  using  all three  bases  is  because  some
instructions  are much easier to understand in one base.

Two other bases are supported,  two's complement and direct ascii
characters  and  are discussed in detail in ch16.  The  following
instructions all do the same thing.

LD B,073H
LD B,01110011B
LD B,65
LD B,"s"

                      Double register LD's

     As you notice from the register display some registers  have
been  grouped  together.  For example the H and L  registers  are
displayed  together as HL.  You can treat these pairs as if  they
were one

LD HL,1000 returns

A  CZPSNH  BC   DE   HL   IX   IY  A' CZPSNH' BC'  DE'  HL'  SP
00 000000 0000 0000 03E8 0000 0000 00 000000 0000 0000 0000 0000

We  can alse transfer this data from one double register pair  to
another

LD BC,HL gives

A  CZPSNH  BC   DE   HL   IX   IY  A' CZPSNH' BC'  DE'  HL'  SP
00 000000 03E8 0000 03E8 0000 0000 00 000000 0000 0000 0000 0000

     The double registers can be any one of BC DE HL IX IY or SP.

                  Transfers to and from memory

     The   above  instructions  all  transfer  data  within   the
microprocessor.  The  following sequence transfers a data to  the
memory (Use F1 or write as a program and then type F5)

LD A,255:LD HL,1000:LD,(HL) A

     Here  we  are using more than one statement in a  line.  The
first  two statements load the registers as we have seen  before.
The  last  statement  loads memory location  1000  with  255.  To
retrieve this data we can use

LD D,(HL)

which transfers 255 from the memory to register D.

To see what is in the memory at any one time use the view
memory command.

In  general  the  memory  location is usually  given  by  the  HL
register  pair.  The BC and DE regsters can be used as the memory
location  but  the  data  can only be  transferred  to  and  from
register A eg LD (BC),A is allowed but LD B,(DE) is not.

A  second  way to transfer data to and from memory is  to  use  a
number instead of a register pair. Thus

LD E,(38C1H) transfers to register E the data in memory 1000
LD (HL),34 transfers to the location stored in HL the no 34

A third way is to use the IX and IY registers. Say for example we
want  to store the word HELLO in memory.  First we would look  up
the  ASCII values for the letters,  which are 72 69 76 76 79.  We
will  store the word starting at memory location  500.  First  we
load IX or IY with 500

LD IX,500

Next the data is transferred

LD (IX),72:LD (IX+1),69:LD (IX+2),76:LD (IX+3),76:LD (IX+4),79

Use the view memory command to see where this data is.

The  final way LD can be used is to transfer double registers  to
and from memory. For example

LD (500),BC

transfers  the contents of C to the memory location 500  and  the
contents of B to location 501.  We can load this data from memory
to register pair DE with a

LD DE,(500)

The  register can be BC DE HL IX  IY  or  SP.

What  follows now is a list of all the possible  LD  instructions
sorted alphabetically.  The letter n is used to indicate a number
between 0 and 255 (0 and 0FFH).  nn represents a number between 0
and  65535 (0 and 0FFFFH).  d is any number between -127 and +127
(the +d can be -d; LD (IX-23) A

LD (BC),A       LD B,(HL)       LD H,(IX+d)
LD (DE),A       LD B,(IX+d)     LD H,(IY+d)
LD (HL),A       LD B,(IY+d)     LD H,A
LD (HL),B       LD B,A          LD H,B
LD (HL),C       LD B,B          LD H,C
LD (HL),D       LD B,C          LD H,D
LD (HL),E       LD B,D          LD H,E
LD (HL),H       LD B,E          LD H,H
LD (HL),L       LD B,H          LD H,L
LD (HL),n       LD B,L          LD H,n
LD (IX+d),A     LD B,n          LD HL,(nn)
LD (IX+d),B     LD BC,(nn)      LD HL,nn
LD (IX+d),C     LD BC,nn        LD I,A
LD (IX+d),D     LD C,(HL)       LD IX,(nn)
LD (IX+d),E     LD C,(IX+d)     LD IX,nn
LD (IX+d),H     LD C,(IY+d)     LD IY,(nn)
LD (IX+d),L     LD C,A          LD IY,nn
LD (IX+d),n     LD C,B          LD L,(HL)
LD (IY+d),A     LD C,C          LD L,(IX+d)
LD (IY+d),B     LD C,D          LD L,(IY+d)
LD (IY+d),C     LD C,E          LD L,A
LD (IY+d),D     LD C,H          LD L,B
LD (IY+d),E     LD C,L          LD L,C
LD (IY+d),H     LD C,n          LD L,D
LD (IY+d),L     LD D,(HL)       LD L,E
LD (IY+d),n     LD D,(IX+d)     LD L,H
LD (nn),A       LD D,(IY+d)     LD L,L
LD (nn),BC      LD D,A          LD L,n
LD (nn),DE      LD D,B          LD R,A
LD (nn),HL      LD D,C          LD SP,(nn)
LD (nn),IX      LD D,D          LD SP,HL
LD (nn),IY      LD D,E          LD SP,IX
LD (nn),SP      LD D,H          LD SP,IY
LD A,(BC)       LD D,L          LD SP,nn
LD A,(DE)       LD D,n
LD A,(HL)       LD DE,(nn)
LD A,(IX+d)     LD DE,nn
LD A,(IY+d)     LD E,(HL)
LD A,(nn)       LD E,(IX+d)
LD A,A          LD E,(IY+d)
LD A,B          LD E,A
LD A,C          LD E,B
LD A,D          LD E,C
LD A,E          LD E,D
LD A,H          LD E,E
LD A,L          LD E,H
LD A,n          LD E,L
LD A,R          LD E,n
LD A,I          LD H,(HL)

****************************************************************

                  CHAPTER 2 The EX instructions

     In  addition  to the registers we have mentioned so far  the
Z80 has several additional registers. The most important of these
are  the so called prime registers,  which are designated A'  BC'
DE' and HL'.  You cannot access these registers directly, but you
can  swap them with the ordinary registers.  If you type  in  the
following code

LD BC,1234H:LD DE,5678H:LD HL,9ABCH  the registers will appear

A  CZPSNH  BC   DE   HL   IX   IY  A' CZPSNH' BC'  DE'  HL'  SP
00 000000 1234 5678 9ABC 0000 0000 00 000000 0000 0000 0000 0000

Now type in

EXX       which swaps BC DE and HL with the prime registers

A  CZPSNH  BC   DE   HL   IX   IY  A' CZPSNH' BC'  DE'  HL'  SP
00 000000 0000 0000 0000 0000 0000 00 000000 1234 5678 9ABC 0000

You can now work on the normal registers, eg

LD BC,1111H

When you want to swap the registers back again use EXX

The  EXX  statement is very useful for storing variables you  are
working on without having to save them in memory.  The equivalent
store  to  memory  for  these three registers  would  take  3  LD
statements.

Other EX commands

Several other commands exist that swap registers.

EX AF,AF'

swaps  the A register and the flags with the corresponding  prime
registers. It is commonly used with EXX.

EX DE,HL

swaps the DE register and the HL register.

EX (SP),HL
EX (SP),IX
EX (SP),IY

all  swap the memory contents pointed to by the SP register  with
the  corresponding register.  The equivalent code for EX
(SP),HL could be

LD HL,1234H:LD BC,5678H:LD (1000H),BC:LD SP,1000H  then
LD BC,(1000H):LD (1000H),HL:LD HL,BC

Thus in the case of EX (SP),HL the L register is swapped with the
data  at the memory location pointed to by the SP  register,  and
the H register is swapped with the memory location + 1 pointed to
by the SP register. Type MEMORY to check this.

Exchange Commands

EXX                           EX (SP),HL
EX AF,AF'                     EX (SP),IX
EX DE,HL                      EX (SP),IY

****************************************************************

                      Chapter 3 The Stack

     The  memory  of a computer can be thought of as  a  library,
with  each  book representing a memory location.  LD BC,(350)  is
like finding the 350th book.  The stack on the other hand is like
a  pile of books.  Instead of storing the BC register  in  memory
location 350 and then retrieving it later we can put it on top of
a pile of books, and take it off later.

     The following code shows how the stack works

LD BC,1234:LD SP,504H initialises the registers

PUSH BC     takes the BC register and puts it on top of the  pile
of books