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 (SPSP) and Stack Segment (SSSS) 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 256256 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: Physical Address=(SS×10h)+SPPhysical\ Address = (SS \times 10h) + SP.

  • Initialization:

    • If a stack is defined as 256256 bytes (100h100h), the SPSP is initially set to 0100h0100h. This points to the byte immediately following the stack area.

PUSH AND POP OPERATIONS
  • PUSH (Insert 16-bit value)

    • Mechanism:

      1. Decrement SPSP by 22 (since each stack entry is a word - 22 bytes): SP=SP2SP = SP - 2.

      2. Store the source operand at the address pointed to by SS:SPSS:SP.

    • 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:

      1. Copy the value at the top of the stack (SS:SPSS:SP) to the destination operand.

      2. Increment SPSP by 22: SP=SP+2SP = SP + 2.

    • 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 endp

  • CALL Instruction:

    • Pushes the current Instruction Pointer (IPIP) onto the stack (the return address).

    • Loads the address of the procedure into the IPIP.

  • RET Instruction:

    • Pops the value from the top of the stack into the IPIP, 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., AX,BXAX, BX) before the procedure is called.

  • Stack Method:

    • More robust. Parameters are PUSHed onto the stack before the CALL. The procedure often uses the Base Pointer (BPBP) to access these parameters without moving the SPSP.

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
  1. String Manipulation: Use procedures to modularize the display logic and handle newlines independently.

  2. Case Inversion: Create a subroutine that iterates through a string and toggles bit 5 (20h20h) to convert between uppercase and lowercase.

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