0% found this document useful (0 votes)
18 views6 pages

Assembly Language Procedures Lab Guide

This document discusses advanced procedures in assembly language. It covers stack frames, accessing stack parameters using the base pointer EBP, the RET instruction, local variables declared with the LOCAL directive, and recursion. The lab work exercises students to write a recursive procedure that prints a hexadecimal number and a recursive factorial function.

Uploaded by

Abraham Damtew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Assembly Language Procedures Lab Guide

This document discusses advanced procedures in assembly language. It covers stack frames, accessing stack parameters using the base pointer EBP, the RET instruction, local variables declared with the LOCAL directive, and recursion. The lab work exercises students to write a recursive procedure that prints a hexadecimal number and a recursive factorial function.

Uploaded by

Abraham Damtew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Faculty of Engineering

Computer Engineering Department


Islamic University of Gaza

Assembly Language Lab # 10


Advanced Procedures

Eng. [Link]
Assembly Language Fundamentals

Objective:
To learn more about procedures.

Part1: Stack Frame (activation record):

Area of the stack set aside for passed arguments, subroutine return address, local variables,
and saved registers.

Created by the following steps:

1. Passed arguments, if any, are pushed on the stack.


2. The subroutine is called, causing the subroutine return address to be pushed on the stack.
3. As the subroutine begins to execute, EBP is pushed on the stack.
4. EBP is set equal to ESP. From this point on, EBP acts as a base reference for all of the subroutine
parameters.
5. If there are local variables, ESP is decremented to reserve space for the variables on the stack.
6. If any registers need to be saved, they are pushed on the stack.
Advanced Procedures

Explicit Access to Stack Parameters:


A procedure can explicitly access stack parameters using constant offsets from EBP.
 EBP is often called the base pointer or frame pointer because it holds the base address
of the stack frame.

 EBP must be restored to its original value when a procedure returns.

RET Instruction:
Assembly Language Lab # 10

 Return from subroutine.


 Pops stack into the instruction pointer (EIP or IP). Control transfers to the target address.

Syntax:
 RET
 RET n

Optional operand n causes n bytes to be added to the stack pointer after EIP (or IP)
is assigned a value.

1
.data
sum DWORD ?
.code
push 6 ; second argument
push 5 ; first argument
call AddTwo ; EAX = sum
mov sum,eax ; save the sum

AddTwo PROC
push ebp
mov ebp,esp ; base of stack frame
mov eax,[ebp + 12] ; second parameter
add eax,[ebp + 8] ; first parameter
pop ebp
ret
AddTwo ENDP

Part2: Local Variables:

A local variable is created, used, and destroyed within a single procedure.

 Local variables are created on the runtime stack, usually below the base pointer (EBP).
 The LOCAL directive declares a list of local variables and immediately follows the PROC
directive, each variable is assigned a type.
 Syntax: LOCAL varlist

Advanced Procedures
Syntax:
LOCAL var1:type1, var2:type2, . . .

Example:

MySub PROC
LOCAL var1:BYTE, var2:WORD, var3:DWORD Assembly Language Lab # 10

Part3: Recursion:

 A recursive procedure is one that calls itself, either directly or indirectly.


 Recursion, the practice of calling recursive procedures, can be a powerful tool when
working with data structures that have repeating patterns.

2
Lab work:
Excercise1:
Write an assembly recursive procedure that prints a hexadecimal number saved in memory on the screen.
Advanced Procedures
Assembly Language Lab # 10

3
Excercise2:
This function calculates the factorial of integer n ≥ 0. A new value of n is saved in each stack
frame: (use n=3, 5, 12)

int function factorial(int n)


{
if(n == 0)
return 1;
else
return n * factorial(n-1);
}

Advanced Procedures
Assembly Language Lab # 10

4
Assembly Language Lab # 10 Advanced Procedures

Common questions

Powered by AI

Recursion in assembly language is implemented by having a function call itself either directly or indirectly. This requires saving the current state of execution on the stack to ensure correct retrieval after completing recursive calls. The stack frames thus grow with each call, storing parameters and local variables specific to each invocation. Recursive procedures are powerful for handling problems characterized by repeating patterns or hierarchical data structures, such as factorial calculation and directory traversal. Assembly can handle such recursive functions by carefully managing EBP and ESP to ensure stack integrity and correct value retrieval .

