Home › Practice › RTL Design › ALU flags: N, Z, C, V
ALU flags: N, Z, C, V
EasyRTLcombinationalarithmeticarchitecture
After every add or subtract, a processor records a few facts about the result so that later instructions can branch on them: was it negative, was it zero, did it overflow?
Concept first. Processors keep four condition flags after an ALU operation. N (negative): the top bit of the result is 1. Z (zero): every bit of the result is 0. C (carry): the carry out of the top bit; for unsigned numbers it says the result did not fit, and for a subtraction done as "add the inverted b plus 1" the carry is 1 when there was no borrow. V (overflow): for signed numbers, two operands of the same sign gave a result of the other sign. C and V answer different questions: C is about unsigned numbers, V about two's-complement signed numbers. A number like 0111 + 0001 = 1000 is fine as unsigned (7 + 1 = 8) but overflows as signed (+7 + 1 is not -8).
What to build.
alu4(a, b, sub):aandbare 4 bits.sub = 0:y = a + b, andcis the carry out of that sum.sub = 1:y = a + ~b + 1(that is, a minus b), andcis the carry out of that sum (1 means no borrow).- Outputs:
y[3:0],n(=y[3]),z(= 1 whenyis 0),c, andv(signed overflow of the operation that was done). - No clock.
No install and no sign-up needed. You write your answer in the editor and Nishkam checks it.