Control flow instructions | Assembly language

Control flow instructions in assembly language alter the sequence of program execution by changing the order of instructions.

Unconditional Jump (jmp)

Transfers control to the specified target address unconditionally.

jmp target_label ; Jump to the instruction labeled 'target_label'

Conditional Jumps (je, jne, jg, jl, etc.)

Conditionally jumps to a target address based on the status of certain flags.

cmp eax, ebx ; Compare the values in EAX and EBX je equal_label ; Jump to 'equal_label' if EAX is equal to EBX

Call and Return (call, ret)

Call transfers control to a subroutine, and ret returns control from a subroutine.

call subroutine ; Call the subroutine at 'subroutine' ; ... (subroutine code) ret ; Return from the subroutine

Loop (loop)

Repeats a block of instructions a specified number of times based on the ECX register.

mov ecx, 5 ; Set the loop counter to 5 loop loop_label ; Jump to 'loop_label' while ECX is not zero

Conditional Move (cmov)

Moves data from one operand to another based on a condition.

cmp eax, ebx ; Compare the values in EAX and EBX cmovge eax, ebx ; Move the value from EBX to EAX if greater than or equal

Interrupt (int)

Triggers a software interrupt, invoking a specific kernel routine or system call.

mov eax, 1 ; System call number for exit xor ebx, ebx ; Exit status: 0 int 0x80 ; Invoke system call

Here are some additional examples of how control flow instructions can be used in assembly language:

; Check if the user has entered the correct password. cmp password, "correct_password" jne wrong_password ; If the user has entered the correct password, grant them access to the system. ; If the user has entered the incorrect password, display an error message. wrong_password: mov eax, 4 call printf ; Print the error message ; Exit the program. mov eax, 1 call exit


Control flow instructions including jmp, call, ret, conditional jumps (je, jne), loops (loop), and interrupt (int), enable the manipulation of program execution by altering the sequence of instructions based on conditions, subroutine calls, and jumps to specific addresses. These instructions are essential for creating branching logic, loops, and organizing the flow of control within a low-level program.