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 Condition
    Computer Organization and Assembly LanguageTopic 57 of 73

    Jumps Based on Specific Condition

    7 minread
    1,229words
    Intermediatelevel

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

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