In computer organization and assembly language, a procedure (also known as a function or subroutine) is a block of code that performs a specific task and can be reused multiple times within a program. Procedures are used to modularize code, making it easier to maintain, understand, and debug.
A procedure is a set of instructions that can be executed when called by the program. When a procedure is called, the program jumps to the procedure, executes the instructions, and then returns to the point where it was called. This makes the code more organized and modular, reducing redundancy and improving efficiency.
A procedure typically has two main parts:
In assembly language, creating a procedure typically involves defining a block of code with a label (a symbolic name) and using a call instruction to invoke it. Procedures are often used to perform repetitive tasks or to organize code logically.
Defining a Procedure: A simple procedure might add two numbers and return the result.
ADD_NUMBERS: ; Procedure label
ADD AX, BX ; Add contents of AX and BX
RET ; Return from procedure
In this example:
ADD_NUMBERS: is the label that marks the start of the procedure.ADD AX, BX performs the addition.RET is the return instruction, which causes the program to jump back to the point where the procedure was called.Calling a Procedure:
To call the ADD_NUMBERS procedure, we use the CALL instruction.
MOV AX, 5 ; Load 5 into AX
MOV BX, 10 ; Load 10 into BX
CALL ADD_NUMBERS ; Call the procedure
CALL instruction pushes the return address onto the stack and jumps to the procedure.RET instruction in the procedure, the program returns to the next instruction after the CALL.Procedures can take inputs, called parameters or arguments, which are passed to them. The most common methods of passing parameters are:
; Procedure to add two numbers
ADD_NUMBERS:
ADD AX, BX ; Add BX to AX (AX holds the result)
RET ; Return from the procedure
; Main program
MOV AX, 5 ; First number in AX
MOV BX, 10 ; Second number in BX
CALL ADD_NUMBERS ; Call procedure, result in AX
In this case, the values in AX and BX are passed as arguments to the ADD_NUMBERS procedure. The result of the addition is stored back in AX.
; Procedure to add two numbers
ADD_NUMBERS:
POP BX ; Pop the first parameter into BX
POP AX ; Pop the second parameter into AX
ADD AX, BX ; Add AX and BX
PUSH AX ; Push the result back onto the stack
RET ; Return from the procedure
; Main program
PUSH 10 ; Push first parameter onto the stack
PUSH 5 ; Push second parameter onto the stack
CALL ADD_NUMBERS ; Call procedure to add the numbers
POP AX ; Pop the result into AX
In this case, we are pushing the parameters onto the stack before calling the procedure. The procedure pops the values off the stack, performs the addition, and then pushes the result back onto the stack.
Local Variables: Variables that are used only within a procedure and are typically stored on the stack. These variables are created when the procedure is called and destroyed when the procedure returns.
Global Variables: Variables that are accessible by all procedures in the program. These are usually stored in memory and retain their values throughout the program’s execution.
When a procedure is called, the stack plays a crucial role in managing the procedure’s execution:
CALL instruction is executed, the address of the next instruction is pushed onto the stack. This is where the program will return after the procedure finishes.RET instruction pops the return address from the stack and jumps back to that address in the main program.A recursive procedure is a procedure that calls itself. This is useful for tasks that can be broken down into smaller, similar tasks, such as calculating factorials or solving problems using divide and conquer methods.
; Procedure to calculate factorial
FACTORIAL:
CMP AX, 1 ; Compare AX with 1
JLE DONE ; If AX <= 1, done
PUSH AX ; Save AX to the stack
DEC AX ; Decrement AX
CALL FACTORIAL ; Recursive call
POP BX ; Restore original AX value
MUL BX ; Multiply result by the original AX
DONE:
RET ; Return from procedure
; Main program
MOV AX, 5 ; Load 5 into AX
CALL FACTORIAL ; Call factorial procedure
In this example, the FACTORIAL procedure calls itself until it reaches the base case (when AX <= 1). The result of the multiplication is returned through the stack.
To return a value from a procedure, you can use the AX register (for 16-bit values) or other registers, depending on the architecture. The value is usually stored in a register when the procedure finishes, and the caller retrieves it after the procedure call.
Here’s an example of how you might structure a program that uses procedures:
; Program to calculate the sum of two numbers using a procedure
SUM_PROCEDURE:
ADD AX, BX ; Add BX to AX
RET ; Return from procedure
START:
MOV AX, 5 ; Load 5 into AX
MOV BX, 10 ; Load 10 into BX
CALL SUM_PROCEDURE ; Call the procedure to sum the numbers
; At this point, AX contains the sum (15)
; Continue with the program...
Procedures in assembly language are a powerful way to organize and reuse code, making programs more modular, readable, and maintainable. By understanding how to define, call, and manage procedures, you can create efficient programs that perform complex tasks with simplicity and clarity. Procedures allow you to break down tasks, pass parameters, and manage execution flow effectively.
Open this section to load past papers