15-213 Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
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.
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.
Y86 Processor State Program Registers
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Lee CSCE 312 TAMU 1 Based on slides provided by Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures Instructor:
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
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.
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.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Machine-Level Programming 1 Introduction Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic operations.
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.
Recitation 2 – 2/4/02 Outline Floating Point Typecasting
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
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.
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.
Recitation 3: Procedures and the Stack
A job ad at a game programming company
Assembly function call convention
Reading Condition Codes (Cont.)
Machine-Level Programming 2 Control Flow
C function call conventions and the stack
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Homework In-line Assembly Code Machine Language
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.
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
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 5 Structured Data
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
Machine-Level Programming 2 Control Flow
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
The Runtime Environment
Machine-Level Programming: Introduction
Machine-Level Representation of Programs II
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
Presentation transcript:

Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday 1:30 – 3:00 Wean Hall 3108 Reminders Lab 2: Tuesday, 11:59

Stacks Grows down Stores local variables that can’t fit in registers Stores arguments and return addresses %esp Stack Pointer –Points to the top value on the stack %ebp Base Pointer –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 Frame contains the state for a single function instant Stack Pointer ( %esp ) Frame Pointer ( %ebp ) Return Addr Saved Registers Argument Build Old %ebp Local Variables Arguments Caller Frame

Procedures call: Caller Responsibilities Arguments( pushl ) –In what order? Return Address(done by call ) ret : Callee Responsibilities Save Registers (especially %ebp ) Set up Stack Frame Return value in %eax

Problem 1: Call Chain void absdiff(int *result, int x, int y) { int z; if (x >= y) z = x - y; else z = y - x; *result = z; return; } int main() { int result; int x,y; x = 5; y = -3; absdiff(&result, x, y); printf("|(%d) - (%d)| = %d\n", x, y, result); return 0; }

Problem 1: Call Chain : push %ebp mov %esp,%ebp sub $0x18,%esp mov 0xc(%ebp),%eax cmp 0x10(%ebp),%eax jl.L1 mov 0xc(%ebp),%eax mov 0x10(%ebp),%edx mov %eax,%ecx sub %edx,%ecx jmp.L2.L1 mov 0x10(%ebp),%eax mov 0xc(%ebp),%edx mov %eax,%ecx sub %edx,%ecx.L2 mov 0x8(%ebp),%eax mov %ecx,(%eax) mov %ebp,%esp pop %ebp ret : push %ebp mov %esp,%ebp sub $0x18,%esp movl $0x5,-8(%ebp) movl $0xfffffffd, -12(%ebp) add $0xfffffffc,%esp mov -12(%ebp),%eax push %eax mov -8(%ebp),%eax push %eax lea -4(%ebp),%eax push %eax call add $0x10, %esp mov -4(%ebp),%eax push %eax mov -12(%ebp),%eax push %eax mov -8(%ebp),%eax push %eax push $0x80484d8 call mov %ebp,%esp pop %ebp ret

Problem 1: Answer : push %ebp mov %esp,%ebp sub $0x18,%esp movl $0x5,-8(%ebp) movl $0xfffffffd, -12(%ebp) add $0xfffffffc,%esp mov -12(%ebp),%eax push %eax mov -8(%ebp),%eax push %eax lea -4(%ebp),%eax push %eax call Old %ebp result x = 5 y = -3 %esp -3 5 &result Rtn Address %esp %ebp

Problem 1: Answer : push %ebp mov %esp,%ebp sub $0x18,%esp mov 0xc(%ebp),%eax cmp 0x10(%ebp),%eax jl.L1 mov 0xc(%ebp),%eax mov 0x10(%ebp),%edx mov %eax,%ecx sub %edx,%ecx jmp.L2.L1 mov 0x10(%ebp),%eax mov 0xc(%ebp),%edx mov %eax,%ecx sub %edx,%ecx.L2 mov 0x8(%ebp),%eax mov %ecx,(%eax) mov %ebp,%esp pop %ebp ret %esp -3 5 &result Rtn Address %ebp Old %ebp %esp ******

Problem 1: Answer : ….. add $0x10, %esp mov -4(%ebp),%eax push %eax mov -12(%ebp),%eax push %eax mov -8(%ebp),%eax push %eax push $0x80484d8 call mov %ebp,%esp pop %ebp ret Old %ebp result 5 -3 %esp result -3 5 %ebp 0x80484d8 Rtn Address %esp

Problem 2: Recursion With the following code, what does the stack look like if we call fib(2, 1, 0) and reach the point where if(n==0) holds true? int fib(int n, int next, int result) { if(n == 0) return result; return fib(n - 1, next + result, next); }

Problem 2: Answer 0 ; third argument to fib 1 ; second 2 ; first ret ; call fib(2,1,0) oldebp ; <--- ebp of fib’s caller 1 ; <--- push next 1 ; <--- next + result 1 ; <--- n - 1 ret ; call fib (1, 1, 1) oldebp ; <--- ebp of fib’s 2 ; <--- push next 3 ; <--- push next + result 0 ; <--- push n-1 ret ; call fib (0, 3, 2)

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

Problem 3: Arrays get_sum: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx # ebx = 1st arg movl 12(%ebp),%ecx # ecx = 2nd arg xorl %eax,%eax # eax = 0 movl %eax,%edx # edx = 0 cmpl %ecx,%eax # jge.L4 # if (ecx >= 0) goto L4.L6: addl (%ebx,%edx,4),%eax # eax += Mem[ebx+edx*4] incl %edx # edx ++ cmpl %ecx,%edx # jl.L6 # if (edx < ecx) goto L6.L4: popl %ebx movl %ebp,%esp popl %ebp ret

Problem 3: Answer int get_sum(int * array, int size) { int sum = 0; int i=0; for (i=0; i<size; i++) sum += array[i]; return sum; } get_sum: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx movl 12(%ebp),%ecx xorl %eax,%eax movl %eax,%edx cmpl %ecx,%eax jge.L4.L6: addl (%ebx,%edx,4),%eax incl %edx cmpl %ecx,%edx jl.L6.L4: popl %ebx movl %ebp,%esp popl %ebp ret

Problem 4: Nested arrays int main(int argc, char **argv) { int i,j,r=0; for (i=0; i<argc; i++) { j=0; while(argv[i][j] != '\0') { r ^= argv[i][j]; j++; } return r; }

Problem 4: Answer main: pushl %ebp movl %esp,%ebp pushl %edi pushl %esi pushl %ebx movl 12(%ebp),%edi xorl %esi,%esi xorl %ebx,%ebx cmpl 8(%ebp),%esi jge.L4.L6: xorl %ecx,%ecx movl (%edi,%ebx,4),%eax cmpb $0,(%eax) je.L5 movl %eax,%edx.L9: movsbl (%ecx,%edx),%eax xorl %eax,%esi incl %ecx cmpb $0,(%ecx,%edx) jne.L9.L5: incl %ebx cmpl 8(%ebp),%ebx jl.L6.L4: movl %esi,%eax popl %ebx popl %esi popl %edi movl %ebp,%esp popl %ebp ret