program code. Many of the flags in EFLAGSare system flags that determine
the current state of the processor. Other than these system flags, there are also
eight status flags, which represent the current state of the processor, usually
with regards to the result of the last arithmetic operation performed. The fol-
lowing sections describe the most important status flags used in IA-32.
The Overflow Flags (CF and OF)
The carry flag(CF) and overflow flag(OF) are two important elements in arith-
metical and logical assembly language. Their function and the differences
between them aren’t immediately obvious, so here is a brief overview.
The CF and OF are both overflow indicators, meaning that they are used to
notify the program of any arithmetical operation that generates a result that is
too large in order to be fully represented by the destination operand. The dif-
ference between the two is related to the data types that the program is deal-
ing with.
Unlike most high-level languages, assembly language programs don’t
explicitly specify the details of the data types they deal with. Some arithmeti-
cal instructions such as ADD(Add) and SUB(Subtract) aren’t even aware of
whether the operands they are working with are signed or unsigned because
it just doesn’t matter—the binary result is the same. Other instructions, such as
MUL (Multiply) and DIV (Divide) have different versions for signed and
unsigned operands because multiplication and division actually produce dif-
ferent binary outputs depending on the exact data type.
One area where signed or unsigned representation always matters is over-
flows. Because signed integers are one bit smaller than their equivalent-sized
unsigned counterparts (because of the extra bit that holds the sign), overflows
are triggered differently for signed and unsigned integers. This is where the
carry flag and the overflow flag come into play. Instead of having separate
signed and unsigned versions of arithmetic instructions, the problem of cor-
rectly reporting overflows is addressed by simply having two overflow flags:
one for signed operands and one for unsigned operands. Operations such as
addition and subtraction are performed using the same instruction for either
signed or unsigned operands, and such instructions set both groups of flags
and leave it up to the following instructions to regard the relevant one.
For example, consider the following arithmetic sample and how it affects
the overflow flags:
mov ax, 0x1126 ; (4390 in decimal)
mov bx, 0x7200 ; (29184 in decimal)
add ax, bx
520 Appendix B
22_574817 appb.qxd 3/16/05 8:45 PM Page 520