1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

There are two types of addressing schemes:
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Computer Organization And Assembly Language
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
CS2422 Assembly Language & System Programming October 24, 2006.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Dr. José M. Reyes Álamo 1.  Review: ◦ Statement Labels ◦ Unconditional Jumps ◦ Conditional Jumps.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
1 Modular Programming, Stack Frames, and High-Level Language Interfacing Read Chapters 8 and 12 of textbook.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
1 Logic, Shift, and Rotate Instructions Read Sections 6.2, 7.2 and 7.3 of textbook.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
1 Assembly Language: Function Calls Jennifer Rexford.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Chapter 8 String Operations. 8.1 Using String Instructions.
Practical Session 3.
Stack Operations Dr. Hadi AL Saadi.
C function call conventions and the stack
Assembly Lab 3.
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
(The Stack and Procedures)
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Stack Frames and Advanced Procedures
MIPS Procedure Calls CSE 378 – Section 3.
EECE.3170 Microprocessor Systems Design I
Practical Session 4.
(The Stack and Procedures)
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
3.6 Data transfer Instructions
X86 Assembly Review.
(The Stack and Procedures)
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

1 The Stack and Procedures Chapter 5

2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is placed at the lowest available address followed then by the data  The stack (subject of this chapter) is used for procedure calls and returns  The heap is used for dynamic memory allocation  this is done by calling the OS at run time (possibly via a library function like malloc() or « new » in C++) Code Data Stack Heap (low memory) (high memory) Increasing addresses

3 The Stack  A stack of a certain size is allocated to every process  The stack is used for procedure calls and returns  It is also used by compilers for storing variables and arrays  But the stack size is fixed when the program is loaded in main memory  The stack size cannot be changed at run time  There is always the risk of a stack overflow at run time (if too much data are pushed onto the stack)  If this is the case, the process is terminated and the OS returns a stack fault message  The default stack size is normally large enough for almost all applications but the programmer can choose its size  With Borland we do this by providing a linker option to the bcc32 command. Ex: bcc32 –lS: hello.asm  This will allocate a stack of 2 million bytes

4 The Stack (cont.)  When the program starts to execute, ESP gets loaded with the offset address of the top of the stack  The top of the stack is the memory byte which immediately follows the byte in the stack which is located at the largest available address  The stack is said to be empty when ESP points to the top of the stack  As we push data onto the stack the (unsigned) value in ESP will decrease and ESP will point deeper into the stack  The stack is said to be full when ESP points to the bottom of the stack  The bottom of the stack is the memory byte in the stack which is located at the smallest available address  The PUSH instruction is used to insert (or save) data onto the stack and the POP instruction is use to retrieve this data  PUSH and POP can only be used with either 16-bit or 32-bit operands (8-bits operands are not allowed)

5 The PUSH Instruction  To push data onto the stack, we use: PUSH source  The source operand can be either reg, mem, imm, (or indirect) but it must be 16-bit or 32-bit in size.  Let S be the size (in bytes) of source (S = 2 or 4). The following sequence of events will occur upon execution of PUSH source:  ESP is first decremented by S  Then the content of source will be copied at the location pointed by ESP

6 PUSH Example  Suppose that the stack size is 100h and starts at address 0.  ESP thus contains 100h when the stack is empty (the byte at address 100h is the top of the stack)  Check now the stack and ESP after each of these PUSH: MOV eax, h PUSH ax PUSH eax  By default, an imm operand of PUSH is 32-bit. This can be overridden by the PTR operator: PUSH –1 ;FFFFFFFFh is ;pushed PUSH word ptr –1 ;FFFFh is pushed PUSH byte ptr –1 ;error PUSH qword ptr -1 ; error Addr 100h FFh FEh FDh FCh FBh FAh 30h 40h 10h 20h 30h 40h ESP (Stack empty) After push ax After push eax STACK

7 The POP Instruction  POP undoes the action of PUSH  To pop data from the stack, we use: POP destination  The operand can be either reg, mem (or indirect) but it must be 16-bit or 32-bit in size.  The operand cannot be imm  Let S be the size (in bytes) of destination (S = 2 or 4). The following sequence of events will occur upon execution of POP destination:  The word (if S=2) or dword (if S=4) located at the address contained in ESP is first copied into the destination operand  ESP is then incremented by S

8 POP Example  Suppose that the stack is initially in the following state  i.e.: ESP contains FAh  Here is the stack and ESP after each of these POP POP eax ; eax = h POP ax ;ax = 3040h POP ah ; error  Note that the data remains in the stack: only ESP is changed (incremented) at each POP  Nevertheless, the stack is said to be empty when ESP points to the top of the stack (here 100h) Addr 100h FFh FEh FDh FCh FBh FAh 30h 40h 10h 20h 30h 40h ESP After pop ax After pop eax (initially) STACK

