Recitation 3: Procedures and the Stack

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
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.
Practical session 7 review. Little – Endian What’s in memory? Section.rodata a: DB ‘hello’, 0x20, ’world’, 10, 0 b: DW ‘hello’, 0x20, ’world’, 10, 0 c:
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
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.
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
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.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
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.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
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.
Recitation 4: The Stack & Lab3 Andrew Faulring Section A 30 September 2002.
1 #include void silly(){ char s[30]; gets(s); printf("%s\n",s); } main(){ silly(); return 0; }
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
Compiler Construction Code Generation Activation Records
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.
Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.
Paradyn Project Paradyn / Dyninst Week Madison, Wisconsin May 2-4, 2011 unstrip: Restoring Function Information to Stripped Binaries Using Dyninst Emily.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Structured Data –struct s / union s –Arrays of structs.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Section 5: Procedures & Stacks
A job ad at a game programming company
Assembly function call convention
Reading Condition Codes (Cont.)
Instruction Set Architecture
C function call conventions and the stack
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Aaron Miller David Cohen Spring 2011
Introduction to Compilers Tim Teitelbaum
Recitation 2 – 2/4/01 Outline Machine Model
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Emily Jacobson and Nathan Rosenblum
Machine-Level Programming 1 Introduction
asum.ys A Y86 Programming Example
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Condition Codes Single Bit Registers
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Assembly Language Programming II: C Compiler Calling Sequences
The Runtime Environment
Machine-Level Programming III: Procedures Sept 18, 2001
EECE.3170 Microprocessor Systems Design I
The Runtime Environment
EECE.3170 Microprocessor Systems Design I
Machine-Level Programming: Introduction
X86 Assembly Review.
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
ICS51 Introductory Computer Organization
Presentation transcript:

Recitation 3: Procedures and the Stack Andrew Faulring 15213 Section A 23 September 2002

Andrew Faulring faulring@cs.cmu.edu Office hours: NSH 2504 (lab) / 2507 (conference room) Thursday 5–6 Lab 2 due Thursday, 11:59pm Look for Lab 3 soon—possible by Tuesday

Today’s Plan Is everyone okay with GDB and Lab2? Procedures and the Stack Homogenous Data: Arrays

Stacks Grows down in memory Stores local variables that cannot fit in registers Stores arguments and return addresses %esp: points to the top value on the stack %ebp: points to a function’s stack frame pushl: decrements, then places value popl: ‘returns’ value, then increments

Stack Frames Abstract partitioning of the stack Each Stack Frame contains the state for a single function instance Recursive functions have multiple Stack Frames—one for each invocation Return Addr Saved Registers Argument Build Old %ebp Local Variables Arguments Caller Frame Frame Pointer (%ebp) Stack Pointer (%esp)

Procedures: Caller Responsibilities Save caller save registers (if necessary) %eax, %ecx, %edx Arguments (pushl) Pushed onto stack In what order? Execute the call instruction Pushes return address (that of the next instruction) onto the stack Return Addr Saved Registers Argument Build Old %ebp Local Variables Arguments

Procedures: Callee Responsibilities (prolog) Save base pointer on stack push %ebp Set base pointer to point to the top of the stack mov %esp,%ebp Save callee save registers %ebx, %esi, %edi push %esi Allocate space for local variables Decrement the stack pointer add 0xFFFFFFF8, %esp Return Addr Saved Registers Argument Build Old %ebp Local Variables Arguments

Procedures: Callee Responsibilities (epilog) Move return value into %eax Restore stack pointer mov %ebp,%esp Restore the base pointer to value for calling function pop %ebp Execute the ret instruction Pops a value from the stack Jumps to the address Return Addr Saved Registers Argument Build Old %ebp Local Variables Arguments

Example 1: add %ebp, %esp int add (int x, int y) { return x+y; } push %ebp mov %esp,%ebp mov 0xc(%ebp),%eax add 0x8(%ebp),%eax mov %ebp,%esp pop %ebp ret y x Return addr %ebp, %esp Saved %ebp

