15-213 Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Structured Data –struct s / union s –Arrays of structs.

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

Compiladores I- 1 - COMPARACION PROGRAMA FUENTE Y ENSAMBLADOR GENERADO.
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
Carnegie Mellon Instructors: Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures : Introduction.
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 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
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.
Machine-Level Programming I: Introduction Apr. 14, 2008 Topics Assembly Programmer’s Execution Model Accessing Information Registers Memory Arithmetic.
Lecture 22 Code Generation Topics Arrays Code Generation Readings: 9 April 10, 2006 CSCE 531 Compiler Construction.
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 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
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.
Carnegie Mellon Machine-Level Programming III: Switch Statements, 1-D Array and IA32 Procedures Lecture, Mar. 7, 2013 These slides are from
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.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
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.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
MACHINE-LEVEL PROGRAMMING III: SWITCH STATEMENTS AND IA32 PROCEDURES.
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.
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.)
C function call conventions and the stack
Recitation 2 – 2/11/02 Outline Stacks & Procedures
Homework In-line Assembly Code Machine Language
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
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:
Instructors: Majd Sakr and Khaled Harras
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 I: Introduction
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 Structured Data –struct s / union s –Arrays of structs James Wilson Office Hours: Friday 1:30 – 3:00 Wean Cluster 52xx 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: 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 5 -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(1, 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 1 ; first ret ; call fib oldebp ; <--- ebp of fib’s caller 0 ; <--- push result 1 ; <--- next + result 0 ; <--- n - 1 ret ; call fib oldebp

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

struct s and union s Organize data struct s store multiple elements, union s store a single element at a time Members of a union change how you look at data union s used for mutually exclusive data

Alignment Contiguous areas of memory Each block is aligned –Size is a multiple of a base value –“Base value” is the largest alignment of data types in structure Why? –Efficient load/store from memory –Virtual Memory paging This applies to any variable type

Structure of a struct Find largest alignment –Size of structure must be a multiple of this For each element e (top to bottom): –Find alignment of e Starting offset must be a multiple of this –Pad previous element with empty space until alignment matches –Allocate alignment worth of space to e Pad last element with empty space until alignment of structure matches Note this isn’t optimal!

Structure of a union Find largest alignment –Size of structure must be a multiple of this Allocate this much space Examples struct one {union two { int i; double d; char c[2];}

Problem 5: Structs #define MAX_STRING 20 #struct student #{ # char first [ MAX_STRING ]; # char last [ MAX_STRING ]; # char id [ MAX_STRING ]; # int age; #}; # struct student s1; # struct student *s2; # strncpy(s1.first, fName, # sizeof(fName)); # strncpy(s1.last, lName, # sizeof(lName));.LC0:.string "Jack".LC1:.string "Black".LC2:.string "12345ZXY".align 32.LC3:.string "Name : %s %s\nID : %s\nAge : %i\n“ main: pushl %ebp movl %esp, %ebp subl $88, %esp subl $4, %esp pushl $5 pushl $.LC0 leal -72(%ebp), %eax pushl %eax call strncpy addl $16, %esp subl $4, %esp pushl $6 pushl $.LC1 leal -72(%ebp), %eax addl $20, %eax pushl %eax call strncpy

Problem 5: Structs addl$16, %esp subl$4, %esp pushl$9 pushl$.LC2 leal-72(%ebp), %eax addl$40, %eax pushl%eax callstrncpy addl$16, %esp movl$19, -12(%ebp) leal-72(%ebp), %eax movl%eax, -76(%ebp) subl$12, %esp movl-76(%ebp), %eax pushl60(%eax) movl-76(%ebp), %eax addl$40, %eax pushl%eax movl-76(%ebp), %eax addl$20, %eax pushl%eax pushl-76(%ebp) pushl$.LC3 callprintf addl$32, %esp movl$0, %eax leave ret strncpy(s1.id, ID, sizeof(ID)); # s1.age = AGE; # s2 = &s1; # printf("Name : %s %s\nID : %s\nAge : %i\n", # s2->first, s2->last, s2->id, s2 ->age); # clean up and exit

Problem 5: Answer int main ( int argc, char** argv ) { struct student s1; struct student *s2; strncpy ( s1.first, fName, sizeof ( fName ) ); strncpy ( s1.last, lName, sizeof ( lName ) ); strncpy ( s1.id, ID, sizeof ( ID ) ); s1.age = AGE; s2 = &s1; printf ( "Name : %s %s\nID : %s\nAge : %i\n", s2 -> first, s2 -> last, s2 -> id, s2 -> age ); return 0; }

Problem 6: Arrays of structs.LC0:.string "%i : %s - “.LC1:.string "Has no partner\n“.LC2:.string "Partnered with : %s \n“.align 32.LC3:.string "Claims parter: %s, but not mutual\n“ partner_check: pushl %ebp movl %esp,%ebp subl $20,%esp pushl %ebx nop movl $0,-4(%ebp).p2align 4,,7.L3: cmpl $5,-4(%ebp) jle.L6 jmp.L4.p2align 4,,7.L6: addl $-4,%esp movl -4(%ebp),%eax movl %eax,%ecx movl %ecx,%edx sall $4,%edx addl %eax,%edx #define MAX_STRING 20 #define MAX_STUDENTS 6 #struct student #{ # char first [ MAX_STRING ]; # char last [ MAX_STRING ]; # char id [ MAX_STRING ]; # int age; # struct student *partner; #}; # Lines with < symbols are moving # data for the printf command at # the end of the < block. left # in for informational purposes <

leal 0(,%edx,4),%eax movl %eax,%edx addl 8(%ebp),%edx leal 40(%edx),%eax pushl %eax movl -4(%ebp),%eax pushl %eax pushl $.LC0 call printf addl $16,%esp movl -4(%ebp),%eax movl %eax,%ecx movl %ecx,%edx sall $4,%edx addl %eax,%edx leal 0(,%edx,4),%eax movl 8(%ebp),%edx cmpl $0,64(%edx,%eax) jne.L7 addl $-12,%esp pushl $.LC1 call printf addl $16,%esp jmp.L5.p2align 4,,7 < < printf ( "%i : %s - ", j, class[j].ID ); < < printf ( "Has no partner\n" ); Problem 6: Arrays of structs

.L7: movl -4(%ebp),%eax movl %eax,%ecx movl %ecx,%edx sall $4,%edx addl %eax,%edx leal 0(,%edx,4),%eax movl 8(%ebp),%edx movl 64(%edx,%eax),%eax movl -4(%ebp),%edx movl %edx,%ebx movl %ebx,%ecx sall $4,%ecx addl %edx,%ecx leal 0(,%ecx,4),%edx movl %edx,%ecx addl 8(%ebp),%ecx cmpl %ecx,64(%eax) jne.L9 addl $-8,%esp movl -4(%ebp),%eax movl %eax,%ecx movl %ecx,%edx sall $4,%edx addl %eax,%edx leal 0(,%edx,4),%eax movl 8(%ebp),%edx movl 64(%edx,%eax),%eax addl $40,%eax pushl %eax pushl $.LC2 call printf addl $16,%esp jmp.L5.p2align 4,,7 < < printf ( "Partnered with : %s \n", ID ); Problem 6: Arrays of structs

.L9: addl $-8,%esp movl -4(%ebp),%eax movl %eax,%ecx movl %ecx,%edx sall $4,%edx addl %eax,%edx leal 0(,%edx,4),%eax movl 8(%ebp),%edx movl 64(%edx,%eax),%eax addl $40,%eax pushl %eax pushl $.LC3 call printf addl $16,%esp.L10:.L8:.L5: incl -4(%ebp) jmp.L3.p2align 4,,7.L4:.L2: movl -24(%ebp),%ebx movl %ebp,%esp popl %ebp ret < < printf ( "Claims parter: %s, but not mutual\n", ID ); Problem 6: Arrays of structs

void partner_check ( struct student * class ) { int j; for (j = 0; j < MAX_STUDENTS; j++ ) { printf ( "%i : %s - ", j, class[j].ID ); if ( (class+j)->partner == NULL ) printf ( "Has no partner\n" ); else if ( (class+j)->partner->partner == (class+j) ) printf ( "Partnered with : %s \n", class[j].partner->ID ); else printf ( "Claims parter: %s, but not mutual\n", class[j].partner->ID ); } Problem 6: Answer