Control flow instructions are used to alter the sequence of program execution. They allow the program to make decisions, repeat code blocks (loops), and transfer control to different parts of the program based on conditions.
- Conditional and unconditional branches
- Loops
- Switches
The Program Counter (PC) tracks the address of the next instruction to be executed
- To change the PC in assembly, use a
jump
instruction - In assembly, the target of a jump is usually a label, which is converted to a code address by the assembler. Labels are written using colon notation.
# Compare and Test Instruction
CMP (Compare):
Syntax:
cmp operand1, operand2
Compares the values of operand1 and operand2 and sets the status flags in the FLAGS register based on the result of the comparison.
Example:
mov eax, 10 ; Load 10 into EAX
cmp eax, 20 ; Compare EAX with 20
TEST (Bitwise AND):
Syntax:
test operand1, operand2
Performs a bitwise AND operation between operand1 and operand2 and sets the status flags in the FLAGS register based on the result.
Example:
mov eax, 10 ; Load 10 into EAX
cmp eax, 20 ; Compare EAX with 20
# Conditional Jump
Conditional jump instructions in x86 assembly enable the program to alter its flow of execution based on the outcome of a comparison operation. They evaluate the status of processor flags, such as zero, carry, sign, and overflow flags, to determine whether to jump to a specified location in the code.
Syntax:
The syntax of a conditional jump typically follows this pattern:
Jcondition label
Here, Jcondition
represents a mnemonic corresponding to a specific condition, and label
denotes the target location in the code where the program will jump if the condition is met.
Common Conditional Jump Mnemonics
1 JZ / JNZ /JE / JNE
JZ = Jump if Zero
JNZ = Jump if not Zero
JE = Jump if Equal
JNE = Jump if Not Equal
Jumps to the specified destination if the zero flag (ZF) is set (for JZ/JNZ) or if two values are equal (for JE/JNE).
cmp eax, 0 ; Compare EAX with 0
jz zero_detected ; Jump to zero_detected if the result is zero
je dword [address] ; Jump to the memory address stored at 'address' if two values are equal
Jumps to label
if the operand are equal
2 JG / JGE/ JL/JLE
JG = Jump if Greater
JGE = Jump if Greater or Equal
JL = Jump if Less
JLE = Jump if Less or Equal
Jumps to the specified destination based on the result of a comparison.
cmp eax, ebx ; Compare EAX with EBX
jg greater_label ; Jump to greater_label if EAX is greater than EBX
jl dword [address] ; Jump to the memory address stored at 'address' if EAX is less than EBX
3 JS/ JNS
JS = Jump if Sign
JNS = Jump if Not Sign
Jumps to the specified destination based on the state of the sign flag (SF).
cmp eax, 0 ; Compare EAX with 0
js negative_label ; Jump to negative_label if the result is negative
jns dword [addr] ; Jump to the memory address stored at 'addr' if the result is non-negative
4 JP/ JNP
JP = Jump if Parity
JNP = Jump if Not Parity
Jumps to the specified destination based on the state of the parity flag (PF).
test eax, 1 ; Test if the least significant bit of EAX is set
jp even_label ; Jump to even_label if the result is even
jnp dword [addr] ; Jump to the memory address stored at 'addr' if the result is odd
5 JC / JNC
JC = Jump if Carry
JNC = Jump if Not Carry
Jumps to the specified destination based on the state of the carry flag (CF).
cmp eax, ebx ; Compare EAX with EBX
jc carry_label ; Jump to carry_label if a carry occurred during the comparison
jnc dword [addr] ; Jump to the memory address stored at 'addr' if no carry occurred
# Unconditional Jump
Always jump to a new location.
JMP - Unconditionally transfers control to the specified destination.
jmp label ; Jump to the specified label
jmp address ; Jump to the specified memory address
label:
; code
address:
; code
Loop Instruction
Syntax:loop destination
Decrements the ECX register by 1 and jumps to the specified destination if ECX is not zero. This instruction is commonly used for implementing counted loops.
mov ecx, 10 ; Initialize loop counter to 10
loop loop_label ; Decrement ECX and jump to loop_label if ECX is not zero
Repeat Instruction: rep
The rep
(repeat) instruction prefix is used to repeat a string operation a specified number of times. It is commonly used with string instructions such as movs
, stos
, cmps
, scas
, and lods
. The count for the repetition is specified in the CX
(or ECX
/RCX
for 32-bit/64-bit modes) register.
The REP
prefix stands for "repeat" and modifies the behavior of the following instruction to repeat until the counter register (CX
, ECX
, or RCX
) reaches zero.
Here are some examples and explanations of how rep
is used with these instructions:
1: rep movsb
/ rep movsw
/ rep movsd
/ rep movsq
- These instructions are used to move a block of data from the source to the destination.
rep movsb
: Repeats the move of a byte from the source address (pointed to bySI
/ESI
/RSI
) to the destination address (pointed to byDI
/EDI
/RDI
) forCX
times.
mov ecx, 100 ; Set the count to 100
mov esi, source ; Set the source address
mov edi, dest ; Set the destination address
rep movsb ; Move 100 bytes from source to dest
2: rep stosb
/ rep stosw
/ rep stosd
/ rep stosq
- These instructions are used to store a value in a block of memory.
rep stosb
: Repeats the store of a byte fromAL
to the destination address (pointed to byDI
/EDI
/RDI
) forCX
times.
mov ecx, 100 ; Set the count to 100
mov al, 0xFF ; Set the value to be stored
mov edi, buffer ; Set the destination address
rep stosb ; Store 0xFF in 100 bytes starting from buffer
3: rep cmpsb
/ rep cmpsw
/ rep cmpsd
/ rep cmpsq
- These instructions are used to compare two blocks of data.
rep cmpsb
: Compares bytes from the source address (pointed to bySI
/ESI
/RSI
) to the destination address (pointed to byDI
/EDI
/RDI
) forCX
times.
mov ecx, 100 ; Set the count to 100
mov esi, source1 ; Set the first source address
mov edi, source2 ; Set the second source address
rep cmpsb ; Compare 100 bytes from source1 and source2
4: rep scasb
/ rep scasw
/ rep scasd
/ rep scasq
- These instructions are used to scan for a value in a block of memory.
rep scasb
: Scans a block of memory for the value inAL
, starting at the address pointed to byDI
/EDI
/RDI
, forCX
times.
mov ecx, 100 ; Set the count to 100
mov al, 0xFF ; Set the value to be scanned for
mov edi, buffer ; Set the start address of the buffer
rep scasb ; Scan 100 bytes for 0xFF starting from buffer
5: rep lodsb
/ rep lodsw
/ rep lodsd
/ rep lodsq
- These instructions are used to load a block of data from memory to the accumulator.
rep lodsb
: Loads bytes from the source address (pointed to bySI
/ESI
/RSI
) intoAL
forCX
times.
mov ecx, 100 ; Set the count to 100
mov esi, source ; Set the source address
rep lodsb ; Load 100 bytes from source into AL (this would typically be used in a loop or similar construct)