Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

EE323: Microprocessor systems design

3. Z80 instruction set and programming techniques

The Z80 microprocessor has 158 instruction types, each instruction has
two parts: one is the task to be performed (such as load, add, Jump), called the
operation code (opcode) and the second is the data to be operated on, called the
operand.

3.1 instruction format

Each instruction in the program is a command in binary, the Z80


instruction ranges from one to four bytes.

- One byte instructions

Example  LD A, B  (0111000)2  (80)16

- Two bytes instructions

Example  LD B, 2AH  (00000110 00101010)2  (3E 2A)16

- Three bytes instructions

Example  LD BC, 2100H  (00000001 00000000 00100001)2  (01 00


21)16

- Four bytes instructions

Example  LD IX, 2200H  (11011101 00100001 00000000 00100022)2 


(DD 21 00 20)16

Remark: a 16 bit address or data is stored low order byte first following by the
high order byte.

3.2 Z80 instruction set

Z80 instruction set can be classed into six categories as follows:

- Data copy or load operation


- Arithmetic operations
- Logic operation
- Bit manipulation
- Branch operation
- Machine control operation
3.2.1. Data copy or load operation

The Z80 has numerous instructions that copy data from one location, called
source to another location, called destination (LD Destination, Source).

- Loading registers and data copy Among registers

LD rdistination, rSource example LD A, B


LD r, 8bit example LD C, 2AH
LD rp, 16 bit example LD BC, 2100H
LD rx, 16 bit example LD IX, 2200H
Where r can be any one of registers A, B, C, D, E, H , L, I, R and rp is register
pair and rx is the index register

- Data copy between Z80 registers and memory


