15-213 Recitation 2 – 2/11/02

27
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: [email protected] Office Hours: Friday 1:30 – 3:00 Wean Cluster 52xx Reminders Lab 2: Tuesday, 11:59

description

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: [email protected] Office Hours: Friday 1:30 – 3:00 Wean Cluster 52xx. Reminders Lab 2: Tuesday, 11:59. - PowerPoint PPT Presentation

Transcript of 15-213 Recitation 2 – 2/11/02

Page 1: 15-213 Recitation 2 – 2/11/02

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:

[email protected]

Office Hours:

Friday 1:30 – 3:00

Wean Cluster 52xx

Reminders• Lab 2: Tuesday,

11:59

Page 2: 15-213 Recitation 2 – 2/11/02

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

Page 3: 15-213 Recitation 2 – 2/11/02

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

SavedRegisters

ArgumentBuild

Old %ebp

LocalVariables

Arguments

CallerFrame

Page 4: 15-213 Recitation 2 – 2/11/02

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

Page 5: 15-213 Recitation 2 – 2/11/02

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

Page 6: 15-213 Recitation 2 – 2/11/02

Problem 1: Answer

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

Old %ebp

result

5

-3

%esp

-3

5

&result

Rtn Address %esp

%ebp

Page 7: 15-213 Recitation 2 – 2/11/02

Problem 1: Answer<absdiff>:

push %ebpmov %esp,%ebpsub $0x18,%espmov 0xc(%ebp),%eaxcmp 0x10(%ebp),%eaxjl .L1 mov 0xc(%ebp),%eaxmov 0x10(%ebp),%edxmov %eax,%ecxsub %edx,%ecxjmp .L2

.L1mov 0x10(%ebp),%eaxmov 0xc(%ebp),%edxmov %eax,%ecxsub %edx,%ecx

.L2mov 0x8(%ebp),%eaxmov %ecx,(%eax)mov %ebp,%esppop %ebpret

%esp

-3

5

&result

Rtn Address

%ebpOld %ebp

%esp

*

*

*

Page 8: 15-213 Recitation 2 – 2/11/02

Problem 1: Answer

<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

Old %ebp

result

5

-3

%esp

result

-3

5

%ebp

$0x80484d8

Rtn Address

%esp

Page 9: 15-213 Recitation 2 – 2/11/02

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

Page 10: 15-213 Recitation 2 – 2/11/02

Problem 2: Answer0 ; third argument to fib1 ; second1 ; firstret ; call fiboldebp ; <--- ebp of fib’s caller0 ; <--- push result1 ; <--- next + result0 ; <--- n - 1ret ; call fiboldebp

Page 11: 15-213 Recitation 2 – 2/11/02

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 = 40cmu[3] 40 + 4*3 = 52cmu[-1] 40 + 4*-1 = 36cmu[15] 40 + 4*15 = 100

Page 12: 15-213 Recitation 2 – 2/11/02

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

Page 13: 15-213 Recitation 2 – 2/11/02

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

Page 14: 15-213 Recitation 2 – 2/11/02

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

Page 15: 15-213 Recitation 2 – 2/11/02

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

Page 16: 15-213 Recitation 2 – 2/11/02

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

Page 17: 15-213 Recitation 2 – 2/11/02

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

Page 18: 15-213 Recitation 2 – 2/11/02

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!

Page 19: 15-213 Recitation 2 – 2/11/02

Structure of a union

• Find largest alignment– Size of structure must be a multiple of this

• Allocate this much space

Examplesstruct one { union two { int i; int i; double d; double d; char c[2]; char c[2];} }

Page 20: 15-213 Recitation 2 – 2/11/02

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

Page 21: 15-213 Recitation 2 – 2/11/02

Problem 5: Structs addl $16, %esp

subl $4, %esppushl $9pushl $.LC2leal -72(%ebp), %eaxaddl $40, %eaxpushl %eaxcall strncpy

addl $16, %espmovl $19, -12(%ebp)

leal -72(%ebp), %eaxmovl %eax, -76(%ebp)

subl $12, %espmovl -76(%ebp), %eaxpushl 60(%eax)movl -76(%ebp), %eaxaddl $40, %eaxpushl %eaxmovl -76(%ebp), %eaxaddl $20, %eaxpushl %eaxpushl -76(%ebp)pushl $.LC3call printf

addl $32, %espmovl $0, %eaxleaveret

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

Page 22: 15-213 Recitation 2 – 2/11/02

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

Page 23: 15-213 Recitation 2 – 2/11/02

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

<<<<<<

Page 24: 15-213 Recitation 2 – 2/11/02

leal 0(,%edx,4),%eaxmovl %eax,%edxaddl 8(%ebp),%edxleal 40(%edx),%eaxpushl %eaxmovl -4(%ebp),%eaxpushl %eaxpushl $.LC0call printfaddl $16,%espmovl -4(%ebp),%eaxmovl %eax,%ecxmovl %ecx,%edxsall $4,%edxaddl %eax,%edxleal 0(,%edx,4),%eaxmovl 8(%ebp),%edxcmpl $0,64(%edx,%eax)jne .L7addl $-12,%esppushl $.LC1call printfaddl $16,%espjmp .L5.p2align 4,,7

<<<<<<<<<< printf ( "%i : %s - ", j, class[j].ID );

<< printf ( "Has no partner\n" );

Problem 6: Arrays of structs

Page 25: 15-213 Recitation 2 – 2/11/02

.L7:movl -4(%ebp),%eaxmovl %eax,%ecxmovl %ecx,%edxsall $4,%edxaddl %eax,%edxleal 0(,%edx,4),%eaxmovl 8(%ebp),%edxmovl 64(%edx,%eax),%eaxmovl -4(%ebp),%edxmovl %edx,%ebxmovl %ebx,%ecxsall $4,%ecxaddl %edx,%ecxleal 0(,%ecx,4),%edxmovl %edx,%ecxaddl 8(%ebp),%ecxcmpl %ecx,64(%eax)jne .L9addl $-8,%espmovl -4(%ebp),%eaxmovl %eax,%ecxmovl %ecx,%edxsall $4,%edxaddl %eax,%edxleal 0(,%edx,4),%eaxmovl 8(%ebp),%edxmovl 64(%edx,%eax),%eaxaddl $40,%eaxpushl %eaxpushl $.LC2call printfaddl $16,%espjmp .L5.p2align 4,,7

<<<<<<<<<<<<< printf ( "Partnered with : %s \n",< class[j].partner->ID );

Problem 6: Arrays of structs

Page 26: 15-213 Recitation 2 – 2/11/02

.L9:addl $-8,%espmovl -4(%ebp),%eaxmovl %eax,%ecxmovl %ecx,%edxsall $4,%edxaddl %eax,%edxleal 0(,%edx,4),%eaxmovl 8(%ebp),%edxmovl 64(%edx,%eax),%eaxaddl $40,%eaxpushl %eaxpushl $.LC3call printfaddl $16,%esp

.L10:

.L8:

.L5:incl -4(%ebp)jmp .L3.p2align 4,,7

.L4:

.L2:movl -24(%ebp),%ebxmovl %ebp,%esppopl %ebpret

<<<<<<<<<<<<<<<< printf ( "Claims parter: %s, but not mutual\n",< class[j].partner->ID );

Problem 6: Arrays of structs

Page 27: 15-213 Recitation 2 – 2/11/02

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