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 PortalCOMP3137Procedures
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
    7 min read
    Topic 60 of 73
    Topic Study Note Illustration
    60

    Procedures

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

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

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language