Conditional statements in Assembly language allow the program to make decisions and execute different instructions based on certain conditions. This is fundamental for creating dynamic and responsive programs. In this section, we will cover the basics of conditional statements, including how to use comparison instructions and conditional jumps.

Key Concepts

  1. Comparison Instructions: Used to compare values.
  2. Conditional Jumps: Used to alter the flow of execution based on the result of a comparison.
  3. Flags: Special registers that store the results of comparisons.

Comparison Instructions

Comparison instructions are used to compare two values and set the appropriate flags in the CPU's status register. The most common comparison instruction is CMP.

CMP Instruction

The CMP instruction compares two operands and sets the flags accordingly. It does not store the result of the comparison but affects the Zero Flag (ZF), Sign Flag (SF), and Carry Flag (CF), among others.

Syntax:

CMP destination, source

Example:

CMP AX, BX  ; Compare the value in AX with the value in BX

Explanation:

  • If AX is equal to BX, the Zero Flag (ZF) is set.
  • If AX is less than BX, the Carry Flag (CF) is set.
  • If AX is greater than BX, neither ZF nor CF is set.

Conditional Jumps

Conditional jumps alter the flow of execution based on the status of the flags set by the comparison instructions. Some common conditional jump instructions include JE (Jump if Equal), JNE (Jump if Not Equal), JL (Jump if Less), and JG (Jump if Greater).

Common Conditional Jump Instructions

Instruction Description
JE Jump if Equal (ZF = 1)
JNE Jump if Not Equal (ZF = 0)
JL Jump if Less (SF ≠ OF)
JG Jump if Greater (ZF = 0 and SF = OF)
JLE Jump if Less or Equal (ZF = 1 or SF ≠ OF)
JGE Jump if Greater or Equal (SF = OF)

Example: Using CMP and JE

section .data
    num1 db 10
    num2 db 20

section .text
    global _start

_start:
    mov al, [num1]  ; Load num1 into AL
    mov bl, [num2]  ; Load num2 into BL
    CMP al, bl      ; Compare AL and BL
    JE equal        ; Jump to 'equal' if AL == BL

    ; Code for not equal case
    mov eax, 1      ; Exit system call
    int 0x80        ; Call kernel

equal:
    ; Code for equal case
    mov eax, 1      ; Exit system call
    int 0x80        ; Call kernel

Explanation:

  • The program compares num1 and num2.
  • If they are equal, it jumps to the equal label.
  • If they are not equal, it continues to the next instruction.

Practical Exercise

Exercise 1: Simple Comparison

Write an Assembly program that compares two numbers and prints "Equal" if they are equal and "Not Equal" if they are not.

Solution:

section .data
    num1 db 15
    num2 db 15
    msg_equal db 'Equal', 0
    msg_not_equal db 'Not Equal', 0

section .bss
    res resb 1

section .text
    global _start

_start:
    mov al, [num1]  ; Load num1 into AL
    mov bl, [num2]  ; Load num2 into BL
    CMP al, bl      ; Compare AL and BL
    JE equal        ; Jump to 'equal' if AL == BL

    ; Code for not equal case
    mov edx, 9      ; Message length
    mov ecx, msg_not_equal ; Message to write
    mov ebx, 1      ; File descriptor (stdout)
    mov eax, 4      ; System call number (sys_write)
    int 0x80        ; Call kernel
    JMP end         ; Jump to end

equal:
    ; Code for equal case
    mov edx, 5      ; Message length
    mov ecx, msg_equal ; Message to write
    mov ebx, 1      ; File descriptor (stdout)
    mov eax, 4      ; System call number (sys_write)
    int 0x80        ; Call kernel

end:
    mov eax, 1      ; Exit system call
    int 0x80        ; Call kernel

Explanation:

  • The program compares num1 and num2.
  • If they are equal, it prints "Equal".
  • If they are not equal, it prints "Not Equal".

Common Mistakes and Tips

  1. Forgetting to Set Up Data Section: Ensure that all data is properly defined in the .data section.
  2. Incorrect Flag Usage: Understand which flags are affected by comparison instructions and how they influence conditional jumps.
  3. Jump Labels: Make sure jump labels are correctly placed and referenced.

Summary

In this section, we covered the basics of conditional statements in Assembly language. We learned about comparison instructions like CMP, conditional jumps like JE and JNE, and how to use them to control the flow of a program. We also provided a practical exercise to reinforce these concepts. Understanding conditional statements is crucial for creating dynamic and responsive Assembly programs.

© Copyright 2024. All rights reserved