ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
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)
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.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Structures and Linked Lists in C/C++ ICS 51. Structures Definition of structure A datatype in C A notion of record aggregating a set of objects into a.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 21: Calling.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
Functions/Methods in Assembly
Stack Usage with MS Visual Studio Without Stack Protection.
Compiler Construction Code Generation Activation Records
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.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Assembly Language: Function Calls Jennifer Rexford.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Section 5: Procedures & Stacks
Recitation 3: Procedures and the Stack
Stack Operations Dr. Hadi AL Saadi.
Assembly function call convention
Computer Architecture & Operations I
Reading Condition Codes (Cont.)
Credits and Disclaimers
Computer Science 210 Computer Organization
C function call conventions and the stack
CS-401 Computer Architecture & Assembly Language Programming
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Computer Architecture and Assembly Language
Machine-Level Programming 4 Procedures
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Machine-Level Programming III: Procedures Sept 18, 2001
EECE.3170 Microprocessor Systems Design I
Practical Session 4.
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
UNIT V Run Time Environments.
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Computer Organization and Assembly Language
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Credits and Disclaimers
ICS51 Introductory Computer Organization
Implementing Functions: Overview
Presentation transcript:

ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions

2 Push instruction push REGISTER 1.Decrements (by 4) the stack pointer register (ESP) 2.Copies the value in REGISTER to the top of the stack push EDI bottom of the stack (higher address) ESP = ESP - 4 content of the stack and ESP after push

3 Pop instruction pop REGISTER 1. Copies the value at the top of the stack to REGISTER 2. Increments (by 4) the stack pointer register (ESP) pop EDI bottom of the stack (higher address) ESP content of the stack and ESP before pop

4 Control flow instructions (subroutine call/return) a procedure that calls another procedure: caller procedure that has been called: callee main() { … countLowCaseChar(“Hello World”) … }

5 The caller should Before calling a procedure: pass parameters to the procedure: push to the stack in reverse order (the last parameter is pushed first) execute call (to perform the procedure call) After the procedure returns: pop parameters from the stack (to restore the stack to its state before call was performed) the return value is in EAX

6 The callee should At the beginning of procedure: access passed parameters using ESP When the procedure is done: the return value should be placed in EAX execute ret instruction (return to the caller)

7 Example void main() { int result = 0; __asm{ PUSH eax; PUSH ebx; PUSH ecx; // 1. Pass parameter(s) to the procedure MOV ebx, 10; PUSH ebx; // 2. Execute call CALL add_func; // 3. Remove parameter(s) from the stack POP ebx; // 4. EAX has the return value MOV result, eax; POP ecx; POP ebx; POP eax; } __declspec(naked) int add_func(int param1) { __asm{ PUSH ebx; PUSH ecx; // 1. Access param1 MOV ebx, dword ptr[esp+12]; MOV ecx, ebx; ADD ecx, 5; // 2. The return value should be placed in EAX MOV eax, ecx; POP ecx; POP ebx; // 3. Return to the caller RET; }

8 Example (cont.) void main() { int result = 0; __asm{ PUSH eax; PUSH ebx; PUSH ecx; // 1. Pass parameter(s) to the procedure MOV ebx, 10; PUSH ebx; // 2. Execute call CALL add_func; // 3. Remove parameter(s) from the stack POP ebx; // 4. EAX has the return value MOV result, eax; POP ecx; POP ebx; POP eax; } ECXESP EBXESP+4 EAXESP+8 10 (param1)ESP ECXESP+4 EBXESP+8 EAXESP+12 Return AddrESP 10 (param1)ESP+4 ECXESP+8 EBXESP+12 EAXESP+16 ECXESP EBXESP+4 EAXESP+8

Example (cont.) 9 Return AddrESP param1ESP+4 ECXESP+8 EBXESP+12 EAXESP+16 __declspec(naked) int add_func(int param1) { __asm{ PUSH ebx; PUSH ecx; // 1. Access param1 MOV ebx, dword ptr[esp+12]; MOV ecx, ebx; ADD ecx, 5; // 2. The return value should be placed in EAX MOV eax, ecx; POP ecx; POP ebx; // 3. Return to the caller RET; } ECXESP EBXESP+4 Return AddrESP+8 param1ESP+12 ECXESP+16 EBXESP+20 EAXESP+24 Return AddrESP param1ESP+4 ECXESP+8 EBXESP+12 EAXESP+16 param1ESP+4 ECXESP+8 EBXESP+12 EAXESP+16

__declspec(naked) __declspec(naked) tells C compiler not to insert prolog/epilog code: 1.function parameters are not copied from stack to the parameter variables, and local variables are not allocated have to retrieve function parameters from stack 2.registers are not saved to stack in the beginning of the function and are not restored at the end have to do it yourself 3.return statement cannot be used you need to explicitly insert ret Assembly instruction at the end of your function you need to return the value by putting it in EAX 10