Chapter 3 Machine-Level (2) Representation of Programs n Lu 11210240054@fudan.edu.cn
Chapter 3 Machine-Level (2) Representation of Programs Jin Lu 11210240054@fudan.edu.cn
Problem 3. 17(P181) short S[7 short T[3] short Uf6]; long double 8 long double Array Element size Total size Start Address Element i 2 14 . i T 4 2 +4i 24 L×」[+4i 12 96 Ⅹ +12i 4 16 +4i
Problem 3.17 (P181) short S[7]; short *T[3]; short **U[6]; long double V[8]; long double *W[4]; Array Element size Total size Start Address Element i S T U V W 2 12 4 4 14 XS 4 96 XV +12i 24 XU +4i X +4i 12 T +2i 16 XW +4i
Problem 3.18(P182 Suppose the address of short integer array s and integer index i are stored in registers %edx and %eCx, respectively. The result should be stored in register %eax if it is a pointer and register element %ax if it is a short integer Expression Type Value Assembly code S+1 short* xs+2 leal 2 (%edx), %eax S[3] short M[Xs+6 moww6(%ed×),%ax &S[ short: Xs+2i leal (%edx, %ecx, 2),%eax S[4*i+1 short M[Xs+81+2] mov 2(%edx, %ecx, 8),%ax S+i-5 short*Ⅹs+2i-10 leal-10(%edx, %ecx, 2),%eax
Problem 3.18 (P182) Suppose the address of short integer array S and integer index i are stored in registers %edx and %ecx, respectively.The result should be stored in register %eax if it is a pointer and register element %ax if it is a short integer. Expression Type Value Assembly code S+1 short * XS+2 leal 2(%edx),%eax S[3] short M[XS+6] movw 6(%edx),%ax &S[i] short * XS+2i leal (%edx, %ecx, 2), %eax S[4*i+1] short M[XS+8i+2] movw 2(%edx, %ecx, 8), %ax S+i-5 short * XS+2i-10 leal -10(%edx, %ecx, 2), %eax
Problem 3.19(P185) #define int mat 1 [MIN] M=5,N=7 int mate[NJM] nt sum element(int i, int j){ return mat1[1][j]+ mat2 0j[i] 1.moⅵ8(%ebp),%ecX Get i 2.moⅵ12(%ebp),%eax Get i 3. leal O(%eax, 4),%ebx 4. leal 0(,%eCx, 8),%edx 8 5. subl %ecx. %edx 6. addl %ebx, %eax 7. sall $2, %eax 8. movl mat2(%eax, %eCx, 4), %eax ma2(20+4*)/41 9. addl mat 1(%ebx, %edx, 4),%eax +mat(4*+28*)/4
Problem 3.19 (P185) #define: int mat1[M][N]; int mat2[N][M]; int sum_element(int i, int j){ return mat1[ i ] [ j ] + mat2[ j ] [ i ]; } 1. movl 8(%ebp), %ecx 2. movl 12(%ebp), %eax 3. leal 0(,%eax,4), %ebx 4. leal 0(,%ecx,8), %edx 5. subl %ecx, %edx 6. addl %ebx, %eax 7. sall $2, %eax 8. movl mat2(%eax,%ecx,4), %eax 9. addl mat1(%ebx,%edx,4), %eax Get i Get j 4*j 8*i 7*i 5*j 20*j mat2[(20*j+4*i)/4] +mat1[(4*j+28*i)/4] M=5, N=7
Problem 3.20(P188) void fix set diag(fix matrix A, int val) for(i=0; i=0 goto loop Create a c code program using optimizations similar to those in the assembly code
Problem 3.20 (P188) void fix_set_diag(fix_matrix A, int val){ int i; for(i = 0; i = 0 goto loop
void fix set diag opt(fix matrix A, int valt int *Aptr= &A[OJO]+ 255 nt cnt=n-1 dot Aptr= val Aptr-=(N+1) cnt While(cnt >=0)
void fix_set_diag_opt(fix_matrix A, int val){ int *Aptr = &A[0][0] + 255; int cnt = N-1; do{ *Aptr = val; Aptr -= (N+1); cnt--; }while(cnt >= 0); }
Problem 3.21(P194) struct prob A. what are the offsets of the intp following fields? struct int x sX next struct prob * next B how many total bytes does the structure require Sp-> S void sp_init(stry 1.moⅵ8(%ebp),%eaX sp>s.x=(&(>8x 2.moM8(%ea×),%edx sp-> p 3. movl %edx, 4(%eax) next s sp 4. leal 4(%eax), %edx 5. movl %edx, %eax 6.moⅵ%eax,12(%eax)
Problem 3.21 (P194) struct prob{ int *p; struct{ int x; int y; }s; struct prob *next; }; void sp_init(struct prob *sp){ sp -> s.x = _____; sp -> p = _____; sp _> next = _____; } A. what are the offsets of the following fields? p: s.x: s.y: next: B. how many total bytes does the structure require? C. 1. movl 8(%ebp),%eax 2. movl 8(%eax),%edx 3. movl %edx,4(%eax) 4. leal 4(%eax),%edx 5. movl %edx,(%eax) 6. movl %eax,12(%eax) &(sp -> s.x) sp -> s.y sp
Problem 3.22(P197) union ele A. what would be the offsets of the struct following fields intp e1.p 0 e1.y: }e1 e1.p el e2x struct e2. next e2.x e2. next int x union ele *next B how many total byt es would the }e2; structure requires 1. movl 8 (%ebp),%eax void proc(union ele up 2. mov 4(%eax), %edx p =*(up->)-up 3. movl (%edx),%eCx 4. movl %ebp, %esp 5. mov (%eax), %eax up->e2. next->e1.y =*(up->e2. next->e1. p)-up->e2.x/6. movl (%ec×),%ecX 7. subl %eax %ecx 8. movl %eCx, 4(%edx)
Problem 3.22 (P197) union ele{ struct{ int *p; int y; } e1; struct{ int x; union ele *next; } e2; }; void proc(union ele *up){ up->____=*(up->____)-up->____; } A. what would be the offsets of the following fields: e1.p: e1.y: e2.x: e2.next: B. how many total bytes would the structure require? C. 1. movl 8(%ebp), %eax 2. movl 4(%eax), %edx 3. movl (%edx), %ecx 4. movl %ebp, %esp 5. movl (%eax), %eax 6. movl (%ecx), %ecx 7. subl %eax, %ecx 8. movl %ecx, 4(%edx) e1.p e1.y e2.x e2.next 0 4 up->e2.next->e1.y = *(up->e2.next->e1.p)-up->e2.x
Problem 3.23(P200) For each of the following structure declarations, determine the offset of each field, the total size of the structure, and its alignment requirement under Linux/A32 A. struct plint i; char c; int j; char d; 1: 1.除cha, short外, linux下gcc B struct paint i; char c; char d; int j: 1 默认为4 bytes aligned C. struct p3(short w[3]; char c[3] ] 2.同类型数据若连续声明,则 D. struct p4short w[3]; char*c[3]; 1 可连续存放。 E. struct p5(struct p1 a[2]; struct p2 *p: 1 a p Total Alignment 0 32 36 4 D Total Alignment 0 20 4
Problem 3.23 (P200) For each of the following structure declarations, determine the offset of each field, the total size of the structure, and its alignment requirement under Linux/IA32. A. struct p1{int i; char c; int j; char d;}; B. struct p2{int i; char c; char d; int j;}; C. struct p3{short w[3]; char c[3];}; D. struct p4{short w[3]; char *c[3];}; E. struct p5{struct p1 a[2]; struct p2 *p;}; A i c j d Total Alignment 0 4 8 12 16 4 B i c j d Total Alignment 0 4 5 8 12 4 C w c Total Alignment 0 6 10 2 D w c Total Alignment 0 8 20 4 E a p Total Alignment 0 32 36 4 1. 除char,short外,linux下gcc 默认为4-bytes aligned。 2. 同类型数据若连续声明,则 可连续存放
Problem 3. 24(P208 char getline Ot char buf [8] A fill in the diagram that follows. (after strlen(buf)+1 executing the instruction at line 6) char *result: gets(buf) 返回地址 result= malloc(strlen(buf) bf ff fc 94 呆存‰ebp strcpy(result, buf) buff return result result = null? buff0-31 2. push %ebp 3. mov %esp, %ebp 00000001 保存%esi 4.sub$0×10,%esp 00000002 呆存%ebX 5. push %esi 6. push %ebx 7. add $Oxfffffff4, %esp B. modify your diagram to show the effect of 8. lea Oxfffffff8(%ebp),%ebx the call to gets(line 10) 9. push %ebx 08048600 10. call 80483ac 31303938 363534 returpadcre The return addr of getline is 0X8048643 %ebp equal to Oxbffffc94 D. What register(s have corrupted' value(s) when getline returns? %esi equal to Ox1, %ebx equal to Ox2 E what two other things are wrong for getline? Type in:"012345678901
Problem 3.24 (P208) char *getline(){ char buf[8]; char *result; gets(buf); result = malloc(strlen(buf)); strcpy(result, buf); return result; } 2. push %ebp 3. mov %esp, %ebp 4. sub $0x10, %esp 5. push %esi 6. push %ebx ----- 7. add $0xfffffff4, %esp 8. lea 0xfffffff8(%ebp), %ebx 9. push %ebx 10.call 80483ac The return addr of getline() is 0x8048643, %ebp equal to 0xbffffc94, %esi equal to 0x1, %ebx equal to 0x2. Type in : "012345678901" A. fill in the diagram that follows.(after executing the instruction at line 6). 08 04 86 43 bf ff fc 94 00 00 00 01 00 00 00 02 返回地址 保存%ebp buf[4-7] buf[0-3] 保存%esi 保存%ebx B. modify your diagram to show the effect of the call to gets(line 10). 08 04 86 00 31 30 39 38 37 36 35 34 33 32 31 30 C. new return address? D. what register(s) have corrupted value(s) when getline returns? E. what two other things are wrong for getline? strlen(buf)+1 result == null?