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<N; i++)Ail[i]=val 1. movl 12(%ebp), %edx Get 2.moⅵ8(%ebp),%eax Get a 3. movl $15, %ecx 4.add$1020,%eax Aptr=&A[oo]+1020/4 5. p2align 4.7 6..L50 oop. 7. movl %edx, (%eax) Aptr= va 8. addl $-68, %eax Aptr-=68/4 9. decl %ecx 10ⅰns L50 if 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 < 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