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 PortalCOMP3137Simple Jump Statements
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 56 of 73
    Topic Study Note Illustration
    56

    Simple Jump Statements

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

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

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    5mremaining
    792words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language