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›Labels in Procedures
    Computer Organization and Assembly LanguageTopic 62 of 73

    Labels in Procedures

    8 minread
    1,279words
    Intermediatelevel

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

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