Definition of flags
Required Knowledge
Register memory.
Flags
As well as the registers the CPU uses there is also another register which acts as many small 'flags'. These flags provide extended information for certain operations, such as the compare and test instructions.
The flags' states are useful in many situations, notably conditional jumps.
The following flags are used under the x86 architecture:
- Sign - The sign flag is a copy of the sign bit from an operation (such as a sub/add etc.),
- Parity - This flag is set if the result of certain operations are even (and cleared when odd),
- Zero - This flag is set when an operation's result is zero. This is also set when various compares and tests are executed, this is a common flag used in conditional jumps (jz/jnz being the same as je/jne),
- Overflow - The overflow flag is set when a signed number exceeds the capacity of the result,
- Carry - The carry flag is set when an unsigned number exceeds the capacity of the result,
- Auxillary Carry (Half carry) - This flag is used in Binary Coded Decimal (BCD) arithmetic (ignore this for now),
- Direction - This flag denotes the direction used in various string operations (ignore this for now),
- Trap - The trap flag is used by debuggers (ignore this for now),
- Interrupt - When set this flag will disable hardware breakpoints (ignore this for now).
The zero flag is set when an operation finds a register to be 0. Various operations can set the zero flag, among these are 'dec', 'test', 'cmp' and others. It is worth noting that the zero flag is the equivalent to an 'equals' flag (which doesn't exist seperately). If you compare two values and they are equal, the zero flag will be set. It is for this reason that "jnz" and "jne" (jump if not zero and jump if not equal) are synonamous.
The zero flag is used in many loops, take the following code as an example:
mov ecx, 5
start:
dec ecx
jnz start
...
When the ecx register becomes 0, the dec operation will set the zero flag and thus the 'jnz' will not be taken and execution will continue. This is similar to the code created using a for loop in various high level languages.
A test on an instruction with itself, such as "test eax, eax" will also set the zero flag if the aforementioned register is zero.
Related links
|
|
Attachment
|
Size
|
Date Added
|
| | |
Categories
CategoryDefinitions
There are no comments on this page. [Add comment]