1-bit position to the right or left. For those shifts that do not shift
through the carry, the carry is set to the value of the bit shifted out
of the register and around. Fig. 12-2 recaps the shift actions.
Rotate shifts are used to align fields within bytes for access and
storage and to enable multiple-register shifts. Data in a CPU register
or memory location may be tested by a rotate shift, but not neces-
sarily destroyed.
Discounting the fact that we can obtain the parity of an 8-bit
operand very easily by performing an AND A or OR A, let us illus-
trate how a rotate may be used to compute the parity of a memory
operand.
PARITY
XOR
A
CLEAR PARITY AND C
LD
B,8
INITIALIZE COUNT
LD
HL,MEMOP
MEMORY OPERAND ADDRESS
LOOP
RLC
(HL)
SHIFT OUT BIT TO CY
JR
NC,JUMP1
GO IF NOT A ONE BIT
XOR
1
FLIP PARITY INDICATOR
JUMP1
DEC
B
DECREMENT COUNT
JR
NZ,LOOP
GO IF NOT 8 BITS
DONE
A REGISTER NOW 0 IF EVEN
# OF 1 BITS, 1 IF NOT
The A register and carry are first cleared by the XOR. Now the
RL (HL) instruction is executed eight times. At the end of eight
times, the contents of MEMOP are identical to the contents before
the routine was entered. The A register lsb was set or toggled each
time a one bit was shifted around to bit 0, so that after eight shifts
the A register bit 0 holds a one if the total number of one bits was
odd, or a 0 if the total number of one bits was even.
Another example of the use of the rotate is shown next. Here, the
rotate is used to convert an 8-bit binary operand to binary-ASCII
BXASB
EQU
$
BINARY-TO-ASCII-BINARY
LD
A,(BYTE)
GET BYTE TO CONVERT
LD
C,8
SET # COUNT
LD
IX,BUF+7
BUFFER, LAST BYTE
LOOP
LD
B,30H
ASCII 0
RRCA
SHIFT OUT BIT
JR
NC,JUMP1
GO IF BIT = 0
INC
B
CHANGE TO ASCII 1
JUMP1
LD
(IX),B
STORE ASCII CHARACTER
DEC
IX
POINT TO HIGHER-ORDER SLOT
DEC
C
DECREMENT COUNT
JR
NZ,LOOP
GO IF NOT 8 TESTS
DONE
178
Need help?
Do you have a question about the Z80 and is the answer not in the manual?
Questions and answers