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 Equality
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
    5 min read
    Topic 55 of 73
    Topic Study Note Illustration
    55

    Jumps Based on Equality

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

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    5mremaining
    911words
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language