Chapter 12 - Control Flow Instructions
The x86 CPU has an instruction pointer (IP) that holds the memory address of the current instruction. The IP register is incremented after each instruction to point to the next. You cannot access the IP register directly, however, you can change it indirectly using the jump instruction. You can place labels inside your code to tell the jump instruction where to go.
fubar: cinvoke printf, "This is an infinite loop " jmp fubar
12.2 Flags Register / Status Register
The flags or status register contains bits that are set after the compare (cmp) instruction or other mathematical operations such as add and decrement. Below are example compare instructions:
cmp ecx, 0 cmp [Count], 20 cmp eax, [A]
|0||CF||Carry Flag||Set if the last arithmetic operation carried or borrowed a bit beyond the size of the register.|
|2||PF||Parity Flag||Set if the number of 1's in the least significant byte is even. For example, if the least significant byte is 01001110, then the PF is set.|
|4||AF||Adjust Flag||Carry of Binary Code Decimal (BCD) numbers in arithmetic operation.|
|6||ZF||Zero Flag||Set if the result of the last operation is 0.|
|7||SF||Sign Flag||Set if the result of the last operation is negative.|
|8||TF||Trap Flag||Set if step-by-step debugging is enabled.|
|9||IF||Interrupt Flag||Set if interrupts are enabled.|
|10||DF||Direction Flag||If set, stream operations will decrement their pointer instead of incrementing it, reading memory backwards.|
|11||OF||Overflow Flag||Set if signed arithmetic operation resulted in a value too large for the register to contain.|
12.3 Conditional Jump
Conditional jumps are based on the flags set from the last compare (cmp) operation. The table below lists a few of the flags in the flags register.
The conditional jump can be used to do if statements and for loops as shown in the programs below.
12.4 Floating Point Compare and Conditional Jump