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 PortalCOMP3137Labels Procedures
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
    8 min read
    Topic 62 of 73
    Topic Study Note Illustration
    62

    Labels in Procedures

    In assembly language, labels are used to mark locations in the code that can be referenced by jump instructions, such as jmp , jne , je ,...

    Using Labels in Procedures in MASM

    In assembly language, labels are used to mark locations in the code that can be referenced by jump instructions, such as jmp, jne, je, call, or ret. In MASM (Microsoft Macro Assembler), labels are used to identify where a procedure starts, where specific instructions or blocks of code are located, and where to jump during the program's execution.

    In this explanation, I'll cover how labels work within procedures, how they can be used for conditional jumps, and demonstrate their functionality with examples.


    1. Labels in MASM Procedures

    A label in MASM is essentially a symbolic name that marks a specific address in the program. The label is followed by a colon (:), and it can be placed before any instruction or block of code. These labels allow you to reference locations in the code for things like loops, branching, or function calls.

    Example 1: Basic Labels in Procedures

    ; label_in_procedure.asm
    .model small
    .stack 100h
    
    .data
        msg db 'Hello, MASM!$', 0
    
    .code
    main:
        mov ax, @data      ; Initialize data segment
        mov ds, ax
    
        ; Call the PrintMessage procedure
        call PrintMessage
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to print a message
    PrintMessage proc
        ; Start of label
        start_printing:
            mov ah, 09h         ; DOS function to print a string
            lea dx, msg         ; Load address of msg into DX
            int 21h             ; DOS interrupt to print the string
            ret                 ; Return from procedure
        ; End of procedure
    PrintMessage endp
    
    end main
    

    Explanation:

    • The label start_printing is placed before the instruction mov ah, 09h. This label is not strictly necessary for this simple example, but it could be used in more complex scenarios (e.g., conditional jumps, loops).
    • The call PrintMessage instruction jumps to the PrintMessage procedure.
    • The ret instruction marks the end of the procedure, and control is returned to the calling program (main).

    2. Labels with Conditional Jumps

    In MASM, labels are often used in conjunction with conditional jump instructions (like jne, je, jg, jl, jmp, etc.) to alter the flow of control. This is useful when you want to perform different operations based on certain conditions within a procedure.

    Example 2: Using Labels with Conditional Jumps

    ; conditional_jump_labels.asm
    .model small
    .stack 100h
    
    .data
        num1 dw 5           ; First number
        num2 dw 10          ; Second number
        result db 'Result is less than or equal to 10$', 0
        greater_result db 'Result is greater than 10$', 0
    
    .code
    main:
        mov ax, @data       ; Initialize data segment
        mov ds, ax
    
        ; Compare num1 and num2, and jump based on the result
        call CompareNumbers
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to compare two numbers
    CompareNumbers proc
        ; Compare num1 and num2
        mov ax, num1        ; Load num1 into AX
        cmp ax, num2        ; Compare AX (num1) with num2
    
        ; Jump based on comparison result
        jg  greater         ; If AX > num2, jump to 'greater' label
        jl  less            ; If AX < num2, jump to 'less' label
    
    equal:
        ; This block is executed if num1 == num2
        lea dx, result      ; Load address of result message
        mov ah, 09h         ; DOS function to print string
        int 21h
        ret
    
    greater:
        ; This block is executed if num1 > num2
        lea dx, greater_result
        mov ah, 09h
        int 21h
        ret
    
    less:
        ; This block is executed if num1 < num2
        lea dx, result
        mov ah, 09h
        int 21h
        ret
    
    CompareNumbers endp
    
    end main
    

    Explanation:

    • cmp ax, num2: Compares num1 (in AX) with num2.
    • jg greater: If AX (the value of num1) is greater than num2, the control jumps to the greater label.
    • jl less: If AX is less than num2, the control jumps to the less label.
    • The equal label is reached if num1 equals num2.
    • The different messages (greater_result or result) are displayed based on the comparison.

    3. Labels in Loops

    Labels are also very useful for implementing loops, as they allow you to jump back to the beginning of a block of code. You can use labels with jmp or conditional jumps to create repetitive processes.

    Example 3: Using Labels in a Loop

    ; loop_with_labels.asm
    .model small
    .stack 100h
    
    .data
        msg db 'Counting down: $', 0
        num dw 5               ; Starting number
    
    .code
    main:
        mov ax, @data         ; Initialize data segment
        mov ds, ax
    
        ; Print the message
        lea dx, msg           ; Load address of msg
        mov ah, 09h           ; DOS function to print string
        int 21h
    
        ; Call the countdown procedure
        call Countdown
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to perform countdown
    Countdown proc
        mov ax, num           ; Load starting number
    
    count_loop:
        ; Print the current value of AX (num)
        ; You would convert AX to ASCII and print here, but for simplicity, we assume it's done
        ; This could be extended to display each value of num in the message.
    
        dec ax                 ; Decrement AX (count down)
        cmp ax, 0              ; Compare AX to 0
        jg  count_loop         ; Jump to count_loop if AX > 0
    
        ret                    ; Return from procedure
    
    Countdown endp
    
    end main
    

    Explanation:

    • The count_loop label marks the beginning of the loop.
    • The dec ax instruction decrements the value of AX (which starts at num).
    • The cmp ax, 0 instruction checks if AX has reached 0.
    • jg count_loop causes the program to jump back to the count_loop label if AX is still greater than 0.
    • When the loop finishes, the ret instruction returns from the Countdown procedure.

    4. Using Labels for Function Calls and Returns

    Labels are integral in defining where procedures start and end. They can also be used to ensure that the program continues after a function call (via ret).

    Example 4: Label Usage for Function Calls

    ; function_call_labels.asm
    .model small
    .stack 100h
    
    .data
        msg db 'This is a simple function call.$', 0
    
    .code
    main:
        mov ax, @data       ; Initialize data segment
        mov ds, ax
    
        ; Print the initial message
        lea dx, msg         ; Load the address of the message
        mov ah, 09h         ; DOS function to print string
        int 21h             ; Call DOS interrupt
    
        ; Call the MyFunction procedure
        call MyFunction
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to print a message
    MyFunction proc
        lea dx, msg         ; Load address of msg into DX
        mov ah, 09h         ; DOS function to print string
        int 21h             ; Print the string
        ret                 ; Return to main
    MyFunction endp
    
    end main
    

    Explanation:

    • MyFunction proc defines the procedure starting with the MyFunction label.
    • ret ensures that the program returns control to the calling function (in this case, the main procedure).

    5. Label Considerations

    • Scope: Labels in MASM are local to the procedure or block where they are defined. They are not accessible outside of their respective procedures.
    • Clarity: Using meaningful labels can significantly improve code readability. Instead of using generic labels like loop1, consider names that describe the purpose of the code at that location, such as start_loop or end_of_processing.
    • Control Flow: Labels control the flow of execution by being targets for jump or call instructions. Ensure that you use them in logical places where jumps make sense, such as loops or error-handling sections.

    Summary

    • Labels are symbolic markers for locations in the code that can be referenced by jump instructions like jmp, jne, jg, jl, call, and ret.
    • Labels help organize code into manageable sections and are used extensively in loops, conditional branches, and function calls.
    • MASM supports labels for modularizing code in procedures, helping you implement complex control flow structures effectively.
    ← Previous topic 61
    File Operations Procedures
    Next topic 63 →
    Stack

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    8mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language