In: Computer Science
How to compute the Z, N, C, and Z the condition code bit values? Given A and B, and after subtracting how do I find the condition codes? Please provide a few examples with the explanation.
The conditional codes Z, N, C stands for zero, negative and carry respectively. The value of conditional codes depends on the result of the previous evaluated expression.
Zero bit:- The value of this bit will be set to 1 if the result of the previous evaluated expression is zero. Otherwise, the value will be set as 0.
Negative bit:- The value of this bit will be set to 1 if the result of the previous evaluated expression is a negative value. The MSB of the result will be 1, if the value is negative. So, negative bit will be set to 1. Otherwise, it will be 0.
Carry bit:- The value of this bit will be set to 1 if the result of the previous evaluated expression generates a carry (addition) or a borrow (subtraction). Otherwise, the value will be set as 0. The value of C bit will also be set to 1, if the number of bits of the result exceeds the number of bits of the operation. (Example: performing n bit operations but resultant bits consists of more than n bits which is not representable)
Example:
Let A and B are equal values. Subtracting two equal values will give zero. Thus, A - B will set the value of zero bit as 1. Since, all the bits of the result are zero, the MSB will also be a zero bit. So, the number is not negative. Thus, N bit is 0. No borrow is generated, so C bit is also 0.
Suppose A is greater than B. Subtracting B from A will result in a positive value. So, Z and N will be set to 0. Since, no borrow is generated, C is also set to 0.
Consider A is less than B. Subtracting B from A will result in a negative value. So, the value of Z is 0 and N is 1. Here, a borrow will be generated, so the value of C will be set to 1.
Hope this helps. Doubts, if any, can be asked in the comment section.