ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3137Conditional Control Fl…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3137

    Computer Organization and Assembly Language

    Progress0 / 73 done
    Topics73
    1Introduction to Computer Organization2Assembly Language3Comparison of Low-Level and High-Level Languages
    4General Purpose and Special Purpose Registers5Introduction and Usage of RAM6Processor7Registers8System Bus9Instruction Execution Cycle10Assembly and Machine Language11Assembler12Linker and Link Libraries13Programmer's View of a Computer System14RISC and CISC Architecture15Physical Address Calculation16Basic Memory Organization17CPU Organization18Top Level View of Computer Function and Interconnection19Assembler Instruction Cycle20Execute Cycle21Interrupts22Interrupt Cycle23Memory Connection24Input/Output Connection25CPU Connection26MASM27MIPS28Defining Data in MASM Assembler29Elements of Assembly Language30Integer Constants31Integer Expressions32Real Number Constants33Character Constants34String Constants35Reserved Words36Identifiers37Directives38Instructions39The NOP (No Operation) Instruction40Adding and Subtracting Integer41INC and DEC Instructions42NEG Instruction43How to Move Integer Number in Register44Adding and Subtracting Numbers in Registers45Declaration and Initialization of Variables46Moving Data from Variable to Register47Data Definition Statement48BYTE and SBYTE Data49WORD and SWORD Data50Defining DWORD and SDWORD Data51Knowledge about Different Data Types52Operations, Array & Loops53Division and Multiplication in Assembly54Jumps Based on Specific Flags55Jumps Based on Equality56Simple Jump Statements57Jumps Based on Specific Condition58Code Examples59Practice on MASM60Procedures61File Operations Procedures62Labels in Procedures63Stack64Runtime Stack65Conditional Control Flow Directives66Compound Expressions67Data Representation & Conversion68Architecture69Data Path70Control Unit71Critical Path72General Principles of Pipelining73Pipelined Y86 Implementations
    7 min read
    Topic 65 of 73
    Topic Study Note Illustration
    65

    Conditional Control Flow Directives

    In MASm (Microsoft Macro Assembler) and other assembly languages, conditional control flow directives allow the programmer to modify the flow of program execution based on certain conditions....

    Conditional Control Flow Directives in MASM (Assembly Language)

    In MASm (Microsoft Macro Assembler) and other assembly languages, conditional control flow directives allow the programmer to modify the flow of program execution based on certain conditions. These directives are closely related to conditional jump and branch instructions and are used to change the order of execution based on flag states or specific conditions.

    These directives typically work with conditional assembly constructs and flags, enabling the programmer to selectively include or exclude code during assembly, depending on the condition.

    Let's explore the conditional control flow concepts and the relevant directives in MASM:


    1. Conditional Assembly Directives in MASM

    MASM provides several conditional assembly directives that allow you to control the inclusion of code during assembly time. These directives work based on evaluation of conditions at the assembly level, not runtime. They are commonly used to manage different code paths, optimize for specific configurations, or target different platforms.

    Common Conditional Directives

    1. IF Directive

      The IF directive allows you to conditionally include or exclude a block of code based on a condition evaluated during assembly. The condition is usually a comparison of constants or expressions.

      Syntax:

      IF condition
          ; code block to include if condition is true
      ENDIF
      

      Example:

      .IF DEBUG
          ; Code for debugging (only included if DEBUG is defined)
          mov ah, 09h
          lea dx, msg
          int 21h
      .ENDIF
      

      In this example, the block of code will only be included in the assembly if the condition DEBUG is true or defined. If DEBUG is undefined, this block is ignored.

    2. ELSE Directive

      The ELSE directive provides an alternative block of code that is assembled when the IF condition is false. It's used in combination with IF.

      Syntax:

      IF condition
          ; code block if condition is true
      ELSE
          ; code block if condition is false
      ENDIF
      

      Example:

      .IF DEBUG
          ; Debugging code
          mov ah, 09h
          lea dx, msg
          int 21h
      .ELSE
          ; Release code
          mov ah, 4Ch
          int 21h
      .ENDIF
      
    3. ELSEIF Directive

      The ELSEIF directive allows multiple conditions to be tested in sequence. It works similarly to ELSE, but with an additional conditional check before the ELSE block.

      Syntax:

      IF condition1
          ; code block if condition1 is true
      ELSEIF condition2
          ; code block if condition2 is true
      ELSE
          ; code block if all previous conditions are false
      ENDIF
      

      Example:

      .IF DEBUG
          ; Debugging code
          mov ah, 09h
          lea dx, msg
          int 21h
      .ELSEIF RELEASE
          ; Release code
          mov ah, 4Ch
          int 21h
      .ELSE
          ; Default case code
          ; Maybe show an error message or something else
          mov ah, 09h
          lea dx, errorMsg
          int 21h
      .ENDIF
      

      In this case, DEBUG code is assembled if DEBUG is defined, RELEASE code if RELEASE is defined, and an error message if neither condition is met.

    4. WHILE Directive

      The WHILE directive allows a block of code to be included in assembly if a specific condition is true. This directive allows for repeating code based on a condition being met, which can be useful for loops or repetitive sections.

      Syntax:

      WHILE condition
          ; block of code to include while condition is true
      ENDW
      

      Example:

      .WHILE COUNT > 0
          ; Some code that gets assembled while COUNT is greater than 0
          dec COUNT
      .ENDW
      

      This assembly code will keep including the block as long as the COUNT variable is greater than 0.


    2. Using Conditional Jumps (Control Flow)

    While the previous directives control the flow at the assembly-time level, MASM (and assembly languages in general) also allows conditional jumps that control the runtime execution flow based on processor flags or values in registers. These jumps are commonly used for implementing loops, if-else constructs, and branching logic.

    Common Conditional Jump Instructions

    These jump instructions alter the flow of execution at runtime, based on the state of certain flags, such as Zero Flag (ZF), Sign Flag (SF), Carry Flag (CF), etc.

    1. JE / JZ (Jump if Equal / Jump if Zero)

      Jumps if the Zero Flag (ZF) is set, which typically indicates equality (result of a comparison or subtraction is zero).

      Example:

      cmp ax, bx     ; Compare AX with BX
      je  EqualLabel ; Jump to EqualLabel if AX == BX
      
    2. JNE / JNZ (Jump if Not Equal / Jump if Not Zero)

      Jumps if the Zero Flag (ZF) is clear, meaning the values are not equal.

      Example:

      cmp ax, bx     ; Compare AX with BX
      jne NotEqualLabel ; Jump if AX != BX
      
    3. JG / JNLE (Jump if Greater / Jump if Not Less or Equal)

      Jumps if the signed comparison indicates that the first value is greater than the second.

      Example:

      cmp ax, bx
      jg GreaterLabel ; Jump if AX > BX (signed comparison)
      
    4. JL / JNGE (Jump if Less / Jump if Not Greater or Equal)

      Jumps if the signed comparison indicates that the first value is less than the second.

      Example:

      cmp ax, bx
      jl LessLabel    ; Jump if AX < BX (signed comparison)
      
    5. JGE / JNL (Jump if Greater or Equal / Jump if Not Less)

      Jumps if the signed comparison indicates that the first value is greater than or equal to the second.

      Example:

      cmp ax, bx
      jge GreaterOrEqualLabel ; Jump if AX >= BX
      
    6. JC / JNC (Jump if Carry / Jump if No Carry)

      Jumps if the Carry Flag (CF) is set (for unsigned comparisons) or clear.

      Example:

      cmp ax, bx
      jc CarrySetLabel   ; Jump if carry flag is set (AX < BX unsigned)
      
    7. JZ / JNZ (Jump if Zero / Jump if Not Zero)

      These jumps are used for checking if two values are equal or not.

      Example:

      cmp ax, bx
      jz EqualValue      ; Jump if AX == BX (Zero Flag is set)
      jnz NotEqualValue  ; Jump if AX != BX (Zero Flag is clear)
      

    3. Using Conditional Directives for Debugging

    The conditional control flow directives are particularly useful during debugging or when creating software that must run in different environments or with different configurations.

    For example, if you have code that is only useful for debugging or logging, you can wrap it in conditional directives:

    .IF DEBUG
        ; Debugging code
        lea dx, debugMessage
        mov ah, 09h
        int 21h
    .ELSE
        ; Regular code
        lea dx, normalMessage
        mov ah, 09h
        int 21h
    .ENDIF
    

    In this example, if the DEBUG symbol is defined during assembly, the code for debugging (e.g., printing a debug message) is included. Otherwise, the regular code is included.


    4. Summary

    • Conditional Assembly Directives such as IF, ELSE, WHILE, and ENDIF are used to control what code gets assembled based on certain conditions. They help customize the code for different configurations or environments.
    • Conditional Jumps like JE, JNE, JG, JL, JC, etc., control the flow of execution during runtime based on the results of comparisons and flags.
    • These directives and jumps are fundamental for writing flexible, optimized, and environment-specific assembly code, as well as for managing debugging and error-handling code paths.

    Conditional control flow allows assembly programmers to write more adaptable code, enabling different execution paths based on values or compilation parameters.

    ← Previous topic 64
    Runtime Stack
    Next topic 66 →
    Compound Expressions

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language