Example 2: fib int fib(int n) { int result; if(n <= 2) result = 1; else result = fib(n-2) + fib(n-1); return result; }

fib: prolog 0x8048420 <fib>: push %ebp mov %esp,%ebp sub $0x10,%esp push %esi push %ebx # first part of body mov 0x8(%ebp),%ebx cmp $0x2,%ebx jg 0x8048437 <fib+23> mov $0x1,%eax jmp 0x8048453 <fib+51> … n Return addr %ebp Saved %ebp 0x10 … %esi %esp %ebx

fib: body 0x8048437 <fib+23> add $0xfffffff4,%esp lea 0xfffffffe(%ebx),%eax push %eax call 0x8048420 <fib> mov %eax,%esi lea 0xffffffff(%ebx),%eax add %esi,%eax … n Return addr %ebp Saved %ebp 0x10 … %esi %esp %ebx … 0xc %eax Return addr %esp %esp … 0xc %eax Return addr

fib: epilog 0x8048453 <fib+51>: lea 0xffffffe8(%ebp),%esp pop %ebx pop %esi mov %ebp,%esp pop %ebp ret … n Return addr %ebp Saved %ebp 0x10 … %esi %esp %ebx … 0xc %eax Return addr %esp … 0xc %eax Return addr

Homogenous Data: Arrays Allocated as contiguous blocks of memory int array[5] = {…} array begins at memory address 40 array[0] 40 + 4*0 = 40 array[3] 40 + 4*3 = 52 array[-1] 40 + 4*-1 = 36 array[15] 40 + 4*15 = 100

Example 3: sum_array int sum_array(int x[], int num) { int i, sum; for(i = 0; i < num; i++) sum += x[i]; return sum; }

sum_array 0x80483f0 <sum_array>: push %ebp 0x80483f1 <sum_array+1>: mov %esp,%ebp 0x80483f3 <sum_array+3>: push %ebx 0x80483f4 <sum_array+4>: mov 0x8(%ebp),%ebx # x 0x80483f7 <sum_array+7>: mov 0xc(%ebp),%ecx # num 0x80483fa <sum_array+10>: xor %eax,%eax # sum 0x80483fc <sum_array+12>: xor %edx,%edx # i 0x80483fe <sum_array+14>: cmp %ecx,%eax # 0 >= num 0x8048400 <sum_array+16>: jge 0x804840a <sum_array+26> 0x8048402 <sum_array+18>: add (%ebx,%edx,4),%eax # sum += x[i] 0x8048405 <sum_array+21>: inc %edx # i++ 0x8048406 <sum_array+22>: cmp %ecx,%edx # i < num 0x8048408 <sum_array+24>: jl 0x8048402 <sum_array+18> 0x804840a <sum_array+26>: pop %ebx 0x804840b <sum_array+27>: mov %ebp,%esp 0x804840d <sum_array+29>: pop %ebp 0x804840e <sum_array+30>: ret

Example 4: linked list typedef struct linked_list { struct linked_list *next; int data; } linked_list; 0x0 next 0x4 data

Example 4: sum_linked_list int sum_linked_list(linked_list *head) { int sum; sum = 0; while(head != NULL) sum += head->data; head = head->next; } return sum;

sum_linked_list 0x8048434 <sum>: push %ebp 0x8048435 <sum+1>: mov %esp,%ebp 0x8048437 <sum+3>: mov 0x8(%ebp),%edx # head 0x804843a <sum+6>: xor %eax,%eax # sum = 0 0x804843c <sum+8>: test %edx,%edx # head == NULL 0x804843e <sum+10>: je 0x8048449 <sum+21> 0x8048440 <sum+12>: add 0x4(%edx),%eax # sum += head->data 0x8048443 <sum+15>: mov (%edx),%edx # head = head->next 0x8048445 <sum+17>: test %edx,%edx # head == NULL 0x8048447 <sum+19>: jne 0x8048440 <sum+12> 0x8048449 <sum+21>: mov %ebp,%esp 0x804844b <sum+23>: pop %ebp 0x804844c <sum+24>: ret

Good luck with Lab2!