LD r,(HL) example LD B,(HL)
LD (HL), r example LD (HL), C
LD (HL), 8bit example LD (HL), 33H
LD A, (rp) example LD A, (BC)
LD (rp), A example LD (DE), A
LD A, (16bit) example LD A, (2050H)
LD (16bit), A example LD (2100H), A
LD (IX+d), 8bit example LD(IX+3), 55H
LD r, (IX+d) example LD C, (IX+d)
LD (IX+d), r example LD (IX+d), B
The 16bits operand or register pair with parentheses is used as memory pointer.
When the memory address is specified by the content of register HL, The
register r can be any one of the general-purpose registers and d is the
displacement.
When the memory address is specified by 16 bits operand or by register pair BC
or DE, the instruction can copy data from accumulator only.
- Data copy between accumulator and I/O devices
IN A, (8bit) example IN A, (01H)
OUT (bit), A example OUT (03H), A
The 8bit I/O addresses are enclosed in parentheses similar to those of memory
addresses.
Remark: Copy instructions do not affect flag
3.2.2. Arithmetic operation
In 8 bits arithmetic operation, the accumulator is generally assumed to be one of
the operand except the increment and decrement operation.
- Addition and subtraction
ADD A, r example ADD A, C
ADD A, 8bit example ADD A, 23H
ADD A, (HL) add memory content to the accumulator
ADD A, (rx+d) add content of memory IX+d to the accumulator
SUB (rx+d) Subtract content of memory IX+d from accumulator
SUB r example SUB C
SUB 8bit example SUB 55H
SUB (HL) subtract memory content from the accumulator
Remark: The result is stored in the accumulator and the flag is modified
according to the result of the operation.
- Increment/ Decrement instructions
INC r example INC B
INC (HL) increment the content of memory
INC rp example INC BC
INC rx example INC IX
INC (rx+d) example INC (IY+3)
DEC r example DEC C
DEC (HL) decrement the content of memory
DEC rp example DEC DE
DEC rx example DEC IX
DEC (rx+d) example DEC (IY+2)
Remarks: In increment and decrement operation the result is stored into the
same operand. The instructions dealing with 8bit registers affect all the flags
except the carry flag, and those dealing with rp do not affect any flags.
- 1’s and 2’s Complement instructions
CPL inverse each bit of the accumulator (1’s complement)
NEG subtract the content of the accumulator from 00 (2’s complement)
Remark: 1’s complement does not affect flag except H and N and 2’s
complement affect all the flag.
3.2.3. Logical operation
For the logical operation the implied operand is the 8 bit word of the
accumulator. After the operation the result will be stored in the accumulator.
- Logic AND, OR, XOR operation
AND r example AND B
AND 8bit example AND 55H
AND (HL) and the content of memory pointed by HL with accumulator
AND (IX+d) example AND (IX+3)
OR r example OR C
OR 8bit example OR77H
OR (HL) and the content of memory pointed by HL with accumulator
XOR r example XOR B
XOR 8bit example XOR55H
XOR (HL) exclusive OR content of memory pointed by HL with accumulator
Remark: Reset carry flag and modify S, Z and PV flags according to the data
condition of the result.
- Compare instruction
The compare instructions test a byte for less than, equal to, or greater than the
content of accumulator. No content are modified, the comparison is indicated by
setting flags
CP r example CP B
CP 8 bit example CP 55H
CP (HL) compares the content of memory pointed by HL with accumulator
CP (IX+d) example CP (IX+4)
If (A) < operand, the Carry flag=1 and Z flag =0
if (A) = operand, the Carry flag =0 and Z flag =1
if (A)> operand, the Carry flag =0 and Z flag =0
Remark: Other flags are affected according to the result of the subtraction
- Shift and Rotate
Each bit in the accumulator, in the register, or in memory can be shifted either
left or right by one position.
RLCA rotate accumulator left (8bit rotation), Carry is affected by rotation
but it’s not part of rotation
RLA rotate accumulator left (9bit rotation), carry is one of bit in the
rotation
RRCA Rotate accumulator right (8bit rotation), carry is affected by rotation
but it’s not part of rotation
RRA Rotate accumulator right (9bit rotate), carry is one of bit in the
rotation
In the following rotate r represent register and m is memory location.
RLC r or m rotate bit left example RLC B
RL r or m rotate bit left through carry example RL D
RRC r or m rotate bit right example RRC (HL)
RR r or m rotate bit right through carry example RR (IX+d)
SLA r or m example SLA (IY+d)
SRL r or m example SRL C
SLA and SRL is shift bits left and right through carry in register or memory and
insert 0 in bit position D0 for SLA and insert 0 in bit D7 for SRL.
3.2.4 Bit manipulation
The bit manipulation group can test, set and rest bit in register or memory
location.
BIT b, r or m test bit number b example BIT 5, (HL)
SET b, r or m set bit number b example SET 7, B
RES b, r or m reset bit number b example RES 0, (IY+5)
Remark: The Set/reset instructions do not affect flag.
3.2.5. Branching operation
This group of instructions alters the sequence of program execution either
conditionally or unconditionally.
- Jump instruction (absolute jump and relative jump)
The conditional jump instruction checks for the appropriate flag. If the condition
is true the program jump to the new address otherwise the execution continues
to the next instruction.
In absolute Jump, the operand specifies the 16 bit address to which the program
sequence should be transferred.
JP 16bit Jump unconditionally to 16 bit address example JP 2150H
JP C, 16bit Jump on carry to 16 bit address example JP C, 2150H
JP NC,16bit jump on no carry to 16 bit address example JP NC, 2150H
JP Z, 16 bit jump on zero to 16 bit address example JP Z, 2150H
JP NZ,16bit jump on no zero to 16 bit address example JP NZ, 2150H
JP M, 16bit jump on minus to 16 bit address example JP M, 2150H
JP P, 16bit jump on positive to 16 bit address example JP P, 2150H
JP PE, 16bit jump on parity even to 16 bit address example JP PE, 2150H
JP PO, 16bit jump on parity odd to 16 bit address example JP PO, 2150H
The relative jump instructions contain an operand that specifies 8bit
displacement, forward or backward. When D7 of displacement is 0 the
displacement is positive (forward jump), otherwise displacement negative
(backward jump) specified in 2’s compliment