9 Ex: Saving and Restoring Registers  Some registers are automatically used by certain instructions. Example:  EAX is used by getch and other instructions  ECX is used by LOOP and other instructions  The stack provides a convenient way for saving and restoring registers that are needed temporarily  Notice the particular order in which PUSH and POP are used ;save registers push eax push ecx ;read and print 3 chars mov ecx,3 again: getch ;char in eax or al,20h ;upper to ;lower case convt. putch eax ;display loop again ;restore registers pop ecx pop eax

10 Saving and Restoring Registers and Flags  The PUSHA instruction (without operands) pushes EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI on stack and POPA pops the same registers in reverse order. Example: PUSHA ;saves 8 registers onto the stack ;use these registers POPA ;restore initial values of registers  PUSHF and POPF (without operands) pushes and pops the EFLAGS register onto and from the stack. Example: PUSHF ;saves EFLAGS onto the stack ;use instructions which will modify EFLAGS POPF ;restores original EFLAGS register

11 Inverting the Input Line  The stack is a last-in first- out data structure  Items come off the stack in the reverse order that they came in  This program uses this property to read a sequence of characters and display them in reverse order on the next line.386.model flat include csi2121.inc.code main: xor ecx,ecx ;sets count to 0 read_again: getch cmp eax,0ah je display push ax ;push char 16-bit inc ecx ;inc count jmp read_again display: jecxz exit again: pop ax ;pop char 16-bit putch eax loop again exit: ret end

12 Exercise 1  We have the following data segment msg DW ‘a’,’b’,’c’,’d’  Suppose that, initially, ESP contains 100h. Give the hexadecimal value contained in the mentioned registers after executing each instruction in this particular sequence: PUSH msg ;ESP = MOV ax,[esp] ;AX = PUSH msg+2 ;ESP = MOV eax,[esp] ;EAX = PUSH dword ptr msg+3 ;ESP = LEA EAX,MSG POP word ptr [eax] ;ESP = MOV ax,msg ;EAX = POP eax ;EAX = POP ax ;EAX =

13 Procedures  A procedure is formally defined with the PROC and ENDP directives: ProcName PROC... set of instructions... RET ProcName ENDP  To transfer control to the procedure ProcName we do: CALL ProcName  The RET instruction transfers control to the instruction immediately following CALL  In fact, only CALL and RET are required. Hence, a procedure can simply be written as: ProcName:... set of instructions... RET  Just like our main procedure

14 CALL and RET  Upon a CALL to a procedure:  ESP is decremented by 4  The content of EIP is copied at the dword pointed by ESP (Note: the content of EIP is the offset address of the instruction following CALL: where the procedure must return)  The offset address of the first instruction in the called procedure is copied into EIP (this will thus be the next instruction to execute)  Upon a RET from a procedure:  The dword pointed by ESP is copied into EIP  ESP is incremented by 4 (the instruction pointed by EIP is then executed)

15 Illustration of CALL and RET Calling Program Called Procedure main: A5100h: call ProcA 006A5105h: inc eax... ProcA PROC 006A5180h: MOV eax,1... RET ProcA ENDP A 00 ESP CALL pushes the return address onto the stack A 00 ESP RET pops the returned address from the stack into EIP

16 Exercise 2  A program contains the following sequence of instructions: CALL PROC1 MOV BX,AX  The instruction MOV BX,AX is located at the address Ah. In addition, PROC1 starts at address h. Finally, ESP initially contains h.  (A) What is the content, in hexadecimal, of the registers EIP, and ESP just after the execution of the instruction CALL PROC1 (and just before the execution of the 1st instruction of PROC1)?  (B) What is the double word pointed by [ESP]?

17 Passing Arguments to Procedures*  Arguments can be passed to procedures via:  The stack: this is the technique used in HLLs. We will use this technique only in later chapters.  Registers: a much faster way to pass arguments (but very few registers are available). We will start by using this technique.  Global variables: the scope of a variable is the.ASM file into which it is defined. Trivial to do and extremely fast but it is contrary to modular programming practice.  Procedures usually return their results in:  Registers : either the returned value or the address of the returned value (ex: a modified array).  Flags : by modifying one or more flags, a procedure can specify the presence or the absence of a property.

18 Using Procedures  When a procedure returns to the caller it should preserve the content of the registers (except those used to return a value)  Hence, the procedure should first save the content of the registers that it will modify and restore them just before returning to the caller  Caution on stack usage:  ESP points to the return address when entering the procedure. Make sure that this is the case just before executing RET.  This also applies to the main procedure. Make sure to push and pop an equal amount of data before exiting with RET.  Here are examples of programs using procedures:  readstr.asm.html readstr.asm.html  is_alpha.asm.html is_alpha.asm.html