Chapter 37 ½ Intel Microprocessors: 8008 to 8086 639
Bit 4 = 0
bit 3 = 0
bit 2 = complement of original value of ZERO
bit 1 = complement of original value of ZERO
bit 0 = complement of original value of PARITY
With the information saved in the above format in a byte called FLAGS, the following two instructions will restore all the saved flag values:
LDA ADD |
FLAGS A |
;load saved flags into accumulator ;add the accumulator to itself |
This instruction sequence loads the saved flags into the accumulator and then doubles the value, thereby moving each bit one position to the left. This causes each flag to be set to its original value, for the following reasons:
The above algorithm relies on the fact that flag values are always consistent, i.e., that the SIGN flag cannot be a 1 when the ZERO flag is a 1. This is always true in the 8008, since the flags come up in a consistent state whenever the processor is reset and flags can only be modified by instructions which always leave the flags in a consistent state. The 8080 and its derivatives allow the programmer to modify the flags in an arbitrary manner by popping a value of his choice off the stack and into the flags. Thus the above algorithm will not work on those processors.
A code sequence for saving the flags in the required format is as follows:
L1:
L2:
L3: |
MVI JNC ORA JZ ORA
JM ORA JPE ORA STA |
A,0 L1 80H L3 06H
L2 60H L3 01H FLAGS |
; move zero in accumulator ;jump if CARRY not set ;OR accumulator with 80 hex ; jump if negative (SIGN set) ;OR accumulator with 60 hex |
APPENDIX 2 DECIMAL ARITHMETIC
A. Packed BCD
1. Addition. Numbers can be represented as a sequence of decimal digits by using a 4-bit binary encoding of the digits and packing these encodings two to a byte. Such a representation is called packed BCD (unpacked BCD would contain only one digit per byte). In order to preserve this decimal interpretation in performing binary addition on packed BCD numbers, the value 6 must be added to each digit of the sum whenever (1) the resulting digit is greater than 9 or (2) a carry occurs out of this digit as a result of the addition. This is because the 4-bit encoding contains six more combinations than there are decimal digits. Consider the following examples (numbers are written in hexadecimal instead of binary for convenience).
Example 1: 81+52
d2 |
d1 |
d0 |
names of digit positions packed BCD augend packed BCD addend
adjustment because d1 > 9 packed BCD sum |
+ |
8 5 |
1 2 |
|
+ |
D 6 |
3 |
|
1 |
3 |
3 |