Conditional Jumps and Loops
Implement decision-making and repetitive tasks using comparison instructions (CMP, TEST) and conditional jump instructions (JMP, JE, JNE, JL, JG, etc.).
Guiding Program Flow
Programs rarely run straight through. We need to make decisions and repeat actions. This is called control flow.
Assembly language provides special instructions to change the order in which instructions are executed. This allows your programs to be dynamic and respond to different conditions.
Think of it like a choose-your-own-adventure story: you decide which path to take next!
Always Taking the Jump
The simplest way to change program flow is with an unconditional jump using the JMP instruction. It always transfers control to a specified label.
This is like a "goto" statement in higher-level languages. The program will immediately execute instructions starting from the target label.
section .data
msg1 db "Hello!", 0xA
len1 equ $ - msg1
msg2 db "Skipped!", 0xA
len2 equ $ - msg2
msg3 db "End.", 0xA
len3 equ $ - msg3
section .text
global _start
_start:
mov rax, 1 ; syscall write
mov rdi, 1 ; stdout
mov rsi, msg1
mov rdx, len1
syscall ; Print "Hello!"
jmp skip_message ; Unconditional jump
mov rax, 1
mov rdi, 1
mov rsi, msg2
mov rdx, len2
syscall ; This part is skipped!
skip_message:
mov rax, 1
mov rdi, 1
mov rsi, msg3
mov rdx, len3
syscall ; Print "End."
mov rax, 60 ; syscall exit
mov rdi, 0
syscallAll lessons in this course
- Data Movement Instructions (MOV, PUSH, POP)
- Arithmetic and Logic Operations
- Conditional Jumps and Loops
- Bitwise and Shift Instructions