15 213 Recitation 3 21102 Outline Stacks Procedures

  • Slides: 27
Download presentation
15 -213 Recitation 3 – 2/11/02 Outline • Stacks & Procedures • Homogenous Data

15 -213 Recitation 3 – 2/11/02 Outline • Stacks & Procedures • Homogenous Data – Arrays – Nested Arrays • Structured Data – structs / unions – Arrays of structs Reminders • Lab 2: Tuesday, 11: 59 Trent e-mail: mmt@cmu. edu Office Hours: Thursday 12: 30 – 2: 00 Wean Hall 3108

Stacks • • Grows down Stores local variables that can’t fit in registers Stores

Stacks • • Grows down Stores local variables that can’t fit in registers Stores arguments and return addresses %esp Stack Pointer – • %ebp – • Base Pointer Points to a function’s stack frame pushl – • Points to the top value on the stack Decrements, then places value popl – ‘Returns’ value, then increments

Stack Frames • • Abstract partitioning of the stack Each Frame contains the state

Stack Frames • • Abstract partitioning of the stack Each Frame contains the state for a single function instant Caller Frame Arguments Frame Pointer (%ebp) Return Addr Old %ebp Saved Registers Local Variables Stack Pointer (%esp) Argument Build

Procedures • call: Caller Responsibilities Arguments (pushl) – • • In what order? Return

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;

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

Problem 1: Answer <main>: push mov sub movl add mov push lea push call

Problem 1: Answer <main>: push mov sub movl add mov push lea push call Old %ebp %esp, %ebp $0 x 18, %esp $0 x 5, -8(%ebp) $0 xfffffffd, -12(%ebp) $0 xfffffffc, %esp -12(%ebp), %eax -8(%ebp), %eax -4(%ebp), %eax <absdiff> %ebp result 5 -3 %esp -3 5 &result Rtn Address %esp

Problem 1: Answer <absdiff>: push mov sub mov cmp jl mov mov sub jmp.

Problem 1: Answer <absdiff>: push mov sub mov cmp jl mov mov sub jmp. L 1 mov mov sub. L 2 mov mov pop ret * %ebp %esp, %ebp $0 x 18, %esp 0 xc(%ebp), %eax 0 x 10(%ebp), %eax. L 1 0 xc(%ebp), %eax 0 x 10(%ebp), %edx %eax, %ecx %edx, %ecx. L 2 0 x 10(%ebp), %eax 0 xc(%ebp), %edx %eax, %ecx %edx, %ecx 0 x 8(%ebp), %eax %ecx, (%eax) %ebp, %esp %ebp * * %esp -3 5 &result Rtn Address Old %ebp %esp

Problem 1: Answer Old %ebp <main>: …. . add mov push call mov pop

Problem 1: Answer Old %ebp <main>: …. . add mov push call mov pop ret %ebp result $0 x 10, %esp -4(%ebp), %eax -12(%ebp), %eax -8(%ebp), %eax $0 x 80484 d 8 <printf> %ebp, %esp %ebp 5 -3 %esp result -3 5 $0 x 80484 d 8 Rtn Address %esp

Problem 2: Recursion With the following code, what does the stack look like if

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 1 1 ret oldebp 0 1 0 ret oldebp ;

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

Homogenous Data: Arrays • Allocated as contiguous blocks of memory Address Computation Examples •

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

Problem 3: Arrays get_sum: pushl %ebp movl %esp, %ebp pushl %ebx . L 6:

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

Problem 3: Answer get_sum: pushl %ebp movl %esp, %ebp pushl %ebx . L 6:

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

Problem 4: Nested arrays int main(int argc, char **argv) { int i, j, r=0;

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] != '') { r ^= argv[i][j]; j++; } } return r; }

Problem 4: Answer. L 9: main: pushl %ebp movl %esp, %ebp pushl %edi pushl

Problem 4: Answer. L 9: 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. L 4. L 6: xorl %ecx, %ecx movl (%edi, %ebx, 4), %eax cmpb $0, (%eax) je. L 5 movl %eax, %edx movsbl (%ecx, %edx), %eax xorl %eax, %esi incl %ecx cmpb $0, (%ecx, %edx) jne. L 9. L 5: incl %ebx cmpl 8(%ebp), %ebx jl. L 6. L 4: movl popl movl popl ret %esi, %eax %ebx %esi %edi %ebp, %esp %ebp

structs and unions • Organize data • structs store multiple elements, unions store a

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

Alignment • • Contiguous areas of memory Each block is aligned – Size is

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

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

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

Problem 5: Structs. LC 0: . string. LC 1: . string. LC 2: .

