Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early.

Similar presentations


Presentation on theme: "Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early."— Presentation transcript:

1 Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early Minglong Shao E-mail: shaoml+213@cs.cmu.edu Office hours: Thursdays 5-6PM Wean Hall 1315

2 Recursive procedure example: Fibonacci 0x8048420 push %ebp 0x8048421 mov %esp,%ebp 0x8048423 sub $0x10,%esp 0x8048426 push %esi 0x8048427 push %ebx 0x8048428 mov 0x8(%ebp),%ebx 0x804842b cmp $0x2,%ebx 0x804842e jg 0x8048437 0x8048430 mov $0x1,%eax 0x8048435 jmp 0x8048453 0x8048437 add $0xfffffff4,%esp 0x804843a lea 0xfffffffe(%ebx),%eax 0x804843d push %eax 0x804843e call 0x8048420 0x8048443 mov %eax,%esi 0x8048445 add $0xfffffff4,%esp 0x8048448 lea 0xffffffff(%ebx),%eax 0x804844b push %eax 0x804844c call 0x8048420 0x8048451 add %esi,%eax 0x8048453 lea 0xffffffe8(%ebp),%esp 0x8048456 pop %ebx 0x8048457 pop %esi 0x8048458 mov %ebp,%esp 0x804845a pop %ebp 0x804845b ret int fibo (int n) { int result; if (n <= 2) result = 1; else result = fibo(n-2) + fibo(n-1); return result; }

3 Stack Frame 0x8048420 push %ebp 0x8048421 mov %esp,%ebp 0x8048423 sub $0x10,%esp 0x8048426 push %esi 0x8048427 push %ebx... 0x8048453 lea 0xffffffe8(%ebp),%esp 0x8048456 pop %ebx 0x8048457 pop %esi 0x8048458 mov %ebp,%esp 0x804845a pop %ebp 0x804845b ret old %ebp old %esi old %ebx rtrn addr x %ebp %esp Stack at this point

4 Write Comments For Body 0x8048428 mov 0x8(%ebp),%ebx# ebx = x 0x804842b cmp $0x2,%ebx# if (x>2) 0x804842e jg 0x8048437# goto L1 0x8048430 mov $0x1,%eax# eax = 1 0x8048435 jmp 0x8048453# goto L2 0x8048437 add $0xfffffff4,%esp# L1: 0x804843a lea 0xfffffffe(%ebx),%eax# 0x804843d push %eax# push x-2 0x804843e call 0x8048420 # call fibo 0x8048443 mov %eax,%esi# esi = eax 0x8048445 add $0xfffffff4,%esp# 0x8048448 lea 0xffffffff(%ebx),%eax# 0x804844b push %eax# push x-1 0x804844c call 0x8048420 # call fibo 0x8048451 add %esi,%eax# eax += esi 0x8048453...# L2:

5 Stack Changes of fibo(3) fibo(3) %ebp %esp call fibo(1) fibo(3) fibo(1) %ebp %esp 1 0x8048443 1 fibo(3) %ebp %esp return

6 Stack Changes of fibo(3) call fibo(2) fibo(3) fibo(2) %ebp %esp 1 2 1 fibo(3) %ebp %esp return 1 fibo(3) %ebp %esp 0x8048451 2

7 Arrays Allocated as contiguous blocks of memory 15213 404448525660 Address Computation Example cmu[0]40+0*sizeof(int) = 40 cmu[3]40+3*sizeof(int) = 52 cmu[i]40+i*sizeof(int) = 40 + 4*I “x = cmu[i]” in assembly code #%edx = cmu, %eax = i movl (%edx, %eax, 4), %eax#%eax stores x int cmu[5] = {…}; /*cmu begins at address 40*/

8 Nested arrays Declaration – T A[R][C]; – Array of data type T – A[i] is an array of C elements – R rows – C columns – Type T element requires K bytes Array Size – R * C * K bytes Arrangement – Row-major ordering

9 Nested arrays: in terms of Memory A [i] [0] A [i] [C-1] A[i] A [R-1] [0] A [R-1] [C-1] A[R-1] A A [0] A [0] [C-1] A[0] int A[R][C]; A+i*C*4A+(R-1)*C*4 A[0][0]A[0][1]…A[0][c-1] ………… A[i][0]A[i][1]…A[i][c-1] ………… A[R-1][0]A[R-1][1]…A[R-1][C-1] Row-major ordering  Like a matrix  Starting address of A[i]

10 Nested arrays: in terms of code Suppose we have “int pgh[4][5]” Compute address of pgh[index][dig]: phg + index*sizeof(int)*5 + sizeof(int)*dig Assembly code: # %ecx = dig # %eax = index leal 0(,%ecx,4),%edx # 4*dig leal (%eax,%eax,4),%eax # 5*index movl pgh(%edx,%eax,4),%eax # *(pgh + 4*dig + 20*index)

