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
You are on page 1/ 18
Lecture # 08
The Stack &
Introduction to Procedures Engr. Fiaz Khan
Engr. Muhammad Fiaz 1
Overview • Introduction to Stack: The stack segment in a program serves as a temporary storage for both data and addresses. • Manipulation of Stack: Section 8.1 introduces the PUSH and POP instructions, which add and remove words from the stack. The last item added to the stack is the first one removed, enabling stack utilization for reversing data lists. • Importance of Procedures: Procedures play a crucial role in both high-level and assembly language programming. Sections 8.3 and 8.4 discuss the fundamentals of assembly language procedures, detailing how they are called and returned at the machine level. • Binary Multiplication Procedure: In section 8.5, an example procedure for binary multiplication using bit shifting and addition is presented. This example also provides insights into the DEBUG program.
Engr. Muhammad Fiaz 2
The Stack • Concept of a Stack: A stack is like a pile of items where things are added and removed from one end only, following a "last-in, first-out" rule. • Top of the Stack: The newest addition to the stack is called the top. Just like the last dish placed on a stack of dishes, the top item in a stack is the most recent one added and can be easily removed. • Memory Allocation for Stack: Programs allocate a block of memory to hold the stack. This is typically done by declaring a stack segment. For example, .STACK 100h reserves memory space for the stack. • Stack Pointer (SP): When a program is assembled and loaded into memory, SP, the stack pointer, is initialized to a specific value, such as 100h. This marks the empty position of the stack. When the stack is not empty, SP holds the memory address of the top item in the stack.
Engr. Muhammad Fiaz 3
PUSH & PUSHF • Adding to the Stack: To add a new word to the stack, we use the PUSH instruction. Its syntax is • PUSH source, • where the source can be a 16-bit register or memory word. For example, PUSH AX adds the content of the AX register to the stack. • Execution of PUSH: When a PUSH instruction is executed, two things happen: • SP (Stack Pointer) is decreased by 2. • The content of the source is copied to the memory address specified by SS:SP. The original source remains unchanged. • PUSHF Instruction: The PUSHF instruction, without any operands, pushes the contents of the FLAGS register onto the stack. • Initial State of SP: Initially, SP contains the offset address of the memory location immediately after the stack segment. The first PUSH instruction decreases SP by 2, pointing it to the last word in the stack segment. As each PUSH decreases SP, the stack grows towards the beginning of memory.
Engr. Muhammad Fiaz 4
Figure 8. 1A Empty Stack Figure 8.1B After PUSH AX
Engr. Muhammad Fiaz 5
Figure 8.1C After PUSH BX
Engr. Muhammad Fiaz 6
POP & POPF • Removing from the Stack: To remove the top item from the stack, we use the POP instruction. Its syntax is POP destination, • where the destination can be a 16-bit register (except IP) or memory word. For example, POP BX removes the top item from the stack and stores it in the BX register. • Execution of POP: When a POP instruction is executed: • The content of SS:SP (the top of the stack) is moved to the destination. • SP is increased by 2. • POPF Instruction: The POPF instruction pops the top of the stack into the FLAGS register. It doesn't affect any flags. • Word Operations: Both PUSH and POP are word operations, meaning they deal with 16-bit data. Therefore, attempting to push or pop a byte (8-bit) or immediate data is illegal. • Operating System Usage: In addition to user programs, the operating system also uses the stack for its own purposes. For instance, in DOS, the stack is utilized to implement INT 21h functions, where DOS saves and restores any registers it uses on the stack during interrupt routines.
Engr. Muhammad Fiaz 7
8.2A Before POP B_2B After POP CX
Engr. Muhammad Fiaz 8
8.2C After POP DX
Engr. Muhammad Fiaz 9
Applications of Stack • Procedure Calls: Used to store return addresses and local variables during function calls. • Expression Evaluation: Helps in evaluating arithmetic expressions involving operands and operators. • Temporary Storage: Provides temporary storage for data and addresses within a program. • Recursive Function Calls: Facilitates recursive function execution by maintaining multiple function calls' contexts. • Error Handling: Assists in storing error codes or messages for error handling routines. • Context Switching: Used in multitasking environments to save and restore the execution context of processes.
Engr. Muhammad Fiaz 10
Terminology of Procedures • Top-down Design: Break down complex problems into smaller, more manageable subproblems. • Assembly Language Procedures: Similar to high-level language functions, used to solve subproblems. • Program Structure: Composed of multiple procedures, with the main procedure serving as the entry point. • Procedure Calls: Control transfers from one procedure to another, executing instructions accordingly. • Return Mechanism: After execution, control returns to the caller, usually to the next instruction after the call statement. • Visibility in Assembly Language: Unlike high-level languages, programmers can observe the implementation of call and return mechanisms.
Engr. Muhammad Fiaz 11
Terminology of Procedures • Procedure Declaration Syntax: Define a procedure using the following format:
• Name: User-defined identifier for the procedure
• Type (Optional): Specifies whether the procedure is NEAR or FAR. 1. NEAR: Procedure and the calling statement are in the same segment. 2. FAR: Calling statement is in a different segment. • RET (return) instruction is essential for transferring control back to the calling procedure, ensuring orderly program execution and resource management. Always place RET as the last statement in procedures, preventing memory leaks and maintaining code readability.
Engr. Muhammad Fiaz 12
Engr. Muhammad Fiaz 13 Communication Between Procedures • In assembly language, procedures communicate by passing values through registers since they lack parameter lists. • Programmers determine how input and output values are handled within procedures, often utilizing registers like AX, BX, CX, and DX. • Detailed discussion on procedure communication is available in Chapter 14 of the reference material.
Engr. Muhammad Fiaz 14
CALL and RET • Procedure Invocation: • Procedures are invoked using the CALL instruction in assembly language. • There are two types of procedure calls: direct and indirect. • Direct Procedure Call Syntax: • Direct procedure calls use the syntax CALL name, where "name" is the name of the procedure being called. • Indirect Procedure Call Syntax: • Indirect procedure calls use the syntax CALL address_expression, where "address_expression" specifies a register or memory location containing the address of the procedure. • Execution of CALL Instruction: • When a CALL instruction is executed, the return address to the calling program is saved on the stack. • This return address is the offset of the next instruction after the CALL statement, with the segment:offset of this instruction in CS:IP at the time the call is executed.
Engr. Muhammad Fiaz 15
Engr. Muhammad Fiaz 16 Engr. Muhammad Fiaz 17 ENGR. MUHAMMAD FIAZ LECTURER COMPUTER SCIENCE
TELF/EF SET / ACEPT CERTIFIED
MICROSOFT ACADEMY TRAINER
ORACLE CERTIFIED PROFESSIONAL
GOOGLE CERTIFIED PROFESSIONAL
THANK YOU! Lahore, Pakistan
+92-320-7617-093
[email protected] Do you have any questions? https://www.linkedin/in/fiazofficials