Search code examples
assemblyflagsstatus-register

About assembly conditional code register


suppose we use the addl instruction to perform the equivalent of the C expression "t=a+b",where a,b,t are variables of type int,then the conditional code will be set according to the following C expression:

CF: (unsigned t) < (unsigned a) Unsigned Overflow

ZF: (t==0) Zero

SF: (t<0) Negative

OF: (a<0 == b<0) && (t<0 != a<0) Signed Overflow

Quted from a computer system textbook.

  • CF is the carry flag.
  • ZF is the zero flag.
  • SF is the sign flag.
  • OF is the overflow flag.

i can't figure out why these C expressions have the effect mentioned above.for example,why the expression (t<0) will set the SF flag?? t<0 is either true,or false(since the textbook only told the type of these variables),but why the sign flag has been set?I'm really confused,please help..thanks!


Solution

  • The CPU sets those flags after carrying out arithmetic operations. What you call "C expressions" are really a description of the conditions under which the various CPU flags are set. For example, if the result is 0, the zero flag will be set.

    Or to address you specific question, the following:

    SF: (t<0) Negative

    means that if the result of an arithmetic operation is negative, the CPU sets the SF flag. The 't<0' is not a C expression - it just explains when the flag is set.

    The flags can be used later for control flow, using instructions that branch conditionally on the value of the flags.