170 likes | 186 Views
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 **U[6]; long double V[8]; long double *W[4]; Array Element size Total size Start Address Element i S T U V W. 14. X S.
E N D
Chapter 3 Machine-Level (2) Representation of Programs Jin Lu 11210240054@fudan.edu.cn
Problem3.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 14 XS +2i 2 4 12 XT +4i 4 24 XU +4i 12 96 XV +12i 4 16 XW +4i
Problem3.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
Problem3.19 (P185) M=5, N=7 #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]
Problem 3.20 (P188) void fix_set_diag(fix_matrix A, int val){ int i; for(i = 0; i < N; i++) A[ i ] [ i ] = val; } 1. movl 12(%ebp), %edx 2. movl 8(%ebp), %eax 3. movl $15, %ecx 4. addl $1020, %eax 5. .p2align 4,,7 6. .L50: 7. movl %edx, (%eax) 8. addl $-68, %eax 9. decl %ecx 10.jns .L50 Create a C code program using optimizations similar to those in the assembly code. Get val Get A i = 15 Aptr=&A[0][0]+1020/4 loop: *Aptr = val Aptr -= 68/4 i-- if i >= 0 goto loop
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{ 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.y &(sp -> s.x) sp
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) 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/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;}; 1. 除char,short外,linux下gcc 默认为4-bytes aligned。 2. 同类型数据若连续声明,则 可连续存放。
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 <_init + 0x50> 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). strlen(buf)+1 08 04 86 43 返回地址 bf ff fc 94 保存%ebp buf[4-7] result == null? buf[0-3] 00 00 00 01 保存%esi 00 00 00 02 保存%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 C. new return address? D. what register(s) have corrupted value(s) when getline returns? E. what two other things are wrong for getline? 33 32 31 30
Problem 3.25 (P214) Generate stack code for the expression x=a*b/c*-(a+b*c). Diagram the contents of the stack for each step of your code. Please refer P250, CSAPP for the answers. * / - pushing order? * c + a b a * R-L-M b c
Problem 3.26 (P217) Write a C expression describing the contents of the top stack element at the end of this code sequence in terms of x, a and b. Please refer P251, CSAPP for the answers.
Problem 3.27 (P220) Diagram the stack contents after each step of the following code: 1. fldl b 2. fldl a 3. fmul %st(1), %st 4. fxch 5. fdivrl c 6. fsubrp 7. fstp x Give a C expression describing this computation. x=a*b-c/b
Problem 3.28 (P221) Function funct2 with arguments a, x, b, and i. 1. movl 8(%ebp), %eax 2. fldl 12(%ebp) 3. flds 20(%ebp) 4. movl %eax, -4(%ebp) 5. fildl -4(%ebp) //p216 6. fxch %st(2) 7. faddp %st, %st(1) 8. fdivrp %st, %st(1) 9. fld1 10.flds 24(%ebp) 11.faddp %st, %st(1) 12.fsubrp %st, %st(1) //missing The returned value is of type double. Write C code for this funct2. double funct2(int a, double x, float b, float i) { return a/(x+b)-(i+1); }
Problem 3.29 (P223) int less(double x, double y){ return x < y; } 1. fldl 16(%ebp) 2. fcompl 8(%ebp) 3. fnstsw %ax 4. andb $69, %ah //69 = [00100101], a mask 5. sete %al 6. movzbl %al, %eax Show how, by inserting a single line of assembly code into the preceding code sequence, you can implement the following function: int greater(double x, double y){ return x > y; } Between lines 4 and 5, insert: cmpb $1, %ah
Problem 3.30 (P228) int ok_smul(int x, int y, int *dest){ long long prod = (long long) x*y; int trunc = (int) prod; *dest = trunc; return (trunc == prod); } A variable declared as type "long long" will have twice the size of normal long variable. Thus, the statement long long prod = (long long) x * y; will compute the full 64-bit product of x and y. Using this facility, write a version of ok_smul that does not use any asmstatements. int ok_smul(int x, int y, int *dest);