control

Control Flow in Assembly

Control Flow in Assembly

Control flow in assembly allows conditional execution of code based on comparisons or unconditional jumps to other parts of the program.

Basic Instructions

Example: Unconditional Jump

section .text
    global _start

_start:
    jmp skip          ; Jump to label "skip"
    mov rax, 1        ; This line is skipped
skip:
    mov rax, 2        ; Execution continues here
    

Example: Conditional Jump

section .text
    global _start

_start:
    mov rax, 10       ; Load 10 into rax
    cmp rax, 5        ; Compare rax with 5
    jg greater        ; Jump if greater
    mov rbx, 0        ; This line executes if rax <= 5
    jmp done
greater:
    mov rbx, 1        ; This line executes if rax > 5
done:
    

In this example, the program checks whether rax is greater than 5 and jumps accordingly.