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›Simple Jump Statements
    Computer Organization and Assembly LanguageTopic 56 of 73

    Simple Jump Statements

    5 minread
    792words
    Beginnerlevel

    Simple Jump Statements in Assembly Language

    In assembly language, jump instructions are used to change the flow of execution in a program. Jumps can be conditional (based on flags set by previous operations like CMP, ADD, SUB, etc.) or unconditional. Unconditional jumps are straightforward; they always result in the program jumping to a specified location or label.

    Unconditional Jump: JMP

    The simplest type of jump is the unconditional jump. The JMP instruction transfers control to another part of the program regardless of any conditions or flags. This is useful when you need to skip over certain instructions or create loops.

    Syntax:

    JMP label  ; Jump to the label unconditionally
    

    Where label is a target location in the program. The program will jump directly to that point in the code.

    Example:

    section .data
        msg db 'This will be printed after the jump.'
    
    section .text
        ; Some code here
        JMP skip_code   ; Jump unconditionally to skip_code label
    
        ; This code will be skipped
        MOV DX, 'This code is skipped'
    
    skip_code:
        ; This code will be executed
        MOV DX, msg      ; Print message after jump
        RET
    

    In this example:

    • The JMP skip_code instruction unconditionally jumps to the label skip_code.
    • The code after JMP (MOV DX, 'This code is skipped') will be skipped.
    • The program then executes the code at skip_code, which displays the message.

    Short Jump vs. Near Jump

    In assembly language, jumps are generally categorized into short and near jumps, depending on the range of the target address.

    1. Short Jump:

      • A short jump is used for relatively small distances (usually within a range of -128 to +127 bytes from the current position).
      • It’s used when the jump target is nearby, typically within the same function or block of code.
    2. Near Jump:

      • A near jump can be used for larger distances, spanning across different sections of code or even different segments.
      • It can be used to jump to locations that are farther away in memory.

    For simple jumps in small programs or loops, short jumps are usually enough. Near jumps are used when a jump target is too far away for a short jump.


    Example of JMP (Unconditional Jump) in a Loop

    Unconditional jumps are especially useful for creating loops. A common use case is jumping back to the beginning of the loop until a certain condition is met.

    Example of a Simple Loop Using JMP:

    section .data
        counter db 0   ; Initialize counter to 0
    
    section .text
        MOV AL, [counter]   ; Load the counter value into AL register
    
    start_loop:
        CMP AL, 5           ; Compare counter value with 5
        JE  end_loop        ; If AL == 5, jump to end_loop
        INC AL              ; Increment counter
        MOV [counter], AL   ; Store updated counter value
        JMP start_loop      ; Jump to the start of the loop
    
    end_loop:
        ; Code here runs after loop ends
        MOV DX, 'Loop Finished'
        RET
    

    In this example:

    • The program starts at start_loop and keeps looping, incrementing the counter and checking if it's equal to 5.
    • The JMP start_loop instruction causes the program to jump back to the start of the loop until the counter reaches 5.
    • Once the condition AL == 5 is met, the program jumps to the end_loop label.

    Jump to a Specific Location (Label)

    Another type of simple jump is when you need to jump directly to a specific location or label within the program, often used for error handling or skipping over unnecessary code.

    Example: Jump to a Label for Error Handling

    section .data
        msg_error db 'Error occurred!', 0
    
    section .text
        ; Some initialization code
        MOV AX, 5          ; Load AX with a value
    
        CMP AX, 10         ; Compare AX with 10
        JGE  handle_error  ; If AX >= 10, jump to handle_error
    
        ; Normal program flow continues
        MOV DX, 'Normal execution'
        RET
    
    handle_error:
        ; Error handling code
        MOV DX, msg_error  ; Display error message
        RET
    

    In this example:

    • If AX >= 10, the program jumps to the handle_error label using JGE handle_error, where it displays an error message.
    • If the condition is not met, the program continues with the normal flow.

    Summary of Simple Jump Statements

    • JMP (Unconditional Jump): Jumps to a specified label or location in the program unconditionally, i.e., without any condition.
    • Short Jumps: These are used for smaller jump distances, usually within a function or block of code.
    • Near Jumps: Used when the jump target is farther away or across different segments of memory.
    • Usage: Unconditional jumps are widely used for:
      • Skipping over code (e.g., skipping error handling if no error occurred).
      • Creating loops by jumping back to the start of a loop.
      • Directing program flow to specific locations like error handlers or special routines.
    Previous topic 55
    Jumps Based on Equality
    Next topic 57
    Jumps Based on Specific Condition

    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 count792
      Code examples0
      DifficultyBeginner