beautypg.com

8 program control group – Zilog Z80380 User Manual

Page 46

background image

5-12

Z380

U

SER

'

S

M

ANUAL

DC-8297-03

Z

ILOG

5.5.8 Program Control Group

This group of instructions (Table 5-13) affect the Program
Counter (PC) and thereby control program flow. The CPU
registers and memory are not altered except for the Stack
Pointer and the Stack, which play a significant role in
procedures and interrupts. (An exception is Decrement
and Jump if Non-Zero [DJNZ], which uses a register as a
loop counter.) The flags are also preserved except for the
two instructions specifically designed to set and comple-
ment the Carry flag.

The Set/Reset Condition flag instructions can be used with
Conditional Jump, conditional Jump Relative, Conditional
Call, and Conditional Return instructions to control the
program flow.

The Jump and Jump Relative (JR) instructions provide a
conditional transfer of control to a new location if the
processor flags satisfy the condition specified in the in-
struction. Jump Relative, with an 8-bit offset (JR e), is a two
byte instruction that jumps any instructions within the
range –126 to +129 bytes from the location of this instruc-
tion. Most conditional jumps in programs are made to
locations only a few bytes away; the Jump Relative, with an
8-bit offset, exploits this fact to improve code compact-
ness and efficiency. Jump Relative, with a 16-bit offset (JR
[cc,]ee), is a four byte instruction that jumps any instruc-
tions within the range –32765 to +32770 bytes from the
location of this instruction, and Jump Relative, with a 24-bit
offset (JR [cc,] eee), is a five byte instruction that jumps any
instructions within the range –8388604 to +8388611 bytes
from the location of this instruction. By using these Jump
Relative instructions with 16-bit or 24-bit offsets allows to
write relocatable (or location independent) programs.

Call and Restart are used for calling subroutines; the
current contents of the PC are pushed onto the stack and
the effective address indicated by the instruction is loaded

Table 5-13. Program Control Group Instructions

Instruction Name

Format

nn

(PC+d)

(HL)

(IX)

(IY)

Call

CALL cc,dst

Complement Carry Flag

CCF

Call Relative

CALR cc,dst

Decrement and Jump if Non-zero

DJNZ dst

Jump

JP cc,dst

JP dst

Jump Relative

JR cc,dst

Return

RET cc

Restart

RST p

Set Carry Flag

SCF

into the PC. The use of a procedure address stack in this
manner allows straightforward implementation of nested
and recursive procedures. Call, Jump, and Jump Relative
can be unconditional or based on the setting of a CPU flag.

Call Relative (CALR) instructions work just like ordinary
Call instructions, but with Relative address. An 8-bit, 16-
bit, or 24-bit offset value can be used, and that allows to call
procedure within the range of –126 to +129 bytes (8-bit
offset;CALR [cc,]e), –32765 to +32770 bytes (16-bit offset;
CALR [cc,]ee), or –8388604 to +8388611 bytes (JR [cc,]
eee) are supported. These instructions are really useful to
program relocatable programs.

Jump is available with Indirect Register mode in addition
to Direct Address mode. It can be useful for implementing
complex control structures such as dispatch tables. When
using Direct Address mode for a Jump or Call, the operand
is used as an immediate value that is loaded into the PC to
specify the address of the next instruction to be executed.

The conditional Return instruction is a companion to the
call instruction; if the condition specified in the instruction
is satisfied, it loads the PC from the stack and pops the
stack.

A special instruction, Decrement and Jump if Non-Zero
(DJNZ), implements the control part of the basic Pascal
FOR loop which can be implemented in an instruction. It
supports 8-bit, 16-bit, and 24-bit displacement.

Note that Jump Relative, Call Relative, and DJNZ instruc-
tions use modulo 2

16

in Native mode, and 2

32

in Extended

mode for address calculation. So it is possible that the
Z380 CPU can jump to an unexpected address.