MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE8 STACK AND PROCEDURES
MICROPROCESSOR AND ASSEMBLY LANGUAGE LECTURE-8 -STACK AND PROCEDURES MUHAMMAD HAFEEZ DEPARTMENT OF COMPUTER SCIENCE GC UNIVERSITY LAHORE
TODAY’S AGENDA l l l What is Stack Use of Stack Procedures Calls
STACK l l l . Stack 100 H Reserve 256 bytes in stack segment Stack is Last-In-First-Out (LIFO) structure l l That’s why Stack always have Top of Stack (TOS) When a stack is not empty SS holds segment address and SP holds offset address Stack in 8086 family of processors is decremented stack Stack has two instruction associated with it PUSH and POP
USE OF STACK l l Preserving temporary data and registers Supporting procedures and interrupt mechanism Passing Parameters to functions Allocating local variable
PUSH INSTRUCTION l l Add a new word to Stack Syntax l l l PUSH Destination can be 16 -bit Register or Memory Location Function of Push Instruction l l SP is decremented by 2 A copy of destination content is moved on top of stack, destination unchanged
PUSH INSTRUCTION EXAMPLE TOS
EXAMPLE l l MOV AX, FFAB MOV BX, CDEA PUSH AX PUSH BX
POP INSTRUCTION l l Remove a word from Top of Stack (TOS) Syntax l l l POP Destination can be 16 -bit Register or Memory Location Function of POP Instruction l l SP is incremented by 2 A copy of TOS is moved to destination
POP INSTRUCTION EXAMPLE
PUSHF AND POPF INSTRUCTIONS l l Pushf is used to push flag register on TOS Popf is used to pop flag register from TOS
PROCEDURES l l l Used to top-down design of programs Divide main problem into subproblem Subproblems are implemented as separate procedures Accept well defined input, perform certain actions and optionally return output Inputs are passed by value or passed by reference
STEPS TO EXECUTE PROCEDURE l l Step 1: Save Return Address Step 2: Call Procedure Step 3: Execute Procedure Step 4: Return Execution to Saved Returned Address at step 1
PROCEDURE DECLARATION Name PROC type ; body of procedure RET Name l ENDP Type can be NEAR or FAR
PROCEDURES DECLARATION Main Procedure User defined called Procedure
CALL INSTRUCTION l l To invoke a procedure CALL instruction is used Two types of CALL instructions l l l Direct In-Direct Syntax l l CALL name ; for direction call CALL address_expression ; for indirect call l Address_Expression can be register or memory location
WORKING OF CALL INSTRUCTION l l Push return address of calling program on Top of Stack This is the address of very next instruction after CALL instruction If CALL is a NEAR call then only IP is pushed If CALL is a FAR call both CS: IP are pushed l l IP is pushed, then CS is pushed
RET INSTRUCTION l l Return the control to calling procedure Syntax l l l RET In case of NEAR procedure, POP IP from TOS, that causes control return to calling procedure In case of FAR procedure, both CS: IP are popped.
PROCEDURE DIRECTIVES l l l l In a NEAR procedure, both calling and called procedures are in the same code segment. Called and calling procedures are in two different segments in a FAR procedure. PROC and ENDP do not generate any code; they are directives, not instructions. If the operand to a PROC directive is NEAR then all RET instructions between that PROC directive and the corresponding ENDP directive are assembled as near returns. If, on the other hand, the operand to a PROC directive is FAR, then all RET instructions within that procedure assembled as far return. If the PROC directive is without any operand, the assembler automatically makes the procedure near or far according to the memory model selected with the. MODEL directive. Tiny, Small, Compact NEAR Medium , Large, Huge, Far
PROGRAM MODULES l Procedures can be put in separate files for large programs l Two Reasons: l Procedures can be coded, assembled and tested separately l When Procedures are coded and assembled separately one can use same names for the variables
PROGRAM MODULES l l l Program modules are created in separate. ASM file This. ASM file is assembled separately and. OBJ file is produces Linker takes in all. OBJ files and produces a single. EXE file
PROGRAM MODULES
EXTRN AND PUBLIC PSEUDO-OP l l l The Use of Near and Far Pseudo-op can be seen by using Extrn and Public pseudo-ops However, a procedure can be NEAR even it is assembled separately A FAR procedure is used when it is impossible for a calling procedure to fit in a single code segment
EXTRN PSEUDO-OP l l When we use DATA or CODE (procedure) in a module (. ASM) file, that is declared to written in another module (. ASM) file. We Inform Assembler with the help of EXTRN pseudo-op l l l Assembler will not generate undefined message Syntax is: EXTRN external_name_list EX: EXTRN PROC 1: NEAR, DATA 1: BYTE, PROC 2: FAR EXTRN may appear anywhere but before first use of external_name_lists
PUBLIC PSEUDO-OP l l l When we want DATA or CODE (procedure) in a module (. ASM) file, to be used in another module (. ASM) file. We Inform Assembler with the help of PUBLIC pseudo-op Syntax is: l l EX: l l PUBLIC external_name_list PUBLIC PROC 1, DATA 1, PROC 2 PUBLIC may appear at start of module
QUESTIONS l ? ? ? ? ? ? ?
- Slides: 25