Problem 5: Structs. LC 0: . string. LC 1: . string. LC 2: . string. align 32. LC 3: . string main: pushl movl #define MAX_STRING 20 "Jack" "Black" "12345 ZXY" "Name : %s %sn. ID : %sn. Age : %in“ #struct #{ # char # int #}; student first [ MAX_STRING ]; last [ MAX_STRING ]; id [ MAX_STRING ]; age; %ebp %esp, %ebp subl pushl leal pushl call $88, %esp $4, %esp $5 $. LC 0 -72(%ebp), %eax strncpy addl subl pushl leal addl pushl call $16, %esp $4, %esp $6 $. LC 1 -72(%ebp), %eax $20, %eax strncpy # struct student s 1; # struct student *s 2; # strncpy(s 1. first, f. Name, # sizeof(f. Name)); # strncpy(s 1. last, l. Name, # sizeof(l. Name));

Problem 5: Structs addl subl pushl leal addl pushl call $16, %esp $4, %esp

Problem 5: Structs addl subl pushl leal addl pushl call $16, %esp $4, %esp $9 $. LC 2 -72(%ebp), %eax $40, %eax strncpy addl movl $16, %esp $19, -12(%ebp) leal movl -72(%ebp), %eax, -76(%ebp) subl movl pushl movl addl pushl call $12, %esp -76(%ebp), %eax 60(%eax) -76(%ebp), %eax $40, %eax -76(%ebp), %eax $20, %eax -76(%ebp) $. LC 3 printf addl movl leave ret $32, %esp $0, %eax strncpy(s 1. id, ID, sizeof(ID)); # s 1. age = AGE; # s 2 = &s 1; # printf("Name : %s %sn. ID : %sn. Age : %in", # s 2 ->first, s 2 ->last, s 2 ->id, s 2 ->age); # clean up and exit

Problem 5: Answer int main ( int argc, char** argv ) { struct student

Problem 5: Answer int main ( int argc, char** argv ) { struct student s 1; struct student *s 2; strncpy ( s 1. first, f. Name, sizeof ( f. Name ) ); strncpy ( s 1. last , l. Name, sizeof ( l. Name ) ); strncpy ( s 1. id , ID , sizeof ( ID ) ); s 1. age = AGE; s 2 = &s 1; printf ( "Name : %s %sn. ID : %sn. Age : %in", s 2 -> first, s 2 -> last, s 2 -> id, s 2 -> age ); return 0; }

Problem 6: Arrays of structs. LC 0: . string "%i : %s - “.

Problem 6: Arrays of structs. LC 0: . string "%i : %s - “. LC 1: . string "Has no partnern“. LC 2: . string "Partnered with : %s n“. align 32. LC 3: . string "Claims parter: %s, but not mutualn“ partner_check: pushl %ebp movl %esp, %ebp subl $20, %esp pushl %ebx nop movl $0, -4(%ebp). p 2 align 4, , 7. L 3: cmpl $5, -4(%ebp) jle. L 6 jmp. L 4. p 2 align 4, , 7. L 6: 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

Problem 6: Arrays of structs leal 0(, %edx, 4), %eax movl %eax, %edx addl

Problem 6: Arrays of structs leal 0(, %edx, 4), %eax movl %eax, %edx addl 8(%ebp), %edx leal 40(%edx), %eax pushl %eax movl -4(%ebp), %eax pushl $. LC 0 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. L 7 addl $-12, %esp pushl $. LC 1 call printf addl $16, %esp jmp. L 5. p 2 align 4, , 7 < < < < < printf ( "%i : %s - ", j, class[j]. ID ); < < printf ( "Has no partnern" );

Problem 6: Arrays of structs. L 7: movl -4(%ebp), %eax movl %eax, %ecx movl

Problem 6: Arrays of structs. L 7: 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. L 9 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 $. LC 2 call printf addl $16, %esp jmp. L 5. p 2 align 4, , 7 < < < < printf ( "Partnered with : %s n", class[j]. partner->ID );

Problem 6: Arrays of structs. L 9: addl $-8, %esp movl -4(%ebp), %eax movl

Problem 6: Arrays of structs. L 9: 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 $. LC 3 call printf addl $16, %esp. L 10: . L 8: . L 5: incl -4(%ebp) jmp. L 3. p 2 align 4, , 7. L 4: . L 2: movl -24(%ebp), %ebx movl %ebp, %esp popl %ebp ret < < < < printf ( "Claims parter: %s, but not mutualn", < class[j]. partner->ID );

Problem 6: Answer void partner_check ( struct student * class ) { int j;

Problem 6: Answer 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 partnern" ); else if ( (class+j)->partner == (class+j) ) printf ( "Partnered with : %s n", class[j]. partner->ID ); else printf ( "Claims parter: %s, but not mutualn", class[j]. partner->ID ); } }