Lab 9: Stack Operations and Procedures
LAB 9: STACK OPERATIONS AND PROCEDURES
OBJECTIVES
Understanding key concepts related to stack operations and procedures in assembly language programming:
Stack Pointer () and Stack Segment () coordination.
The mechanics of PUSH and POP operations.
Modular programming using Procedures (subroutines).
Control flow via CALL and RET instructions.
Advanced concept of Parameter Passing using Registers and Memory.
Nested Procedure Calls and label scoping.
RUNTIME STACK
The runtime stack is a specialized memory area managed by the CPU to store temporary data and return addresses.
LIFO (Last In, First Out): The stack operates on a principle where the last item pushed is the first one to be popped.
Stack Direction: In x86 architecture, the stack grows downward in memory, meaning as data is added, the stack pointer moves toward lower memory addresses.
Allocation:
stack 100h(Allocates bytes in decimal).The stack size must be sufficient to prevent Stack Overflow during deep nesting or complex recursions.
STACK SEGMENT (SS) AND STACK POINTER (SP)
Stack Segment (SS): Holds the base segment address of the stack.
Stack Pointer (SP): Holds the offset address of the current top of the stack.
Physical Address Calculation:
The actual memory location is determined by: .
Initialization:
If a stack is defined as bytes (), the is initially set to . This points to the byte immediately following the stack area.
PUSH AND POP OPERATIONS
PUSH (Insert 16-bit value)
Mechanism:
Decrement by (since each stack entry is a word - bytes): .
Store the source operand at the address pointed to by .
Operands: Can be a 16-bit register (AX, BX, etc.) or an immediate value (in newer processors).
Example:
push ax;
POP (Retrieve 16-bit value)
Mechanism:
Copy the value at the top of the stack () to the destination operand.
Increment by : .
Operands: Must be a 16-bit register or memory location. It cannot be an immediate value.
Example:
pop dx;
CREATING AND CALLING PROCEDURES
Procedures: Named blocks of code that perform specific tasks, promoting code reusability.
Structure:
asm sample proc ; Procedure instructions ret ; Must end with return to send control back to caller sample endpCALL Instruction:
Pushes the current Instruction Pointer () onto the stack (the return address).
Loads the address of the procedure into the .
RET Instruction:
Pops the value from the top of the stack into the , resuming execution at the point immediately following the original
CALL.
PARAMETER PASSING METHODS
Register Method:
Efficient but limited. Parameters are placed in registers (e.g., ) before the procedure is called.
Stack Method:
More robust. Parameters are
PUSHed onto the stack before theCALL. The procedure often uses the Base Pointer () to access these parameters without moving the .
LOCAL AND GLOBAL LABELS
Local Labels: Symbols defined within a procedure that are only "visible" to instructions within that specific procedure.
Global Labels: Identifiers followed by a double colon (
LabelName::) that can be accessed from any procedure within the program module.
LAB TASKS SUMMARY
String Manipulation: Use procedures to modularize the display logic and handle newlines independently.
Case Inversion: Create a subroutine that iterates through a string and toggles bit 5 () to convert between uppercase and lowercase.
Arithmetic Subroutines: Develop procedures for multiplication and division.
Task 5 Note: Requires using the stack specifically to pass the dividend and divisor before calling the division procedure.
EXAMPLE OF PROCEDURE SCOPE ERRORS
main PROC
jmp L2 ; Error: L2 is local to sub2 and not accessible from main
L1:: ; Global label accessible everywhere
exit
main ENDP
sub2 PROC
L2: ; Local label
jmp L1 ; Valid jump to global label L1
ret
sub2 ENDP