This the multi-page printable view of this section.Click here to print.

Return to the regular view of this page.

Arithmetic

Arithmetic

Table of Contents

1 - ADD without carry

Addition without carry

The ADD instruction performs an addition without carry. Any overflow from the addition will be passed on to the carry flag.

1.1 - ADD r without carry

Addition of a register without carry
76543210
 
\(A \longleftarrow A + r\)
ADD A, r
10000r
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h----
sset if result negative
zset if result is 0
hset if carry from bit 3
Opcode Matrix
ABCDEHL
A
ADD A,A
8714
ADD A,B
8014
ADD A,C
8114
ADD A,D
8214
ADD A,E
8314
ADD A,H
8414
ADD A,L
8514
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register

1.2 - ADD n without carry

Addition of a number without carry
76543210
 
\(A \longleftarrow A + n\)
ADD A, n
11000110C6
n
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset if carry from bit 3
p/vset if overflow
cset if carry from bit 7
Opcode Matrix
n
A
ADD A,n
C6nn27
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Implicit

1.3 - ADD (dd) without carry

Addition of memory without carry
76543210
 
\(A \longleftarrow A + (HL)\)
ADD A, (HL)
1000011086
 
\(A \longleftarrow A + (IX+d)\)
ADD A, (IX+d)
11011101DD
1000011086
d
 
\(A \longleftarrow A + (IY+d)\)
ADD A, (IY+d)
11111101FD
1000011086
d
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset if carry from bit 3
p/vset if overflow
cset if carry from bit 7
Opcode Matrix
(HL)(IX+d)(IY+d)
A
ADD A,(HL)
8617
ADD A,(IX+d)
DD86nn319
ADD A,(IY+d)
FD86nn319
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Memory

1.4 - ADD ss to HL without carry

Addition without carry
76543210
 
\(HL \longleftarrow HL + dd\)
ADD HL, dd
00dd1001
 
\(IX \longleftarrow IX + pp\)
ADD IX, pp
11011101DD
00pp1001
 
\(IY \longleftarrow IY + mm\)
ADD IY, mm
11111101FD
00mm1001
Registers
Valueddmmpp
00BCBCBC
01DEDEDE
10HLIYIX
11SPSPSP
Flags Affected
Flags
sz-h---c
sset if result negative
zset if result is 0
hset if carry from bit 11
cset if carry from bit 15
Opcode Matrix
BCDEHLSPIXIY
HL
ADD HL,BC
09111
ADD HL,DE
19111
ADD HL,HL
29111
ADD HL,SP
39111


IX
ADD IX,BC
DD09215
ADD IX,DE
DD19215

ADD IX,SP
DD39215
ADD IX,IX
DD29215

IY
ADD IY,BC
FD09215
ADD IY,DE
FD19215

ADD IY,SP
FD39215

ADD IY,IY
FD29215
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register

2 - ADC Add with Carry

Addition with carry

The ADC instruction performs an addition with carry. If carry is set then it will be included in the calculation whilst any overflow from the addition will be passed on to the carry flag.

2.1 - ADC 8 bit add with Carry

Addition with carry
76543210
 
\(A \longleftarrow A + r + Carry\)
ADC A,r
10001r
 
\(A \longleftarrow A + n + Carry\)
ADC A,n
11001110CE
n
 
\(A \longleftarrow A + (HL) + Carry\)
ADC A, (HL)
100011108E
 
\(A \longleftarrow A + (IX+d) + Carry\)
ADC A, (IX + d)
11011101DD
100011108E
d
 
\(A \longleftarrow A + (IY+d) + Carry\)
ADC A, (IY + d)
11111101FD
100011108E
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset if carry from bit 3
p/vset if overflow
cset if carry from bit 7
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)n
A
ADC A,A
8F14
ADC A,B
8814
ADC A,C
8914
ADC A,D
8A14
ADC A,E
8B14
ADC A,H
8C14
ADC A,L
8D14
ADC A,(HL)
8E17
ADC A,(IX+d)
DD8Enn319
ADC A,(IY+d)
FD8Enn319
ADC A,n
CEnn27
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory Implicit

2.2 - ADC 16 bit add with Carry

Addition with carry
76543210
 
\(HL \longleftarrow HL + ss + Carry\)
ADC HL, dd
11101101ED
01dd1010
Registers
Valuedd
00BC
01DE
10HL
11SP
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset if carry from bit 11
p/vset if overflow
cset if carry from bit 15
Opcode Matrix
BCDEHLSP
HL
ADC HL,BC
ED4A215
ADC HL,DE
ED5A215
ADC HL,HL
ED6A215
ADC HL,SP
ED7A215
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register

