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›Code Examples
    Computer Organization and Assembly LanguageTopic 58 of 73

    Code Examples

    7 minread
    1,137words
    Intermediatelevel

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

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