Recitation 3 Outline • Recursive procedure • Complex data structures – Arrays – Structs – Unions • Function pointer Reminders • Lab 2: Wed.

Download Report

Transcript Recitation 3 Outline • Recursive procedure • Complex data structures – Arrays – Structs – Unions • Function pointer Reminders • Lab 2: Wed.

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:
[email protected]
Office hours:
Thursdays 5-6PM
Wean Hall 1315
Recursive procedure example: Fibonacci
int fibo (int n)
{ int result;
if (n <= 2)
result = 1;
else
result = fibo(n-2)
+ fibo(n-1);
return result;
}
0x8048420
0x8048421
0x8048423
0x8048426
0x8048427
0x8048428
0x804842b
0x804842e
0x8048430
0x8048435
0x8048437
0x804843a
0x804843d
0x804843e
0x8048443
0x8048445
0x8048448
0x804844b
0x804844c
0x8048451
0x8048453
0x8048456
0x8048457
0x8048458
0x804845a
0x804845b
push
mov
sub
push
push
mov
cmp
jg
mov
jmp
add
lea
push
call
mov
add
lea
push
call
add
lea
pop
pop
mov
pop
ret
%ebp
%esp,%ebp
$0x10,%esp
%esi
%ebx
0x8(%ebp),%ebx
$0x2,%ebx
0x8048437
$0x1,%eax
0x8048453
$0xfffffff4,%esp
0xfffffffe(%ebx),%eax
%eax
0x8048420 <fibo>
%eax,%esi
$0xfffffff4,%esp
0xffffffff(%ebx),%eax
%eax
0x8048420 <fibo>
%esi,%eax
0xffffffe8(%ebp),%esp
%ebx
%esi
%ebp,%esp
%ebp
Stack Frame
<fibo>
0x8048420
0x8048421
0x8048423
0x8048426
0x8048427
push
mov
sub
push
push
%ebp
%esp,%ebp
$0x10,%esp
%esi
%ebx
Stack at this point
lea
pop
pop
mov
pop
ret
0xffffffe8(%ebp),%esp
%ebx
%esi
%ebp,%esp
%ebp
. . . . . .
0x8048453
0x8048456
0x8048457
0x8048458
0x804845a
0x804845b
x
rtrn addr
%ebp
old %ebp
old %esi
%esp
old %ebx
Write Comments For Body
0x8048428
0x804842b
0x804842e
0x8048430
0x8048435
0x8048437
0x804843a
0x804843d
0x804843e
0x8048443
0x8048445
0x8048448
0x804844b
0x804844c
0x8048451
0x8048453
mov
cmp
jg
mov
jmp
add
lea
push
call
mov
add
lea
push
call
add
. . .
0x8(%ebp),%ebx
$0x2,%ebx
0x8048437
$0x1,%eax
0x8048453
$0xfffffff4,%esp
0xfffffffe(%ebx),%eax
%eax
0x8048420 <fibo>
%eax,%esi
$0xfffffff4,%esp
0xffffffff(%ebx),%eax
%eax
0x8048420 <fibo>
%esi,%eax
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
ebx = x
if (x>2)
goto L1
eax = 1
goto L2
L1:
push x-2
call fibo
esi = eax
push x-1
call fibo
eax += esi
L2:
Stack Changes of fibo(3)
return
call fibo(1)
%ebp
fibo(3)
%ebp
fibo(3)
fibo(3)
%esp
1
0x8048443
%ebp
fibo(1)
%esp
%esp
1
Stack Changes of fibo(3)
return
call fibo(2)
%ebp
%ebp
fibo(3)
%esp
fibo(3)
1
fibo(3)
1
2
0x8048451
%ebp
fibo(2)
%esp
1
%esp
2
Arrays
• Allocated as contiguous blocks of memory
int cmu[5] = {…};
1
40
/*cmu begins at address 40*/
5
44
2
48
1
52
3
56
60
• Address Computation Example
cmu[0]
cmu[3]
cmu[i]
40+0*sizeof(int) = 40
40+3*sizeof(int) = 52
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
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
Nested arrays: in terms of Memory
int A[R][C];
Like a matrix 
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 
A[0]
A
[0]
[0]
A
•••
A[i]
A
[0]
• • •
[C-1]
A
[i]
[0]
•••
A[R-1]
A
A
[i]
• • • [R-1]
[C-1]
[0]
A+i*C*4
Starting address of A[i]
•••
A
[R-1]
[C-1]
A+(R-1)*C*4
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)
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
i
0
a
4
p
16
Structures - Code
• Offset of each structure member determined at
compile time
struct rec {
int i;
int a[3];
int *p;
};
r
i
0
a
4
p
16
r + 4 + 4*idx
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
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):
struct S1 {
– K = 8, due to ”double” element
char c;
int i[2];
double v;
} *p;
c
p+0
i[0]
p+4
Multiple of 4
Multiple of 8
i[1]
p+8
v
p+16
p+24
Multiple of 8
Multiple of 8
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]
up+0
v
up+4
i[1]
up+8
Homework problem 3.36
A piece of C code
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
Assembly code of test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
push %ebp
mov %esp, %ebp
push %ebx
mov 0x8(%ebp), %eax
mov 0xc(%ebp), %ecx
lea (%eax,%eax,4),%eax
lea 0x4(%ecx,%eax,4),%eax
mov (%eax), %edx
shl $0x2, %edx
mov 0xb8(%ecx), %ebx
add (%ecx), %ebx
mov %ebx,0x4(%ebx,%eax,1)
pop %ebx
mov %ebp, %esp
pop %ebp
ret
Homework problem 3.36
• Question:
– Find out the value of CNT
– Complete declaration of a_struct
Homework problem 3.36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void test(int i, b_struct *bp){
int n = bp->left + bp->right;
a_struct *ap = &bp->a[i];
ap->x[ap->idx] = n;
}
push %ebp
mov %esp, %ebp
push %ebx
mov 0x8(%ebp), %eax
# %eax = i
mov 0xc(%ebp), %ecx
# %ecx = bp
lea (%eax,%eax,4),%eax
#
lea 0x4(%ecx,%eax,4),%eax
# %eax = bp+4+4*(5*i) = ap
mov (%eax), %edx
# %edx = *(%eax) = idx
shl $0x2, %edx
# %edx = %edx * 4 = idx * 4
mov 0xb8(%ecx), %ebx
#
add (%ecx), %ebx
# %ebx = *(bp)+*(bp+0xb8)= n
mov %ebx,0x4(%edx,%eax,1)
# *(ap + 4 + idx*4) = %ebx
pop %ebx
mov %ebp, %esp
pop %ebp
ret
Answer:
CNT = 9
typedef struct {
int idx;
int x[4];
} a_struct;
bp+4
left idx x[0] x[1] x[2] x[3]
bp+0
bp->a[0]
……
idx x[0] x[1] x[2] x[3] right
bp->a[8]
bp+184
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;
}
Function pointer (assembly code)
main:
push
mov
sub
and
mov
sub
sub
push
push
call
add
mov
leave
ret
%ebp
%esp, %ebp
$0x8, %esp
$0xfffffff0, %esp
$0x0, %eax
%eax, %esp
$0x8, %esp
0x8048444
0x8048328
0x8048343 <message>
$0x10, %esp
$0x0, %eax
message:
push %ebp
mov
%esp, %ebp
sub
$0x8, %esp
sub
$0xc, %esp
push 0xc(%ebp)
mov
0x8(%ebp), %eax
call *%eax
add
$0x10, %esp
leave
ret
myprint:
0x8048328
0x8048329
push %ebp
mov %esp, %ebp
Function pointer is starting address of a function