3 - SUB Subtract without Carry

Subtraction without Carry

\(A \longleftarrow A - s\)

This s operand is any of r, n, (HL), (IX+d), or (IY+d).

These possible op code/operand combinations are assembled as follows in the object code:

76543210
 
SUB r
10010r
 
SUB n
11010110D6
n
 
SUB (HL)
1001011096
 
SUB (IX+d)
11011101DD
1001011096
d
 
SUB (IX+d)
11111101FD
1001011096
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset if borrow from bit 4
p/vset if overflow
cset if borrow
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)n
A
SUB A,A
9714
SUB A,B
9014
SUB A,C
9114
SUB A,D
9214
SUB A,E
9314
SUB A,H
9414
SUB A,L
9514
SUB A,(HL)
9617
SUB A,(IX+d)
DD96nn319
SUB A,(IY+d)
FD96nn319
SUB A,n
D6nn27
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory Implicit

4 - SBC Subtract with Carry

Subtraction with Carry
76543210
 
\(A \longleftarrow A - r - Carry\)
SBC A, r
10011r
 
\(A \longleftarrow A - n - Carry\)
SBC A,n
11011110DE
 
\(A \longleftarrow A - (HL) - Carry\)
SBC A, (HL)
100111109E
 
\(A \longleftarrow A - (IX+d) - Carry\)
SBC A, (IX+d)
11011101DD
100111109E
d
 
\(A \longleftarrow A - (IY+d) - Carry\)
SBC A, (IY+d)
11111101FD
100111109E
d
 
\(A \longleftarrow A - ss - Carry\)
SBC HL, ss
11101101ED
01dd0010
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Registers
Valuedd
00BC
01DE
10HL
11SP
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset if borrow from bit 4
p/vset if overflow
cset if borrow
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)nBCDEHLSP
A
SBC A,A
9F14
SBC A,B
9814
SBC A,C
9914
SBC A,D
9A14
SBC A,E
9B14
SBC A,H
9C14
SBC A,L
9D14
SBC A,(HL)
9E17
SBC A,(IX+d)
DD9Enn119
SBC A,(IY+d)
FD9Enn119
SBC A,n
DEnn27




HL











SBC HL,BC
ED42215
SBC HL,DE
ED52215
SBC HL,HL
ED62215
SBC HL,SP
ED72215
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory Implicit

5 - AND

Binary AND

\(A \longleftarrow A \land s\)

76543210
 
AND r
10100r
 
AND n
11100110E6
n
 
AND(HL)
10100110A6
 
AND (IX+d)
11011101DD
10100110A6
d
 
AND (IY+d)
11111101FD
10100110A6
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset
p/vset if overflow
creset
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)n
A
AND A,A
A714
AND A,B
A014
AND A,C
A114
AND A,D
A214
AND A,E
A314
AND A,H
A414
AND A,L
A514
AND A,(HL)
A617
AND A,(IX+d)
DDA6nn319
AND A,(IY+d)
FDA6nn319
AND A,n
E6nn27
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory Implicit

6 - OR

Binary OR

\(A \longleftarrow A \lor s\)

76543210
 
OR r
10110r
 
OR n
11110110F6
n
 
OR (HL)
10110110B6
 
OR (IX+d)
11011101DD
10110110B6
d
 
OR (IY+d)
11111101FD
10110110B6
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hreset
p/vset if overflow
creset
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)n
A
OR A,A
B714
OR A,B
B014
OR A,C
B114
OR A,D
B214
OR A,E
B314
OR A,H
B414
OR A,L
B514
OR A,(HL)
B617
OR A,(IX+d)
DDB6nn319
OR A,(IY+d)
FDB6nn319
OR A,n
F6nn27
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory Implicit

7 - XOR

Binary Exclusive OR

\(A \longleftarrow A \oplus s\)

76543210
 
XOR r
10101r
 
XOR n
11101110EE
n
 
XOR (HL)
10101110AE
 
XOR (IX+d)
11011101DD
10101110AE
d
 
XOR (IY+d)
11111101FD
10101110AE
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hreset
p/vset if overflow
creset
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)n
A
XOR A,A
AF14
XOR A,B
A814
XOR A,C
A914
XOR A,D
AA14
XOR A,E
AB14
XOR A,H
AC14
XOR A,L
AD14
XOR A,(HL)
AE17
XOR A,(IX+d)
DDAEnn319
XOR A,(IY+d)
FDAEnn319
XOR A,n
EEnn27
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory Implicit

