w1coding.comAll tutorials

Assembly Control

Assembly lessons
  1. Environment Setup
  2. Introduction
  3. Syntax
  4. Datamovement
  5. Operators
  6. Control
  7. Loops
  8. Interupts
  9. Functions
  10. Memory
  11. System
  12. Debugging
  13. Macros
  14. Execution
  15. Floating
  16. Interupts
  17. Optimization
  18. Linking
  19. Inline
  20. Registers
  21. Bootloader
  22. Macros
  23. Stack
  24. Call
  25. Technique
  26. Responsive

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

  • jmp: Unconditional jump to a label.
  • je: Jump if equal (zero flag is set).
  • jne: Jump if not equal (zero flag is not set).
  • jg: Jump if greater (sign and zero flags are clear).
  • jl: Jump if less (sign flag is set).
  • loop: Decrement rcx and jump if rcx is not zero.

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.