chapter 3 machine-level (2) representation of programs
DESCRIPTION
Chapter 3 Machine-Level (2) Representation of Programs. Jin Lu [email protected]. Problem 3.17 (P181). shortS[7]; short*T[3]; short**U[6]; long doubleV[8]; long double*W[4]; ArrayElement sizeTotal size Start AddressElement i S T U V W. 14. X S. - PowerPoint PPT PresentationTRANSCRIPT
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
+12iXV96
+4iXU24
+4iXT12
+2i
+4iXW16
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 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), %ecx2. movl 12(%ebp), %eax3. leal 0(,%eax,4), %ebx4. leal 0(,%ecx,8), %edx5. subl %ecx, %edx6. addl %ebx, %eax7. sall $2, %eax8. movl mat2(%eax,%ecx,4), %eax9. addl mat1(%ebx,%edx,4), %eax
Get iGet j4*j8*i7*i5*j
20*jmat2[(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){
int i;for(i = 0; i < N; i++) A[ i ] [ i ] = val;
}
1. movl 12(%ebp), %edx2. movl 8(%ebp), %eax3. movl $15, %ecx4. addl $1020, %eax5. .p2align 4,,76. .L50:7. movl %edx, (%eax)8. addl $-68, %eax9. decl %ecx10.jns .L50
Create a C code program using optimizations similar to those in the assembly code.
Get valGet Ai = 15
Aptr=&A[0][0]+1020/4
loop:*Aptr = val
Aptr -= 68/4i--
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),%eax2. movl 8(%eax),%edx3. movl%edx,4(%eax)4. leal 4(%eax),%edx5. movl %edx,(%eax)6. movl%eax,12(%eax)
&(sp -> s.x)
sp -> s.y
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), %eax2. movl 4(%eax), %edx3. movl (%edx), %ecx4. movl %ebp, %esp5. movl (%eax), %eax6. movl (%ecx), %ecx7. subl %eax, %ecx8. 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/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(){
char buf[8];char *result;gets(buf);result = malloc(strlen(buf));strcpy(result, buf);return result;
}2. push %ebp3. mov %esp, %ebp4. sub $0x10, %esp5. push %esi6. push %ebx -----7. add $0xfffffff4, %esp8. lea 0xfffffff8(%ebp), %ebx9. push %ebx10.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).
08 04 86 43bf ff fc 94
00 00 00 0100 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 0031 30 39 3837 36 35 3433 32 31 30C. 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?
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.
*/
+-
a **
b c
ca b
pushing order?
R-L-M
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 b2. fldl a3. fmul %st(1), %st4. fxch5. fdivrl c6. fsubrp7. 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), %eax2. fldl 12(%ebp)3. flds 20(%ebp)4. movl %eax, -4(%ebp)5. fildl -4(%ebp) //p2166. fxch %st(2)7. faddp %st, %st(1)8. fdivrp %st, %st(1)9. fld110.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 %ax4. andb $69, %ah //69 = [00100101], a mask5. sete %al6. 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)
A variable declared as type "long long" will have twice the size of normal long variable. Thus, the statementlong 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 asm statements.
int ok_smul(int x, int y, int *dest);
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);
}
Thank you!