Code_ The Hidden Language of Computer Hardware and Software - Charles Petzold [104]
XRI A,FFh
DAA stands for Decimal Adjust Accumulator, as I mentioned earlier, and it's probably the most sophisticated single instruction in the 8080. A whole little section of the microprocessor is dedicated specifically to performing this instruction.
The DAA instruction helps a programmer implement decimal arithmetic using a method of representing numbers known as binary-coded decimal, or BCD. In BCD, each nibble of data may range only from 0000 through 1001, corresponding to decimal digits 0 through 9. The 8 bits of a byte can store two decimal digits in BCD format.
Suppose the accumulator contains the BCD value 27h. Because this is a BCD value, it actually refers to the decimal value 27. (Normally, the hexadecimal value 27h has the decimal equivalent 39.) Suppose also that register B contains the BCD value 94h. If you execute the instruction
MVI A,27h
MVI B,94h
ADD A,B
the accumulator will contain the value BBh, which, of course, isn't a BCD value because the nibbles of BCD bytes never exceed 9. But now execute the instruction
DAA
Now the accumulator contains 21h, and the Carry flag is set. That's because the decimal sum of 27 and 94 equals 121. This can be handy if you need to do BCD arithmetic.
Very often it's necessary to add 1 to a particular value or subtract 1 from a value. In the multiplication program in Chapter 17, we needed to subtract 1 from a value, and the way we did it was to add FFh, which is the two's complement value of –1. The 8080 includes special instructions for increasing a register or memory location by 1 (this is known as an increment) or decreasing by 1 (decrement):
Opcode
Instruction
Opcode
Instruction
04
INR B
05
DCR B
0C
INR C
0D
DCR C
14
INR D
15
DCR D
1C
INR E
1D
DCR E
24
INR H
25
DCR H
2C
INR L
2D
DCR L
34
INR [HL]
35
DCR [HL]
3C
INR A
3D
DCR A
The single-byte INR and DCR instructions affect all flags except the Carry flag.
The 8080 also includes four Rotate instructions. These instructions shift the contents of the accumulator 1 bit to the left or right:
Opcode
Instruction
Meaning
07
RLC
Rotate accumulator left
0F
RRC
Rotate accumulator right
17
RAL
Rotate accumulator left through carry
1F
RAR
Rotate accumulator right through carry
Only the Carry flag is affected by these instructions.
Suppose the accumulator contains the value A7h, or 10100111 in binary. The RLC instruction shifts the bits left. The lowest bit (shifted out of the bottom) becomes the highest bit (shifted into the top) and also determines the state of the Carry flag. The result is 01001111, and the Carry flag is 1. The RRC instruction shifts the bits right in the same way. Beginning with 10100111, the result after an RRC instruction is 11010011, and the Carry flag is 1.
The RAL and RAR instructions work a little differently. The RAL instruction sets the Carry flag to the lowest bit of the accumulator when shifting left but sets the highest bit to the previous contents of the Carry flag. For example, if the accumulator contains 10100111 and the Carry flag is 0, RAL causes the accumulator to become 01001110 and the Carry flag to be 1. Similarly, under the same initial conditions RAR causes the accumulator to become 01010011 and the Carry flag to be set to 1.
The shift instructions come in handy when you're multiplying a number by 2 (that's a shift left) or dividing a number by 2 (a shift right).
The memory that the microprocessor addresses is called random access memory (RAM) for a reason: The microprocessor can access any particular memory location simply by supplying an address of that location. RAM is like a book that we can open to any page. It's not like a week's worth of a newspaper on microfilm. Finding something in Saturday's edition requires us to scan through most of the week. Similarly, playing the last song on a cassette tape requires us to fast forward through the whole side of the album. The term for microfilm or tape storage isn't random access