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 Based Specific C…
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 57 of 73
    Topic Study Note Illustration
    57

    Jumps Based on Specific Condition

    In assembly language, jumps based on a specific condition allow you to alter the flow of a program depending on the result of an operation. These conditional...

    Jumps Based on Specific Condition in Assembly Language

    In assembly language, jumps based on a specific condition allow you to alter the flow of a program depending on the result of an operation. These conditional jumps are typically used after a comparison operation (CMP) or any arithmetic/logical operation that affects the flags in the EFLAGS register. Depending on the state of the flags (such as the Zero Flag (ZF), Carry Flag (CF), Sign Flag (SF), etc.), you can control the program's execution path.

    Overview of Conditional Jump Instructions

    Conditional jumps are instructions that cause a jump to a specific label if a certain condition (based on the processor's flags) is met. The key flags used for these jumps are set by the previous instructions and dictate the flow.

    Here’s a breakdown of common conditional jumps based on specific conditions:


    1. Jump if Zero (JE or JZ)

    • Condition: Jump if the Zero Flag (ZF) is set.
    • Meaning: This happens if the result of the last operation (e.g., CMP, SUB, etc.) was zero. This indicates that two compared values are equal.

    Instruction:

    JE  label   ; Jump if Zero Flag (ZF) is set (i.e., if the values are equal)
    JZ  label   ; Same as JE
    

    Example:

    CMP AX, BX   ; Compare AX and BX
    JE  equal    ; Jump to "equal" if AX == BX (ZF = 1)
    
    MOV DX, 'Not Equal'
    RET
    
    equal:
    MOV DX, 'Equal'
    RET
    

    Here, CMP AX, BX compares the contents of AX and BX. If they are equal, the Zero Flag (ZF) is set, and the program will jump to the equal label.


    2. Jump if Not Zero (JNE or JNZ)

    • Condition: Jump if the Zero Flag (ZF) is clear.
    • Meaning: This happens if the result of the last operation was non-zero. This indicates that two compared values are not equal.

    Instruction:

    JNE label   ; Jump if Zero Flag (ZF) is clear (i.e., if the values are not equal)
    JNZ label   ; Same as JNE
    

    Example:

    CMP AX, BX   ; Compare AX and BX
    JNE not_equal ; Jump to "not_equal" if AX != BX (ZF = 0)
    
    MOV DX, 'Equal'
    RET
    
    not_equal:
    MOV DX, 'Not Equal'
    RET
    

    In this example, the program jumps to not_equal if the values in AX and BX are different (i.e., ZF = 0).


    3. Jump if Carry (JC)

    • Condition: Jump if the Carry Flag (CF) is set.
    • Meaning: This usually occurs after an arithmetic operation like addition or subtraction. The Carry Flag is set if there was an overflow during an unsigned operation (like when adding two large numbers or subtracting with a borrow).

    Instruction:

    JC label   ; Jump if Carry Flag (CF) is set (i.e., there was an unsigned overflow or borrow)
    

    Example:

    CMP AX, BX    ; Compare AX and BX (sets flags)
    JC  carry_set ; Jump to "carry_set" if there was a carry (CF = 1)
    
    MOV DX, 'No Carry'
    RET
    
    carry_set:
    MOV DX, 'Carry Occurred'
    RET
    

    Here, the CMP instruction compares AX and BX, and the jump will happen if there is a carry flag set (i.e., an unsigned overflow or borrow).


    4. Jump if No Carry (JNC)

    • Condition: Jump if the Carry Flag (CF) is clear.
    • Meaning: This happens if there was no carry during the previous arithmetic operation.

    Instruction:

    JNC label   ; Jump if Carry Flag (CF) is clear (i.e., no carry occurred)
    

    Example:

    CMP AX, BX    ; Compare AX and BX (sets flags)
    JNC no_carry  ; Jump to "no_carry" if no carry occurred (CF = 0)
    
    MOV DX, 'Carry Occurred'
    RET
    
    no_carry:
    MOV DX, 'No Carry'
    RET
    

    This example demonstrates how to check if there was no carry after a comparison.


    5. Jump if Positive (JS)

    • Condition: Jump if the Sign Flag (SF) is set.
    • Meaning: The Sign Flag (SF) is set if the result of the previous operation was negative (in a signed operation).

    Instruction:

    JS label   ; Jump if Sign Flag (SF) is set (i.e., the result was negative)
    

    Example:

    CMP AX, BX    ; Compare AX and BX (sets flags)
    JS  negative  ; Jump to "negative" if AX < BX (SF = 1)
    
    MOV DX, 'Positive or Zero'
    RET
    
    negative:
    MOV DX, 'Negative Result'
    RET
    

    Here, the jump to negative occurs if the comparison shows that AX is less than BX (i.e., SF = 1 after a signed comparison).


    6. Jump if No Sign (JNS)

    • Condition: Jump if the Sign Flag (SF) is clear.
    • Meaning: This happens if the result of the previous operation was non-negative (in a signed operation).

    Instruction:

    JNS label   ; Jump if Sign Flag (SF) is clear (i.e., the result was non-negative)
    

    Example:

    CMP AX, BX    ; Compare AX and BX (sets flags)
    JNS positive_or_zero  ; Jump if AX >= BX (SF = 0)
    
    MOV DX, 'Negative Result'
    RET
    
    positive_or_zero:
    MOV DX, 'Positive or Zero'
    RET
    

    This example shows how to check for non-negative results in signed comparisons.


    7. Jump if Overflow (JO)

    • Condition: Jump if the Overflow Flag (OF) is set.
    • Meaning: The Overflow Flag (OF) is set if there was an overflow in a signed arithmetic operation, meaning the result exceeded the representable range for the data type.

    Instruction:

    JO label   ; Jump if Overflow Flag (OF) is set (i.e., signed overflow occurred)
    

    Example:

    CMP AX, BX    ; Compare AX and BX (sets flags)
    JO  overflow  ; Jump to "overflow" if signed overflow occurred (OF = 1)
    
    MOV DX, 'No Overflow'
    RET
    
    overflow:
    MOV DX, 'Overflow Occurred'
    RET
    

    Here, the program jumps to overflow if there was a signed overflow in the comparison.


    8. Jump if No Overflow (JNO)

    • Condition: Jump if the Overflow Flag (OF) is clear.
    • Meaning: This happens if there was no overflow during the last signed arithmetic operation.

    Instruction:

    JNO label   ; Jump if Overflow Flag (OF) is clear (i.e., no signed overflow)
    

    Example:

    CMP AX, BX    ; Compare AX and BX (sets flags)
    JNO no_overflow ; Jump to "no_overflow" if no signed overflow (OF = 0)
    
    MOV DX, 'Overflow Detected'
    RET
    
    no_overflow:
    MOV DX, 'No Overflow'
    RET
    

    In this case, the program jumps to no_overflow if there was no overflow in the signed comparison.


    Summary of Common Conditional Jumps

    Jump Instruction Condition Flag Checked Meaning
    JE / JZ Equal ZF (Zero Flag) Jump if the two values are equal
    JNE / JNZ Not Equal ZF (Zero Flag) Jump if the two values are not equal
    JC Carry CF (Carry Flag) Jump if there was a carry (unsigned)
    JNC No Carry CF (Carry Flag) Jump if there was no carry (unsigned)
    JS Negative SF (Sign Flag) Jump if the result was negative
    JNS Positive or Zero SF (Sign Flag) Jump if the result was non-negative
    JO Overflow OF (Overflow Flag) Jump if there was signed overflow
    JNO No Overflow OF (Overflow Flag) Jump if there was no signed overflow

    Example: Complete Program with Conditional Jumps

    section .data
        msg db 'Comparison Result: ', 0
    
    section .text
        MOV AX, 5
        MOV BX, 10
        CMP AX, BX          ; Compare AX and BX
    
        JE  equal           ; Jump if equal (ZF = 1)
        JNE not_equal       ; Jump if not equal (ZF =
    
    ← Previous topic 56
    Simple Jump Statements
    Next topic 58 →
    Code Examples

    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