Online Book Reader

Home Category

Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [103]

By Root 1531 0
low-order byte and ADC for the high-order byte. Any carry bit that results from the first addition is included in the second addition. But because you can add only with the accumulator, this little snippet of code requires no fewer than 4 MOV instructions. Lots of MOV instructions usually show up in 8080 code.

This is a good time to talk about the 8080 flags. In our processor in Chapter 17, we had a Carry flag and a Zero flag. The 8080 has three more, called Sign, Parity, and Auxiliary Carry. All the flags are stored in yet another 8-bit register called the Program Status Word (PSW). Instructions such as LDA, STA, or MOV don't affect the flags at all. The ADD, SUB, ADC, and SBB instructions do affect the flags, however, in the following way:

The Sign flag is set to 1 if the most significant bit of the result is 1, meaning that the result is negative.

The Zero flag is set to 1 if the result is 0.

The Parity flag is set to 1 if the result has even parity, which means that the number of 1 bits in the result is even. The parity flag is 0 if the result has odd parity. Parity is sometimes used as a crude form of error checking. This flag isn't often used in 8080 programming.

The Carry flag is set to 1 if an ADD or ADC operation results in a carry or if a SUB and SBB does not result in a carry. (This is different from the implementation of the Carry flag in the Chapter 17 computer.)

The Auxiliary Carry flag is 1 if the operation results in a carry from the low nibble into the high nibble. This flag is used only for the DAA (Decimal Adjust Accumulator) instruction.

Two instructions affect the carry flag directly:

Opcode

Instruction

Meaning

37

STC

Set Carry flag to 1

3F

CMC

Complement Carry flag

The computer in Chapter 17 performed ADD, ADC, SUB, and SBB instructions (although not with nearly as much flexibility), but the 8080 does Boolean AND, OR, and XOR operations as well. Both arithmetic and logical operations are performed by the processor's Arithmetic Logic Unit (ALU).

Opcode

Instruction

Opcode

Instruction

A0

AND A,B

B0

OR A,B

A1

AND A,C

B1

OR A,C

A2

AND A,D

B2

OR A,D

A3

AND A,E

B3

OR A,E

A4

AND A,H

B4

OR A,H

A5

AND A,L

B5

OR A,L

A6

AND A,[HL]

B6

OR A,[HL]

A7

AND A,A

B7

OR A,A

A8

XOR A,B

B8

CMP A,B

A9

XOR A,C

B9

CMP A,C

AA

XOR A,D

BA

CMP A,D

AB

XOR A,E

BB

CMP A,E

AC

XOR A,H

BC

CMP A,H

AD

XOR A,L

BD

CMP A,L

AE

XOR A,[HL]

BE

CMP A,[HL]

AF

XOR A,A

BF

CMP A,A

The AND, XOR, and OR instructions perform bitwise operations. This means that the logical operation is performed on each pair of bits separately.

For example,

MVI A,0Fh

MVI B,55h

AND A,B

The value in the accumulator will be 05h. If the third instruction were an OR, the result would be 5Fh. If the instruction were an XOR, the result would be 5Ah.

The CMP (Compare) instruction is just like the SUB instruction except that the result isn't stored in the accumulator. In other words, the CMP performs a subtraction and then throws away the result. What's the point? The flags! The flags tell you the relationship between the 2 bytes that you compared. For example, consider the following instructions:

MVI B,25h

CMP A,B

After this instruction, the contents of A remain unchanged. However, the Zero flag is set if the value in A equals 25h. The Carry flag is set if the value in A is less than 25h.

The eight arithmetic and logic operations also have versions that operate on an immediate byte:

Opcode

Instruction

Opcode

Instruction

C6

ADI A,xx

E6

ANI A,xx

CE

ACI A,xx

EE

XRI A,xx

D6

SUI A,xx

F6

ORI A,xx

DE

SBI A,xx

FE

CPI A,xx

For example, the two lines shown above can be replaced with

CPI A,25h

Here are two miscellaneous 8080 instructions:

Opcode

Instruction

27

DAA

2F

CMA

CMA stands for Complement Accumulator. It performs a ones' complement of the value in the accumulator. Every 0 becomes a 1 and every 1 becomes a 0. If the accumulator is 01100101, the CMA instruction causes it to be 10011010.

Return Main Page Previous Page Next Page

®Online Book Reader