Tue, 3 Feb 2015, 01:25

A student writes:

> Professor Patt,
> This may seem a trivial question. However I was wondering why the choice 
> was made to use all three condition code bits (n,z,p), when you could 
> represent the 'signed state' of the PSR in just two bits? 
> i.e. only use n and z, if both are low then the state must be positive. 
> I can not think of why a bit would matter all that much to the PSR, 
> I was just wondering if there is a specific reason for the choice, 
> like speed in the microarchitecture or for simplicity. 
> Thanks for your clarification-
> <>

Good question!  It gives me a chance to tell you more about condition codes.
...as well as repeat some stuff I mentioned last Wednesday very fast!

First, most ISAs indeed do as you suggest, just N and Z, recognizing if both
are 0, the value must be positive.  Recall that the LC-3b is an afterthought
of the LC-3 which I came up with to teach the freshman course, which also
involves a fair dose of assembly language programming.  The LC-2, in fact,
initially had just N and Z.  ...and the LC-1 had just N, which is enough
actually although it makes the code for testing a little unwieldly.  

I added the P bit in order to provide a mechanism where the program flow 
never had to be contorted.  That is, suppose the flow is better if you can
reverse the conditions underwhich you take the branch vs fall through.  Adding
the P bit makes this simple.  Just flip bits [11:9] of the branch instruction.
For example, bits [11:9] = 110 says branch if less than or equal to zero,
whereas bits [11:9] = 001 says branch if greater than zero.  If you want to
branch if A=B, we calculate A-B in the ALU, and follow it with a branch with 
[11:9] = 010.  If we want to take the fall through path f A=B, we follow the 
subtract with [11:9] = 101.

You are correct, I did this to make it easier for freshmen to program the LC-3
in assembly language.

You could argue that since I took out LDI and STI when I moved to the LC-3b,
shouldn't I have also used the standard set of condition codes and got rid of
P.  Answer: Probably!  Guilty as charged!  I didn't.

If you are still paying attention, I will close with the statement that just
about all ISAs have four condition codes (N,Z,V,C).  They leave off P for the
reason you stated.  They add V (for oVerflow) in case the ALU adds two positive
numbers and produces a negative result (or two negatives and produces a 
positive).  They also add C (for carry) in case they want to write procedures
to operate on long integers (integers that are a multiple of the word length
of the machine - that is, 32 bits, 48 bits, etc. since the word length of the
LC-3b is 16 bits).  We can write procedures to add 2's complement integers of
arbitrary size without a C bit, but having a C bit makes it easier, since it
enables us to add 16 bits of A, 16 bits of B, and the carry out of the previous
add with a single instruction ADC (ADD with Carry).


Thanks for asking.  Sorry I got carried away!  :-)

Yale Patt