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 PortalCOMP3137Code Examples
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 58 of 73
    Topic Study Note Illustration
    58

    Code Examples

    Here are some practical examples demonstrating conditional jumps in assembly. We'll use the x86 assembly language with MASM-style syntax to show how these instructions work. These examples...

    Code Examples of Conditional Jumps in Assembly Language

    Here are some practical examples demonstrating conditional jumps in assembly. We'll use the x86 assembly language with MASM-style syntax to show how these instructions work. These examples assume you are using a 16-bit or 32-bit x86 environment, but the concepts apply to both.


    Example 1: Jump if Equal (JE or JZ)

    This example shows how to jump if two values are equal. The CMP instruction compares two values, and if they are equal, the Zero Flag (ZF) is set, causing the program to jump to the equal label.

    section .data
        msg db 'Equal', 0
        msg_not_equal db 'Not Equal', 0
    
    section .text
        ; Assume AX = 5, BX = 5
        MOV AX, 5
        MOV BX, 5
    
        CMP AX, BX        ; Compare AX with BX
        JE equal          ; Jump to "equal" if AX == BX (ZF = 1)
    
        ; If not equal
        MOV DX, msg_not_equal
        RET
    
    equal:
        ; If equal
        MOV DX, msg
        RET
    

    Explanation:

    • MOV AX, 5 and MOV BX, 5 load both registers with the value 5.
    • CMP AX, BX compares AX with BX. Since the values are equal, the Zero Flag (ZF) will be set to 1.
    • JE equal will jump to the equal label because ZF = 1 (indicating the two values are equal).

    Example 2: Jump if Not Equal (JNE or JNZ)

    This example shows how to jump if two values are not equal. The CMP instruction compares the two values, and if they are not equal, the Zero Flag (ZF) is cleared, causing the program to jump to the not_equal label.

    section .data
        msg db 'Equal', 0
        msg_not_equal db 'Not Equal', 0
    
    section .text
        ; Assume AX = 5, BX = 10
        MOV AX, 5
        MOV BX, 10
    
        CMP AX, BX        ; Compare AX with BX
        JNE not_equal     ; Jump to "not_equal" if AX != BX (ZF = 0)
    
        ; If equal
        MOV DX, msg
        RET
    
    not_equal:
        ; If not equal
        MOV DX, msg_not_equal
        RET
    

    Explanation:

    • MOV AX, 5 and MOV BX, 10 load AX with 5 and BX with 10.
    • CMP AX, BX compares AX with BX. Since the values are different, the Zero Flag (ZF) is cleared (ZF = 0).
    • JNE not_equal will jump to the not_equal label because ZF = 0 (indicating the values are not equal).

    Example 3: Jump if Carry (JC)

    This example demonstrates jumping if there was a carry. A carry can occur after an addition or subtraction that results in an overflow, but for simplicity, we'll use a subtraction.

    section .data
        msg db 'Carry Detected', 0
        msg_no_carry db 'No Carry', 0
    
    section .text
        ; Assume AX = 10, BX = 20
        MOV AX, 10
        MOV BX, 20
    
        CMP AX, BX          ; Compare AX and BX
        JC carry_detected   ; Jump to "carry_detected" if carry (CF = 1)
    
        ; No carry
        MOV DX, msg_no_carry
        RET
    
    carry_detected:
        ; Carry occurred
        MOV DX, msg
        RET
    

    Explanation:

    • MOV AX, 10 and MOV BX, 20 load AX and BX with values 10 and 20, respectively.
    • CMP AX, BX compares AX with BX. Since AX < BX, there will be a borrow (carry in unsigned terms).
    • JC carry_detected will jump to carry_detected because the Carry Flag (CF) is set after the comparison.

    Example 4: Jump if Overflow (JO)

    This example demonstrates how to jump if there is an overflow. Overflow occurs in signed arithmetic operations when the result exceeds the maximum value that can be represented.

    section .data
        msg db 'Overflow Detected', 0
        msg_no_overflow db 'No Overflow', 0
    
    section .text
        ; Assume AX = 32767, BX = 1 (Overflow in signed addition)
        MOV AX, 32767      ; Largest positive signed 16-bit value
        MOV BX, 1          ; Small value to cause overflow
    
        ADD AX, BX         ; AX = AX + BX
        JO overflow_detected ; Jump to "overflow_detected" if overflow (OF = 1)
    
        ; No overflow
        MOV DX, msg_no_overflow
        RET
    
    overflow_detected:
        ; Overflow detected
        MOV DX, msg
        RET
    

    Explanation:

    • MOV AX, 32767 and MOV BX, 1 load AX with the largest positive signed 16-bit value (32767) and BX with 1.
    • ADD AX, BX adds AX and BX, resulting in 32768, which exceeds the 16-bit signed integer limit.
    • JO overflow_detected will jump to the overflow_detected label because the Overflow Flag (OF) is set after the addition.

    Example 5: Jump if Negative (JS)

    This example shows how to jump if the result of a signed comparison is negative, based on the Sign Flag (SF).

    section .data
        msg_negative db 'Negative', 0
        msg_non_negative db 'Non-negative', 0
    
    section .text
        ; Assume AX = -5, BX = 5
        MOV AX, -5
        MOV BX, 5
    
        CMP AX, BX          ; Compare AX and BX
        JS negative         ; Jump to "negative" if AX < BX (SF = 1)
    
        ; Non-negative
        MOV DX, msg_non_negative
        RET
    
    negative:
        ; Negative
        MOV DX, msg_negative
        RET
    

    Explanation:

    • MOV AX, -5 and MOV BX, 5 load AX with -5 and BX with 5.
    • CMP AX, BX compares AX and BX. Since AX < BX in signed terms, the Sign Flag (SF) will be set (SF = 1).
    • JS negative will jump to the negative label because SF = 1 indicates a negative result.

    Example 6: Jump if No Sign (JNS)

    This example demonstrates how to jump if the result of a signed operation is non-negative, i.e., if the Sign Flag (SF) is clear.

    section .data
        msg_positive_or_zero db 'Positive or Zero', 0
        msg_negative db 'Negative', 0
    
    section .text
        ; Assume AX = -5, BX = 5
        MOV AX, -5
        MOV BX, 5
    
        CMP AX, BX          ; Compare AX and BX
        JNS positive_or_zero ; Jump to "positive_or_zero" if AX >= BX (SF = 0)
    
        ; Negative
        MOV DX, msg_negative
        RET
    
    positive_or_zero:
        ; Non-negative or Zero
        MOV DX, msg_positive_or_zero
        RET
    

    Explanation:

    • MOV AX, -5 and MOV BX, 5 load AX with -5 and BX with 5.
    • CMP AX, BX compares AX and BX. Since AX is less than BX, the Sign Flag (SF) is set (SF = 1), meaning it's a negative result.
    • JNS positive_or_zero will jump to the positive_or_zero label if the result is non-negative (SF = 0).

    Summary of 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)
    ← Previous topic 57
    Jumps Based on Specific Condition
    Next topic 59 →
    Practice on MASM

    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.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language