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›Jumps Based on Specific Flags
    Computer Organization and Assembly LanguageTopic 54 of 73

    Jumps Based on Specific Flags

    7 minread
    1,244words
    Intermediatelevel

    Jumps Based on Specific Flags in Assembly Language

    In assembly language, conditional jumps allow you to alter the flow of a program depending on the state of certain processor flags. These flags are set by various arithmetic and logical instructions (such as CMP, ADD, SUB, etc.) and represent the results of the most recent operation. Depending on the condition, you can use different jump instructions to control the program’s execution flow.

    Condition Flags

    The most commonly used flags for conditional jumps are part of the EFLAGS register (in x86 and x86-64 systems). These flags reflect the result of the most recent arithmetic or logical operation.

    Here are the key flags:

    • ZF (Zero Flag): Set if the result of the operation is zero.
    • SF (Sign Flag): Set if the result is negative (most significant bit is 1).
    • OF (Overflow Flag): Set if the result of a signed operation is too large to fit in the destination.
    • CF (Carry Flag): Set if there was a carry out of the most significant bit in an unsigned operation (used in addition, subtraction, and shifts).
    • PF (Parity Flag): Set if the number of set bits in the result is even.
    • AF (Auxiliary Carry Flag): Set if there is a carry from bit 3 to bit 4 in BCD (Binary Coded Decimal) arithmetic operations.

    These flags are automatically set by operations like ADD, SUB, CMP, MUL, DIV, etc. Conditional jumps use these flags to make decisions based on the outcome of the last operation.


    Types of Conditional Jumps

    In assembly, the conditional jump instructions typically test a specific flag, and if the condition is met, they jump to the specified label. Here’s a list of common jump instructions based on specific flags:

    1. Jump if Zero (ZF = 1)

    • JE (Jump if Equal) / JZ (Jump if Zero):

      • Jumps if the Zero Flag (ZF) is set (i.e., the result of the previous operation was zero).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JE  equal     ; Jump to "equal" if AX == BX (ZF = 1)
      ; Other code
      

      This instruction is used after a CMP (compare) instruction to check if two values are equal.

    2. Jump if Not Zero (ZF = 0)

    • JNE (Jump if Not Equal) / JNZ (Jump if Not Zero):

      • Jumps if the Zero Flag (ZF) is clear (i.e., the result of the previous operation was non-zero).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JNE not_equal ; Jump to "not_equal" if AX != BX (ZF = 0)
      ; Other code
      

      This instruction is used to jump when two values are not equal.

    3. Jump if Carry (CF = 1)

    • JC (Jump if Carry):

      • Jumps if the Carry Flag (CF) is set. This happens in the case of an unsigned overflow or when there’s a carry-out during addition or a borrow during subtraction.
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JC  carry     ; Jump if carry (CF = 1)
      
    • JNC (Jump if No Carry):

      • Jumps if the Carry Flag (CF) is clear, i.e., there was no carry in the previous operation.
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JNC no_carry  ; Jump if no carry (CF = 0)
      

      These instructions are typically used when performing unsigned arithmetic operations.

    4. Jump if Sign (SF = 1)

    • JS (Jump if Sign):

      • Jumps if the Sign Flag (SF) is set (i.e., the result of the previous operation was negative).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JS  negative  ; Jump if the result was negative (SF = 1)
      
    • JNS (Jump if No Sign):

      • Jumps if the Sign Flag (SF) is clear (i.e., the result of the previous operation was non-negative).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JNS positive  ; Jump if the result was positive (SF = 0)
      

      These instructions are used in signed arithmetic operations to test whether the result is positive or negative.

    5. Jump if Overflow (OF = 1)

    • JO (Jump if Overflow):

      • Jumps if the Overflow Flag (OF) is set (i.e., a signed overflow occurred during the previous operation).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JO  overflow  ; Jump if overflow (OF = 1)
      
    • JNO (Jump if No Overflow):

      • Jumps if the Overflow Flag (OF) is clear (i.e., no overflow occurred).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JNO no_overflow ; Jump if no overflow (OF = 0)
      

      These instructions are typically used for signed arithmetic operations to handle overflows.

    6. Jump if Parity (PF = 1)

    • JP (Jump if Parity):

      • Jumps if the Parity Flag (PF) is set (i.e., the number of set bits in the result is even).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JP  even_parity ; Jump if parity is even (PF = 1)
      
    • JNP (Jump if No Parity):

      • Jumps if the Parity Flag (PF) is clear (i.e., the number of set bits in the result is odd).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JNP no_parity  ; Jump if parity is odd (PF = 0)
      

      Parity is often used in error checking and communication protocols.

    7. Jump if Less (SF ≠ OF)

    • JL (Jump if Less):

      • Jumps if the result of the previous operation was less, based on signed comparison (i.e., SF does not equal OF).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JL  less      ; Jump if AX < BX (signed comparison)
      
    • JGE (Jump if Greater or Equal):

      • Jumps if the result of the previous operation was greater or equal (i.e., SF equals OF).
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JGE greater_or_equal ; Jump if AX >= BX (signed comparison)
      

      These jumps are commonly used for signed comparisons between two values.

    8. Jump if Below (CF = 1)

    • JB (Jump if Below):

      • Jumps if the Carry Flag (CF) is set, i.e., in unsigned comparisons, if the first operand is less than the second.
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JB  below     ; Jump if AX < BX (unsigned comparison)
      
    • JAE (Jump if Above or Equal):

      • Jumps if the Carry Flag (CF) is clear, i.e., if the first operand is greater than or equal to the second in an unsigned comparison.
      CMP AX, BX    ; Compare AX and BX (sets the flags)
      JAE above_or_equal ; Jump if AX >= BX (unsigned comparison)
      

    Example Program Using Jumps Based on Flags

    section .data
        num1 dw 10
        num2 dw 5
    
    section .text
        MOV AX, [num1]   ; Load AX with num1 (10)
        MOV BX, [num2]   ; Load BX with num2 (5)
        CMP AX, BX       ; Compare AX and BX (sets flags)
    
        JE  equal        ; Jump to "equal" if AX == BX (ZF = 1)
        JNE not_equal    ; Jump to "not_equal" if AX != BX (ZF = 0)
        JC  carry        ; Jump to "carry" if there was a carry (CF = 1)
        JNC no_carry     ; Jump to "no_carry" if no carry (CF = 0)
        JS  negative     ; Jump to "negative" if result was negative (SF = 1)
        JNS positive     ; Jump to "positive" if result was positive (SF = 0)
        
    equal:
        ; Code for when AX == BX
        MOV DX,
    
    Previous topic 53
    Division and Multiplication in Assembly
    Next topic 55
    Jumps Based on Equality

    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,244
      Code examples0
      DifficultyIntermediate