What is the purpose of the reserved/undefined bit

2019-06-24 16:36发布

问题:

In the flag register of Z80, 8080, 8085, and 8086 processors, what is the purpose of bits 1, 3, 5, which are documented as "reserved" or "undefined"?

回答1:

These bits are unused; that is, no instruction explicitly sets them to any value. The designers decided that 5/6 flags was enough, and they just left the remaining bits of the flags register unused.

They are documented as being "undefined" because it is not possible to know in advance which value will they have after any of the instructions are executed—the processor design is simpler that way, as opposed to setting them explicitly to 0 or 1.

Now, strictly speaking, and for the Z80 at least, these flags do actually get a predictable value—after all, a processor will always produce the same output and change to internal state given the same combination of input and previous internal state. For example, this document about undocumented Z80 instructions says the following about CPI:

Flag 3 and 5 are set like this: Take A, subtract the last (HL), and then decrease it with 1 if the H flag was set (/after/ the CP). Bit 1 of this value is flag 5, bit 3 is flag 3.

The point, however, is that this behavior is unintentional—just a side effect—and not guaranteed to be present in future iterations of the processors. That's why they are documented as being just "undefined", as in, "they aren't useful to you so just ignore them".

Note that while on Z80 all bits of the F register can be set to any state, on i8080 bits 3 and 5 (that correspond to the undocumented Z80 flags) always read to be zero and bit 1 (that corresponds to Z80's N flag) always reads to be one.