Control Flow and Function Calls
Learn about conditional jumps, loops, and the mechanics of function calls, including stack usage.
Control Flow: Making Programs Smart
In assembly, instructions usually run one after another. This is called sequential execution. But real programs need to make decisions, repeat actions, and call functions.
This is where control flow comes in! It's how a program changes its execution path based on conditions, creating loops and calling subroutines.
Conditional Jumps: If-Statements
Conditional jumps are like if statements in high-level languages. They let your program execute different code blocks based on a condition.
The CMP instruction compares two values and sets CPU flags. Then, jump instructions like JE (Jump if Equal) or JNE (Jump if Not Equal) check these flags to decide whether to jump.
; Example: if (EAX == EBX) goto equal_label;
MOV EAX, 10
MOV EBX, 10
CMP EAX, EBX ; Compare EAX and EBX
JE equal_label ; Jump if Equal
; Code here runs if EAX != EBX
JMP end_label
equal_label:
; Code here runs if EAX == EBX
end_label:
; Program continuesAll lessons in this course
- x86/x64 Assembly Basics
- Registers and Memory Operations
- Control Flow and Function Calls
- The Stack & Calling Conventions