8 - INC Increment

Increment by 1

INC increments either an 8-bit register or an 16-bit register pair.

8.1 - INC 8-bit Increment

Increment 8-bit register by 1
76543210
 
\(s \longleftarrow r + 1\)
INC r
00r100
 
\((HL) \longleftarrow (HL) + 1\)
INC (HL)
0011010034
 
\((IX+d) \longleftarrow (IX+d) + 1\)
INC (IX+d)
11011101DD
0011010034
d
 
\((IY+d) \longleftarrow (IY+d) + 1\)
INC (IY+d)
11111101FD
0011010034
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v--
sset if result negative
zset if result is 0
hset if carry from bit 3
p/vset if register was 0x7F before operation, reset otherwise
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)
Op
INC A
3C14
INC B
0414
INC C
0C14
INC D
1414
INC E
1C14
INC H
2414
INC L
2C14
INC (HL)
34111
INC (IX+d)
DD34nn323
INC (IY+d)
FD34nn323
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory

8.2 - INC 16-bit Increment

Increment 16-bit register pair by 1
76543210
 
\(dd \longleftarrow dd + 1\)
INC qq
00dd0011
 
\(IX \longleftarrow IX + 1\)
INC IX
11011101DD
0010001123
 
\(IY \longleftarrow IY + 1\)
INC IY
11111101FD
0010001123
Registers
Valuedd
00BC
01DE
10HL
11SP
Flags Affected
None.
Opcode Matrix
BCDEHLSPIXIY
Op
INC BC
0316
INC DE
1316
INC HL
2316
INC SP
3316
INC IX
DD23210
INC IY
FD23210
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register

9 - DEC Decrement

Decrement

DEC decrements either an 8-bit register or an 16-bit register pair.

9.1 - DEC 8-bit Decrement

Decrement
76543210
 
\(r \longleftarrow r - 1\)
DEC r
00r101
 
\((HL) \longleftarrow (HL) - 1\)
DEC (HL)
00110101
 
\((IX+d) \longleftarrow (IX+d) - 1\)
DEC (IX+d)
11011101DD
0011010135
d
 
\((IY+d) \longleftarrow (IY+d) - 1\)
DEC (IY+d)
11111101FD
0011010135
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v--
sset if result negative
zset if result is 0
hset if borrow from bit 4
p/vset if register was 0x80 before operation, reset otherwise
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)
Op
DEC A
3D14
DEC B
0514
DEC C
0D14
DEC D
1514
DEC E
1D14
DEC H
2514
DEC L
2D14
DEC (HL)
35111
DEC (IX+d)
DD35nn323
DEC (IY+d)
FD35nn323
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory

9.2 - DEC 16-bit Decrement

Decrement 16-bit register pair
76543210
 
\(dd \longleftarrow dd - 1\)
DEC dd
00dd1011
 
\(IX \longleftarrow IX - 1\)
DEC IX
11011101DD
001010112B
 
\(IY \longleftarrow IY - 1\)
DEC IY
11111101FD
001010112B
Registers
Valuedd
00BC
01DE
10HL
11SP
Flags Affected
None.
Opcode Matrix
BCDEHLSPIXIY
Op
DEC BC
0B16
DEC DE
1B16
DEC HL
2B16
DEC SP
3B16
DEC IX
DD2B210
DEC IY
FD2B210
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register

10 - CP

Comparison

\(A - s\)

76543210
 
CP r
10111r
 
CP n
11111110FE
n
 
CP (HL)
10111110BE
 
CP (IX+d)
11011101DD
10111110BE
d
 
CP (IY+d)
11111101FD
10111110BE
d
Registers
Registerr
B000
C001
D010
E011
H100
L101
A111
Flags Affected
Flags
sz-h-p/v-c
sset if result negative
zset if result is 0
hset if borrow from bit 4
p/vset if overflow
cset if borrow
Opcode Matrix
ABCDEHL(HL)(IX+d)(IY+d)n
Op
CP A
BF14
CP B
B814
CP C
B914
CP D
BA14
CP E
BB14
CP H
BC14
CP L
BD14
CP (HL)
BE17
CP (IX+d)
DDBEnn319
CP (IY+d)
FDBEnn319
CP n
FEnn27
Opcode Matrix Legend
Instruction Opcode hexSize bytesCycle count
 Register Memory Implicit