In a recursive factorial function in assembly, each recursive call creates a new stack frame that saves the current values of parameters and the return address. The base case checks if n is 0, returning 1 directly. For other values, the function calls itself with (n-1), multiplying the returned result by n. With each call, the current value of n and partial computations are pushed onto the stack, managed through EBP and ESP adjustments. This process continues until the base case is reached, after which the stack frames are popped off as control returns through the recursive calls, cumulating the factorial result .

The LOCAL directive optimizes stack management by explicitly declaring variables that are only relevant to the individual execution of a procedure. By localizing these variables within the stack frame associated with the procedure, memory is efficiently allocated and deallocated automatically, minimizing leakage and enabling clear stack organization. This ensures variables do not persist beyond their useful scope, preventing stack overcrowding and making debugging and understanding stack states less complex in complex procedures .

Changing ESP influences stack operations directly as it controls where the top of the stack is located, thereby dictating where new data can be pushed and from where data can be popped. Correct adjustment of ESP is crucial for setting up stack frames for subroutine calls and local variable allocation. Mismanagement can lead to stack overflows, incorrect data access, or loss of data integrity. In subroutine execution, ESP is modified initially to accommodate arguments and return addresses, and throughout execution as local variables and saved states are added or removed from the stack .

The stack frame, also known as the activation record, is crucial for managing subroutines in assembly language. It organizes the stack space for passed arguments, the subroutine return address, local variables, and saved registers. When a subroutine is called, arguments are pushed onto the stack, followed by the return address and the current base pointer (EBP). EBP is then set equal to ESP (the stack pointer) to serve as the base reference for accessing subroutine parameters. Local variables and registers that need to be saved are managed by adjusting the ESP. The stack frame ensures that each subroutine has a clean state to execute with its variables and return control efficiently post-execution .

The RET instruction is integral to subroutine execution as it facilitates a return from the subroutine to the calling function. It achieves this by popping the return address from the stack into the instruction pointer (EIP or IP), transferring control back to the calling code. An optional operand 'n' can be used with RET to adjust the stack pointer by adding 'n' bytes after assigning the return address, which helps in managing the stack by cleaning up parameters automatically when needed. This instruction ensures the smooth transfer of control and helps maintain the stack's integrity .

EBP, or the base pointer, plays the role of holding the base address of the stack frame, allowing procedures to access stack parameters using constant offsets from EBP. When a subroutine is called, EBP is initially pushed onto the stack, then set to equal ESP, providing a stable reference point. This setup enables consistent indexing to access arguments from the call stack. EBP must be restored to its initial value before the procedure returns to ensure the stack state is maintained correctly .

Novice assembly programmers often face challenges like understanding the intricacies of direct memory manipulation, managing preliminary stack setup, and ensuring precise subroutine execution with stack frame integrity. Mitigation strategies include thorough grounding in fundamental concepts such as base pointers, stack operations, and instruction executions. Utilizing extensive practice exercises and working with guided examples, such as implementing recursion or handling stack-based variables, can build confidence. Understanding error checking cues, following structured coding practices, and close referencing of procedure call conventions can significantly ease the learning curve .

Local variables in assembly are handled by allocating space within the runtime stack, typically below the base pointer (EBP). The LOCAL directive is used to declare them immediately following the PROC directive, with each variable being assigned a specific type. This setup allows local variables to be used during the procedure's execution and ensures they are destroyed upon the procedure’s termination. This management facilitates efficient use of memory and stack space during the lifespan of the subroutine .

Direct parameter access in assembly involves using the registers that immediately contain argument values, ideal for fast calling conventions and simple tasks. In contrast, stack-based parameter access uses the stack to pass and manage arguments, using EBP as a reference for consistent indexing. Stack-based access is better suited for complex procedures requiring more parameters, local variable management, and recursive functions. Direct access allows for quicker execution while stack access provides structured and scalable parameter management, with the latter offering more robust handling of complex and numerous data points through standardized stack operations .

You might also like