Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microprocessor and Assembly Language

Similar presentations


Presentation on theme: "Microprocessor and Assembly Language"— Presentation transcript:

1 Microprocessor and Assembly Language
Lecture-8-Stack and Procedures Muhammad Hafeez Department of Computer Science GC University Lahore

2 Today’s Agenda What is Stack Use of Stack Procedures Calls

3 Stack .Stack 100H Reserve 256 bytes in stack segment
Stack is Last-In-First-Out (LIFO) structure 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

4 Use of Stack Preserving temporary data and registers
Supporting procedures and interrupt mechanism Passing Parameters to functions Allocating local variable

5 Push Instruction Add a new word to Stack Syntax
PUSH Destination Destination can be 16-bit Register or Memory Location Function of Push Instruction SP is decremented by 2 A copy of destination content is moved on top of stack, destination unchanged

6 Push Instruction Example
TOS

7 Example MOV AX,FFAB MOV BX,CDEA PUSH AX PUSH BX

8 Pop Instruction Remove a word from Top of Stack (TOS) Syntax
POP Destination Destination can be 16-bit Register or Memory Location Function of POP Instruction SP is incremented by 2 A copy of TOS is moved to destination

9 Pop instruction Example

10 Pushf and Popf Instructions
Pushf is used to push flag register on TOS Popf is used to pop flag register from TOS

11 Procedures 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

12 Steps to Execute Procedure
Step 1: Save Return Address Step 2: Call Procedure Step 3: Execute Procedure Step 4: Return Execution to Saved Returned Address at step 1

13 Procedure Declaration
Name PROC type ;body of procedure RET Name ENDP Type can be NEAR or FAR

14 Procedures Declaration
Main Procedure User defined called Procedure

15 Call Instruction To invoke a procedure CALL instruction is used
Two types of CALL instructions Direct In-Direct Syntax CALL name ;for direction call CALL address_expression ; for indirect call Address_Expression can be register or memory location

16 Working of Call Instruction
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 IP is pushed, then CS is pushed

17 Ret Instruction Return the control to calling procedure Syntax
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.

18 Procedure Directives 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 are 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

19 Program Modules Two Reasons:
Procedures can be put in separate files for large programs Two Reasons: Procedures can be coded, assembled and tested separately When Procedures are coded and assembled separately one can use same names for the variables

20 Program Modules 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

21 Program Modules

22 Extrn and Public Pseudo-Op
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

23 Extrn Pseudo-Op 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 Assembler will not generate undefined message Syntax is: EXTRN external_name_list EX: EXTRN PROC1:NEAR, DATA1:BYTE, PROC2:FAR EXTRN may appear anywhere but before first use of external_name_lists

24 Public Pseudo-Op 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: PUBLIC external_name_list EX: PUBLIC PROC1, DATA1, PROC2 PUBLIC may appear at start of module

25 Questions ??????????????????????????


Download ppt "Microprocessor and Assembly Language"

Similar presentations


Ads by Google