ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Computer Organization and Assembly Language
    COMP3137
    Progress0 / 73 topics
    Topics
    1. Introduction to Computer Organization2. Assembly Language3. Comparison of Low-Level and High-Level Languages4. Register Types (16-bit): General Purpose and Special Purpose Registers5. Introduction and Usage of RAM6. Processor7. Registers8. System Bus9. Instruction Execution Cycle10. Assembly and Machine Language11. Assembler12. Linker and Link Libraries13. Programmer's View of a Computer System14. RISC and CISC Architecture15. Physical Address Calculation16. Basic Memory Organization17. CPU Organization18. Top Level View of Computer Function and Interconnection19. Assembler Instruction Cycle20. Execute Cycle21. Interrupts22. Interrupt Cycle23. Memory Connection24. Input/Output Connection25. CPU Connection26. MASM27. MIPS28. Defining Data in MASM Assembler29. Elements of Assembly Language30. Integer Constants31. Integer Expressions32. Real Number Constants33. Character Constants34. String Constants35. Reserved Words36. Identifiers37. Directives38. Instructions39. The NOP (No Operation) Instruction40. Adding and Subtracting Integer41. INC and DEC Instructions42. NEG Instruction43. How to Move Integer Number in Register44. Adding and Subtracting Numbers in Registers45. Declaration and Initialization of Variables46. Moving Data from Variable to Register47. Data Definition Statement48. BYTE and SBYTE Data49. WORD and SWORD Data50. Defining DWORD and SDWORD Data51. Knowledge about Different Data Types52. Operations, Array & Loops53. Division and Multiplication in Assembly54. Jumps Based on Specific Flags55. Jumps Based on Equality56. Simple Jump Statements57. Jumps Based on Specific Condition58. Code Examples59. Practice on MASM60. Procedures61. File Operations Procedures62. Labels in Procedures63. Stack64. Runtime Stack65. Conditional Control Flow Directives66. Compound Expressions67. Data Representation & Conversion68. Architecture69. Data Path70. Control Unit71. Critical Path72. General Principles of Pipelining73. Pipelined Y86 Implementations
    COMP3137›Conditional Control Flow Directives
    Computer Organization and Assembly LanguageTopic 65 of 73

    Conditional Control Flow Directives

    7 minread
    1,182words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,182
      Code examples0
      DifficultyIntermediate