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

    Procedures

    7 minread
    1,235words
    Intermediatelevel

    Using Procedures in MASM (Microsoft Macro Assembler)

    In MASM, a procedure (or function) is a block of code that performs a specific task and can be called from different parts of the program. Procedures help to modularize code, making it easier to read, maintain, and reuse.

    Below, I'll cover how to define and use procedures in MASM, including examples of passing parameters, returning values, and calling procedures from the main code.


    1. Basic Procedure Example (No Parameters)

    In this example, we'll define a simple procedure that prints a string message.

    MASM Code:

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

    Explanation:

    • PrintMessage proc: Declares the start of the procedure PrintMessage.
    • mov ah, 09h and int 21h: These are DOS interrupt calls to print a string.
    • ret: This instruction returns control to the caller (main program) when the procedure is finished.

    To assemble and run:

    1. Write the program in a text file named basic_procedure.asm.
    2. Assemble using MASM: ml basic_procedure.asm
    3. Link the program: link basic_procedure.obj
    4. Run the resulting executable.

    2. Procedure with Parameters

    In this example, we will pass parameters to the procedure. We'll pass two numbers to a procedure that will add them together and print the result.

    MASM Code:

    ; procedure_with_params.asm
    .model small
    .stack 100h
    
    .data
        num1 dw 5           ; First number
        num2 dw 3           ; Second number
        result db 'Result: $'
    
    .code
    main:
        mov ax, @data       ; Initialize data segment
        mov ds, ax
    
        ; Call AddNumbers procedure with parameters
        mov ax, num1        ; Load num1 into AX
        mov bx, num2        ; Load num2 into BX
        call AddNumbers     ; Call procedure
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to add two numbers and display result
    AddNumbers proc
        add ax, bx          ; AX = AX + BX (num1 + num2)
        ; Print the result message
        mov ah, 09h         ; DOS function to print string
        lea dx, result      ; Load address of result message
        int 21h             ; Call DOS interrupt to print message
    
        ; Convert result to ASCII and print it
        add al, '0'         ; Convert to ASCII (assuming result is between 0-9)
        mov dl, al          ; Load result into DL for printing
        mov ah, 02h         ; DOS function to print character
        int 21h             ; Call DOS interrupt to print character
    
        ret                 ; Return to main program
    AddNumbers endp
    end main
    

    Explanation:

    1. Main program: We load the values of num1 and num2 into AX and BX, then call the AddNumbers procedure.
    2. AddNumbers procedure:
      • add ax, bx: Adds the values of AX (num1) and BX (num2).
      • The result is stored in AX and is printed by converting the result into an ASCII character.
      • The result is printed using DOS interrupt 21h (int 21h) to display the message and the sum.
    3. The procedure does not return any complex data types but simply modifies the contents of the AX register.

    3. Procedure with Return Values

    In this example, we'll create a procedure that calculates the sum of two numbers and returns the result in a register (specifically AX).

    MASM Code:

    ; procedure_with_return_value.asm
    .model small
    .stack 100h
    
    .data
        num1 dw 10          ; First number
        num2 dw 15          ; Second number
        resultMessage db 'The sum is: $'
    
    .code
    main:
        mov ax, @data       ; Initialize data segment
        mov ds, ax
    
        ; Call AddNumbers procedure with parameters
        mov ax, num1        ; Load num1 into AX
        mov bx, num2        ; Load num2 into BX
        call AddNumbers     ; Call procedure
    
        ; Print result message
        mov ah, 09h         ; DOS function to print string
        lea dx, resultMessage ; Load address of result message
        int 21h             ; Call DOS interrupt to print message
    
        ; Print the result (in AX)
        add ax, '0'         ; Convert result to ASCII
        mov dl, al          ; Load result into DL
        mov ah, 02h         ; DOS function to print character
        int 21h             ; Call DOS interrupt to print result
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to add two numbers and return the result in AX
    AddNumbers proc
        add ax, bx          ; AX = AX + BX
        ret                 ; Return to caller with result in AX
    AddNumbers endp
    end main
    

    Explanation:

    1. Main program:
      • Loads the values of num1 and num2 into AX and BX.
      • Calls the AddNumbers procedure.
    2. AddNumbers procedure:
      • add ax, bx: Adds the values in AX and BX and stores the result in AX.
      • The result is returned in AX, which is then printed in the main program.
    3. We use the add ax, '0' instruction to convert the result (a number) into an ASCII character before printing it.

    4. Procedure with Multiple Parameters

    This example demonstrates how to pass multiple parameters to a procedure and handle more complex calculations.

    MASM Code:

    ; multiple_params.asm
    .model small
    .stack 100h
    
    .data
        num1 dw 10          ; First number
        num2 dw 5           ; Second number
        num3 dw 2           ; Third number
        resultMessage db 'The sum of the numbers is: $'
    
    .code
    main:
        mov ax, @data       ; Initialize data segment
        mov ds, ax
    
        ; Call AddThreeNumbers procedure with multiple parameters
        mov ax, num1        ; Load num1 into AX
        mov bx, num2        ; Load num2 into BX
        mov cx, num3        ; Load num3 into CX
        call AddThreeNumbers ; Call procedure
    
        ; Print result message
        mov ah, 09h         ; DOS function to print string
        lea dx, resultMessage ; Load address of result message
        int 21h             ; Call DOS interrupt to print message
    
        ; Print the result (in AX)
        add ax, '0'         ; Convert result to ASCII
        mov dl, al          ; Load result into DL
        mov ah, 02h         ; DOS function to print character
        int 21h             ; Call DOS interrupt to print result
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to add three numbers and return the result in AX
    AddThreeNumbers proc
        add ax, bx          ; AX = AX + BX
        add ax, cx          ; AX = AX + CX
        ret                 ; Return with result in AX
    AddThreeNumbers endp
    end main
    

    Explanation:

    1. Main program:
      • Loads three numbers (num1, num2, and num3) into AX, BX, and CX respectively.
      • Calls the AddThreeNumbers procedure.
    2. AddThreeNumbers procedure:
      • Adds the values of AX, BX, and CX together.
      • The result is returned in AX, which is then printed in the main program.

    5. Using the Stack for Local Variables (Push and Pop)

    In MASM, you can use the stack to allocate local variables in procedures. This allows each procedure to have its own private workspace, independent of other parts of the program.

    MASM Code:

    ; stack_local_variables.asm
    .model small
    .stack 100h
    
    .data
        resultMessage db 'The result of the operation is: $'
    
    .code
    main:
        mov ax, @data       ; Initialize data segment
        mov ds, ax
    
        ; Call Calculate procedure
        call Calculate
    
        ; Exit program
        mov ah, 4Ch
        int 21h
    
    ; Procedure to perform calculations with local variables
    Calculate proc
        push ax              ; Save AX to stack (local variable)
    
    Previous topic 59
    Practice on MASM
    Next topic 61
    File Operations Procedures

    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,235
      Code examples0
      DifficultyIntermediate