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.
In this example, we'll define a simple procedure that prints a string message.
; 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
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:
basic_procedure.asm.ml basic_procedure.asmlink basic_procedure.objIn 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.
; 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
num1 and num2 into AX and BX, then call the AddNumbers procedure.add ax, bx: Adds the values of AX (num1) and BX (num2).AX and is printed by converting the result into an ASCII character.int 21h) to display the message and the sum.AX register.In this example, we'll create a procedure that calculates the sum of two numbers and returns the result in a register (specifically AX).
; 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
num1 and num2 into AX and BX.AddNumbers procedure.add ax, bx: Adds the values in AX and BX and stores the result in AX.AX, which is then printed in the main program.add ax, '0' instruction to convert the result (a number) into an ASCII character before printing it.This example demonstrates how to pass multiple parameters to a procedure and handle more complex calculations.
; 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
num1, num2, and num3) into AX, BX, and CX respectively.AddThreeNumbers procedure.AX, BX, and CX together.AX, which is then printed in the main program.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.
; 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)
Open this section to load past papers