11 Structures Contiguously-allocated region of memory Refer to members within structure by names Members may be of different types Example: struct rec { int i; int a[3]; int *p; }; Memory Layout iap 0416

12 Structures - Code Offset of each structure member determined at compile time struct rec { int i; int a[3]; int *p; }; iap 0416 r + 4 + 4*idx r int * find_a (struct rec *r, int idx){ return &r->a[idx]; } # %ecx = idx # %edx = r leal 0(,%ecx,4),%eax# 4*idx leal 4(%eax,%edx),%eax# r+4*idx+4

13 Alignment Offsets Within Structure –Must satisfy element’s alignment requirement Overall Structure Placement –Each structure has alignment requirement K Largest alignment of any element –Initial address & structure length must be multiples of K Example (under Windows): –K = 8, due to ”double” element struct S1 { char c; int i[2]; double v; } *p; ci[0]i[1]v p+0p+4p+8p+16p+24 Multiple of 4Multiple of 8

14 Unions Overlay union elements Allocate according to largest element Can only use one field at a time union U1 { char c; int i[2]; double v; } *up; c i[0]i[1] v up+0up+4up+8

15 Homework problem 3.36 typedef struct { int left; a_struct a[CNT]; int right; } b_struct; void test(int i, b_struct *bp) { int n = bp->left + bp->right; a_struct *ap = &bp->a[i]; ap->x[ap->idx] = n; } Suppose a_struct only has elements: x and idx 1push %ebp 2mov %esp, %ebp 3push %ebx 4mov 0x8(%ebp), %eax 5mov 0xc(%ebp), %ecx 6lea (%eax,%eax,4),%eax 7lea 0x4(%ecx,%eax,4),%eax 8mov (%eax), %edx 9shl $0x2, %edx 10mov 0xb8(%ecx), %ebx 11add (%ecx), %ebx 12mov %ebx,0x4(%ebx,%eax,1) 13pop %ebx 14mov %ebp, %esp 15pop %ebp 16ret Assembly code of test: A piece of C code

16 Homework problem 3.36 Question: –Find out the value of CNT –Complete declaration of a_struct

17 Homework problem 3.36 1push %ebp 2mov %esp, %ebp 3push %ebx 4mov 0x8(%ebp), %eax# %eax = i 5mov 0xc(%ebp), %ecx# %ecx = bp 6lea (%eax,%eax,4),%eax# 7lea 0x4(%ecx,%eax,4),%eax# %eax = bp+4+4*(5*i) = ap 8mov (%eax), %edx# %edx = *(%eax) = idx 9shl $0x2, %edx# %edx = %edx * 4 = idx * 4 10mov 0xb8(%ecx), %ebx# 11add (%ecx), %ebx# %ebx = *(bp)+*(bp+0xb8)= n 12mov %ebx,0x4(%edx,%eax,1)# *(ap + 4 + idx*4) = %ebx 13pop %ebx 14mov %ebp, %esp 15pop %ebp 16ret void test(int i, b_struct *bp){ int n = bp->left + bp->right; a_struct *ap = &bp->a[i]; ap->x[ap->idx] = n; }

18 Answer: CNT = 9 typedef struct { int idx; int x[4]; } a_struct; leftrightidxx[0]x[1]x[2]x[3] bp+0bp+184 idxx[0]x[1]x[2]x[3] bp->a[0] bp+4 bp->a[8] ……

19 Function pointer (C code) void (*pfunc) (char *message); /*declaration*/ void myprint(char *message) { printf(“my message: %s\n", message); } void message(void (*pfunc)(char *message), char *str) { pfunc(str); } int main(int argc, char* argv[]) { message(myprint, “hello world”); return 0; }

20 Function pointer (assembly code) main: push%ebp mov%esp, %ebp sub$0x8, %esp and$0xfffffff0, %esp mov$0x0, %eax sub%eax, %esp sub$0x8, %esp push0x8048444 push0x8048328 call0x8048343 add$0x10, %esp mov$0x0, %eax leave ret message: push%ebp mov%esp, %ebp sub$0x8, %esp sub$0xc, %esp push0xc(%ebp) mov0x8(%ebp), %eax call*%eax add$0x10, %esp leave ret Function pointer is starting address of a function myprint: 0x8048328push%ebp 0x8048329mov %esp, %ebp


Download ppt "Recitation 3 Outline Recursive procedure Complex data structures –Arrays –Structs –Unions Function pointer Reminders Lab 2: Wed. 11:59PM Lab 3: start early."

Similar presentations


Ads by Google