1 / 27

15-213 Recitation 2 – 2/11/02

15-213 Recitation 2 – 2/11/02. Outline Stacks & Procedures Homogenous Data Arrays Nested Arrays Structured Data struct s / union s Arrays of structs. James Wilson e-mail: wilson2@andrew.cmu.edu Office Hours: Friday 1:30 – 3:00 Wean Cluster 52xx. Reminders Lab 2: Tuesday, 11:59.

lalo
Download Presentation

15-213 Recitation 2 – 2/11/02

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 15-213 Recitation 2 – 2/11/02 Outline • Stacks & Procedures • Homogenous Data • Arrays • Nested Arrays • Structured Data • structs / unions • Arrays of structs James Wilson e-mail: wilson2@andrew.cmu.edu Office Hours: Friday 1:30 – 3:00 Wean Cluster 52xx • Reminders • Lab 2: Tuesday, 11:59

  2. 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

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

  4. 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

  5. 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; }

  6. Problem 1: Answer Old %ebp %ebp <main>: 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 <absdiff> result 5 -3 %esp -3 5 &result Rtn Address %esp

  7. Problem 1: Answer <absdiff>: 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 Old %ebp %ebp %esp

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

  9. 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); }

  10. 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

  11. 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

  12. 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

  13. Problem 3: Answer 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 int get_sum(int * array, int size) { int sum = 0; int i=0; for (i=0; i<size; i++) sum += array[i]; return sum; }

  14. 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; }

  15. Problem 4: Answer .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 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

  16. 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

  17. 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

  18. 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!

  19. 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; int i; double d; double d; char c[2]; char c[2]; } }

  20. 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

  21. Problem 5: Structs 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 addl $16, %esp subl $4, %esp pushl $9 pushl $.LC2 leal -72(%ebp), %eax addl $40, %eax pushl %eax call strncpy addl $16, %esp movl $19, -12(%ebp) leal -72(%ebp), %eax movl %eax, -76(%ebp) subl $12, %esp movl -76(%ebp), %eax pushl 60(%eax) movl -76(%ebp), %eax addl $40, %eax pushl %eax movl -76(%ebp), %eax addl $20, %eax pushl %eax pushl -76(%ebp) pushl $.LC3 call printf addl $32, %esp movl $0, %eax leave ret

  22. 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; }

  23. Problem 6: Arrays of structs #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 < < < < < < .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

  24. 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 %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" );

  25. 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", < class[j].partner->ID );

  26. 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", < class[j].partner->ID );

  27. 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 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 ); } }

More Related