Chapter 12 - Control Flow Instructions


12
.1  Jump

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]

Flags Register

Bit

Abbr

Name Description
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) or mathematical operation.  For example, if you compare the values in two registers, the CPU will subtract the two numbers and set ZF (Zero Flag) to 1 if the subtraction is zero.

Conditional Jumps

Instr

Description

Example

je Jump if equal cmp eax, 100
je Fubar
jne Jump if not equal inc ecx
cmp ecx, 10
jne MyLoop
jz Jump if zero dec ecx
jz LoopFinished
jg Jump if greater than cmp [Value], 100
jg Approved
jge Jump if greater than or equal to cmp [Age], 18
jge VotingAge
jl Jump if less than cmp [Value], 100
jl Denied
jle Jump if less than or equal to cmp [HitPoints], 0
jle GameOver

The conditional jump can be used to do if statements and for loops as shown in the programs below.

if_statement.asm

format PE console
include 'win32ax.inc'

;=======================================
section '.code' code readable executable
;=======================================
start:
        cinvoke printf, "Enter a number from 1 - 100: "
        cinvoke scanf, "%d", Num
        cmp [Num], 50
        je Equal
        jg Over
        jl Under
Equal:
        cinvoke printf, "Your number is 50 %c", 10
        jmp Ending
Over:
        cinvoke printf, "Your number is over 50 %c", 10
        jmp Ending
Under:
        cinvoke printf, "Your number is under 50 %c", 10
Ending:
        invoke Sleep,-1

;======================================
section '.bss' data readable writeable
;======================================
Num     rd 1

;====================================
section '.idata' import data readable
;====================================
library msvcrt,'msvcrt.dll',kernel32,'kernel32.dll'
import msvcrt,printf,'printf',scanf,'scanf'
import kernel32,Sleep,'Sleep'

Output

Enter a number from 1 - 100: 75
Your number is over 50

for_loop.asm

format PE console
include 'win32ax.inc'

;=======================================
section '.code' code readable executable
;=======================================
start:
     cinvoke printf,"Loop Demonstration %c", 10
myloop:
     cinvoke printf,"%d %c",[Count],10
     dec [Count]
     jnz myloop
     invoke Sleep,-1

;======================================
section '.data' data readable writeable
;======================================
Count  dd 10

;====================================
section '.idata' import data readable
;====================================
library msvcrt,'msvcrt.dll',kernel32,'kernel32.dll'
import msvcrt,printf,'printf'
import kernel32,Sleep,'Sleep'          

Output

Loop Demonstration
10
9
8
7
6
5
4
3
2
1


12
.4  Floating Point Compare and Conditional Jump

TBA