X86 Assembly/Control Flow

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Comparison Instructions

test arg1, arg2 GAS Syntax
test arg2, arg1 Intel syntax


performs a bit-wise AND on the two operands and sets the flags, but does not store a result.


cmp arg1, arg2 GAS Syntax
cmp arg2, arg1 Intel syntax


performs a subtraction between the two operands and sets the flags, but does not store a result.

[edit] Jump Instructions

[edit] Unconditional Jumps

jmp loc

loads EIP with the specified address (i.e. the next instruction executed will be the one specified by jmp).

[edit] Jump on Equality

je loc

Loads EIP with the specified address, if operands of previous CMP instruction are equal. For example:

mov ecx, 5
mov edx, 5
cmp ecx, edx
je equal
; if it did not jump to the label equal, then this means 5 and 5 are not equal.
equal:
; if it jumped here, then this means 5 and 5 are equal

jne loc

Loads EIP with the specified address, if operands of previous CMP instruction are not equal.

[edit] Jump if Greater

jg loc

Loads EIP with the specified address, if first operand of previous CMP instruction is greater than the second (performs signed comparison).

jge loc

Loads EIP with the specified address, if first operand of previous CMP instruction is greater than or equal to the second (performs signed comparison).

ja loc

Loads EIP with the specified address, if first operand of previous CMP instruction is greater than the second. ja is the same as jg, except that it performs an unsigned comparison.

jae loc

Loads EIP with the specified address, if first operand of previous CMP instruction is greater than or equal to the second. jae is the same as jge, except that it performs an unsigned comparison.

[edit] Jump if Less

jl loc

Loads EIP with the specified address, if first operand of previous CMP instruction is less than the second (performs signed comparison).

jle loc

Loads EIP with the specified address, if first operand of previous CMP instruction is less than or equal to the second (performs signed comparison).


jb loc

Loads EIP with the specified address, if first operand of previous CMP instruction is less than the second. jb is the same as jl, except that it performs an unsigned comparison.

jbe loc

Loads EIP with the specified address, if first operand of previous CMP instruction is less than or equal to the second. jbe is the same as jle, except that it performs an unsigned comparison.

[edit] Jump on Overflow

jo loc

Loads EIP with the specified address, if the overflow bit is set on a previous arithmetic expression.

[edit] Jump on Zero

jnz loc

Loads EIP with the specified address, if the zero bit is not set from a previous arithmetic expression. jnz is identical to jne.

jz loc

Loads EIP with the specified address, if the zero bit is set from a previous arithmetic expression. jz is identical to je.

[edit] Function Calls

call proc

pushes the value EIP+4 onto the top of the stack, and jumps to the specified location. This is used mostly for subroutines.

ret [val]

Loads the next value on the stack into EIP, and then pops the stack the specified number of times. If val is not supplied, the instruction will not pop any values off the stack after returning.

[edit] Loop Instructions

loop arg

The loop instruction decrements ECX and jumps to the address specified by arg unless decrementing ECX caused its value to become zero. For example:

mov ecx, 5
start_loop:
; the code here would be executed 5 times
loop start_loop

loop does not set any flags.

loopx arg

These loop instructions decrement ECX and jump to the address specified by arg if their condition is satisfied, unless decrementing ECX caused its value to become zero.

  • loope
  • loopne
  • loopnz
  • loopz

[edit] Enter and Leave

enter arg

Creates a stack frame with the specified amount of space allocated on the stack.

leave

destroys the current stack frame, and restores the previous frame

[edit] Other Control Instructions

hlt

Halts the processor

nop

"No Operation". This instruction doesn't do anything, but wastes an instruction cycle in the processor. This instruction is often translated to an XCHG operation with the operands EAX and EAX.

lock

asserts #LOCK

wait

waits for the CPU to finish its last calculation