JR d Jump relative unconditionally example JR 0FH


JR Z,d Jump relative if Z=1 example JR Z,F5H
JR NZ,d Jump relative if Z=0 example JR NZ,F5H
JR C,d Jump relative if C=1 example JR C,05H
JR NC,d Jump relative if C=0 example JR NC,80H
Remarks: There are no relative jump instructions based on sign and parity flag.
The jump instructions do not affect any flags
- Call and Return instructions
This instruction change the sequence of program by calling subroutine (chapter
4) or returning from subroutine.
CALL 16bit call subroutine located at 16 bit address.
CALL C, 16bit call subroutine located at 16 bits address if Carry=1
CALL NC, 16bit call subroutine located at 16 bits address if Carry=0
CALL Z, 16bit call subroutine located at 16 bits address if zero carry=1
CALL NZ, 16bit call subroutine located at 16 bits address if zero carry=0
CALL C, 16bit call subroutine located at 16 bits address if Carry=1
CALL NC, 16bit call subroutine located at 16 bits address if Carry=0
CALL PO, 16bit call subroutine located at 16 bits address if parity odd
CALL PE, 16bit call subroutine located at 16 bits address if parity even
CALL M, 16bit call subroutine located at 16 bits address on minus
CALL P, 16bit call subroutine located at 16 bits address on positive
RET return from subroutine
RET C, RET NC, RET Z, RET NZ, RET PO, RET PE, RET P, RET M
return from subroutine with condition.
- Restart instruction
These instructions are used to change the program sequence to one of eight
restart location on memory page 00. This instruction is generally used with
interrupt. RST 00 to 38H
RST 00, RST 08, RST 10H, RST 18H, RST 20H, RST 28H, RST
30H, RST 38H
Remark: no flag are affected
3.2.6 Machine control operation
These instructions control µp such as Halt and Interrupt.
HALT Suspender execution of instruction
DI Disable interrupt by resting enable flip flops
EI Enable interrupt
3.3 Programming techniques

A program is a sequence of instructions used to perform a specific task. To write


a program we need to divide a given problem into small steps and translate these
steps into the operations that the equipment can perform.
3.3.1. Flowchart
The steps listed in the problem analysis and the sequence can be represented in
block diagram, called flowchart. By definition the flowchart is a graphic of
logical and sequence of tasks to be performed. The six symbols used in
flowcharting are shown in the following figure.
Symbol Meaning
Oval: Indicates the beginning or end of program

Arrow: indicates the direction of the program execution

Rectangle: represents a process or an operation

Diamond: Represent a decision making block

Double-sided rectangle: represents a predefined


process such as subroutine.
Circle with an Arrow: Represents continuation (an
entry or exit) to a different page.

To write assembly language program, we need to translate the blocks shown in


the flowchart into Z80 operations and then into mnemonics. To convert the
mnemonics into hex code, we need to look up the code in the Z80 instruction
set.
3.2.1. Programming techniques: looping, counting, and indexing
Looping, counting and indexing are techniques used to perform a repetitive task.
- A loop is set up by using either a conditional jump or an unconditional
Jump. With looping we define the task to be repeated.
- The counter is set by loading a count (number of times the task is to be
repeated) into register or into register pair. The counting is done by
decrementing/ incrementing the count every time the loop is repeated.
- Indexing is specify the location of data , by loading the memory address
into register pair using a register pair as a memory pointer or index.
- The end of repetition is indicated by the state of the flag.

Donne by D.BELAIDI
February, 2021
Reference:
Book: Z80 Microprocessor , Architecture , interfacing , Programming, and design , third
edition, by Ramesh .S.Gaonkar. Pages [131-161], [183-251]

You might also like