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 PortalCOMP3137Jumps Specific Flags
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 54 of 73
    Topic Study Note Illustration
    54

    Jumps Based on Specific Flags

    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...

    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 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