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 Equality
    Computer Organization and Assembly LanguageTopic 55 of 73

    Jumps Based on Equality

    5 minread
    911words
    Intermediatelevel

    Jumps Based on Equality in Assembly Language

    In assembly, jumps based on equality are used to alter the flow of the program when two values are equal or not equal. These types of jumps are commonly used after performing a comparison (CMP) between two values.

    When you perform a comparison (CMP), it subtracts one operand from another but does not store the result—rather, it updates the flags (Zero Flag (ZF), Carry Flag (CF), Sign Flag (SF), etc.) based on the result of the subtraction. The Zero Flag (ZF) is particularly important when checking for equality.

    Key Conditional Jumps for Equality

    1. JE (Jump if Equal):

      • Jumps if the Zero Flag (ZF) is set, which indicates that the two compared values are equal.
      • Equivalent to JZ (Jump if Zero), because both check the Zero Flag (ZF).
    2. JNE (Jump if Not Equal):

      • Jumps if the Zero Flag (ZF) is clear, which indicates that the two compared values are not equal.
      • Equivalent to JNZ (Jump if Not Zero), as both check if the Zero Flag (ZF) is not set.

    How Conditional Jumps Work

    • CMP Instruction: This is a typical instruction used to compare two values, and it works by subtracting the second operand from the first operand. It updates the flags in the EFLAGS register based on the result.
      • ZF = 1 if the result of the subtraction is zero (i.e., the two values are equal).
      • ZF = 0 if the result is non-zero (i.e., the values are not equal).

    After using CMP, you can use JE or JNE to jump based on whether the values are equal or not equal.


    1. Jump if Equal (JE or JZ)

    The JE (Jump if Equal) instruction is used when you want to jump to a label if the two values compared are equal. It checks the Zero Flag (ZF) to determine if the comparison resulted in equality.

    Example: Using JE

    section .data
        num1 dw 10
        num2 dw 10
    
    section .text
        MOV AX, [num1]  ; Load AX with num1 (10)
        MOV BX, [num2]  ; Load BX with num2 (10)
        CMP AX, BX      ; Compare AX and BX (sets flags)
    
        JE  values_equal ; Jump to "values_equal" if AX == BX (ZF = 1)
    
        ; Code here will execute if AX != BX (ZF = 0)
        ; For example, output something indicating they are not equal
        MOV DX, 'Not Equal'
    
        ; Continue with program
        RET
    
    values_equal:
        ; Code here will execute if AX == BX (ZF = 1)
        ; For example, output something indicating they are equal
        MOV DX, 'Equal'
        RET
    

    In this example, the CMP instruction compares the values in AX and BX. If they are equal, the Zero Flag (ZF) will be set, and the program will jump to the values_equal label.


    2. Jump if Not Equal (JNE or JNZ)

    The JNE (Jump if Not Equal) instruction is used when you want to jump to a label if the two values compared are not equal. It checks if the Zero Flag (ZF) is not set, indicating that the comparison resulted in a non-zero difference.

    Example: Using JNE

    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)
    
        JNE values_not_equal ; Jump to "values_not_equal" if AX != BX (ZF = 0)
    
        ; Code here will execute if AX == BX (ZF = 1)
        MOV DX, 'Equal'
    
        ; Continue with program
        RET
    
    values_not_equal:
        ; Code here will execute if AX != BX (ZF = 0)
        MOV DX, 'Not Equal'
        RET
    

    In this example, the CMP instruction compares AX and BX. Since they are not equal (10 != 5), the Zero Flag (ZF) will be clear, and the program will jump to the values_not_equal label.


    Understanding the CMP Instruction

    The CMP instruction subtracts the second operand from the first and updates the processor flags based on the result of the subtraction. Importantly, CMP does not store the result, it only modifies the flags.

    Key Flags Affected by CMP:

    • Zero Flag (ZF): Set if the result is zero (i.e., the two values are equal).
    • Carry Flag (CF): Affects unsigned comparisons. Set if there is a borrow in subtraction.
    • Sign Flag (SF): Set if the result is negative (used for signed comparisons).
    • Overflow Flag (OF): Affects signed arithmetic overflows.

    Example of CMP Instruction Behavior:

    CMP AX, BX  ; AX - BX (set flags)
    
    • If AX == BX, then ZF = 1 (zero result).
    • If AX > BX, then ZF = 0 and SF = 0 (sign result).
    • If AX < BX, then ZF = 0 and SF = 1 (sign result).

    Summary of Equality Jumps

    • JE (Jump if Equal): Jumps if Zero Flag (ZF) = 1 (i.e., the values are equal).

      • Equivalent: JZ (Jump if Zero).
    • JNE (Jump if Not Equal): Jumps if Zero Flag (ZF) = 0 (i.e., the values are not equal).

      • Equivalent: JNZ (Jump if Not Zero).

    Example Flow:

    1. Compare two values using CMP.
    2. Check if they are equal using JE (if the Zero Flag is set).
    3. Check if they are not equal using JNE (if the Zero Flag is cleared).

    This mechanism allows for efficient decision-making in assembly language programs based on the outcome of previous operations.

    Previous topic 54
    Jumps Based on Specific Flags
    Next topic 56
    Simple Jump Statements

    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 time5 min
      Word count911
      Code examples0
      DifficultyIntermediate