深入理解计算机系统第三章练习题

习题3.1

地址 值
0x100 0xFF
0x104 0xAB
0x108 0x13
0x10C 0x11
寄存器 值
%rax 0x100
%rcx 0x1
%rdx 0x3

%rax 0x100 寄存器寻址
0x104 0xAB 绝对寻址
0x108 0x108 立即数寻址 (%rax) 0xFF 间接寻址 4(%rax) 0x4+0x100 = 0x104 错了 应该为M(0x104)即0x104的内存地址为0xAB 9(%rax,%rdx) 0x9+0x100+0x3 = 0x10B 错了 应该为M(0x10C) 即0x11 260(%rcx,%rdx) 0x260+0x1+0x3 = 0x264 这里题和答案不一样,答案是rcx
0xFC(,%rcx,4) 0xFC+40x1 = 0xF11 错,应该为M(0xFC+0x4) = M(0x100)即内存地址0x100的值为0xFF
(%rax,%rdx,4) 0x100+4
0x3 = 0x10C 错,应该为M( 0x100+4*0x3) = M(0x10C)即内存地址为0x10C的值为0x11
错题原因,带括号的算出来还得在前面加上M(算出来的值),即对应算出来的值得内存地址的值

习题3.2

movl %eax,(%rsp)
movw (%rax),%dx
movb %xFF,%bl
movb (%rsp,%rdx,4) %dl
movq (%rdx),%rax
movw %dx ,(%rax)

习题3.3

movb 0xF (%ebx) %ebx不能作为目的寄存器 movl %rax,(%rsp) %rax应该用movq movw (%rax),4(%rsp) 内存不能直接传送到内存,应该先传送到寄存器 movb %al,%sl 没有sl这个寄存器 movq %rax,0x123 立即数不能作为目的数
movl %eax,%rdx 应该为movq
movb %si,8(%rbp) 应该为movw

习题3.4

long long
movq (%rdi), %rax
movq %rax , (%rsi)

char int
movsbl (%rdi),%eax
movl %eax,(%rsi)

char unsigned
movsbl (%rdi),%eax
movl %eax,(%rsi)

unsigned char long
movzbq (%rdi),%rax
movq %rax,(%rsi)

int char
movb (%rdi),%al
movb %al,(%rsi)

unsigned unsigned char
movb (%rdi),%al
movb %al,(%rsi)

char short
movsbw (%rdi),%ax
movw %ax .(%rsi)

寄存器 内存
rdi(4) 3
rsi(3)
rax

习题3.5
long a = *xp
long b = *yp
long c = *zp
*yp = a;
*zp = c;
*xp = b;

习题3.6
%rax = x,%rcx = y
6+x
x+y
x+4y
9y+7
4y+10
x+2y+9
习题3.7
%rdi=x,%rsi=y,%rdx=z
5x+2y+8z

习题3.8
目的 值
0x100 0x1+0xFF = 0x100
0x100+8 =0x108 0xAB-0x3 = 0xA8
(0x100+0x18)=0x118 0x11 * 16 = 0x110
0x110 0x14
%rcx 0x0
%rax 0xFD

习题3.9
SALQ $4,%rax
SARQ %cl,%rax

习题3.10
long t1 = (x | y)/8
t2 = ~t1;
t3 = z
t4 = z-x
习题3.12
movq %rdx,%r8
movq %rdi,%rdx
divq %rsi
movq %rax,(%r8)
movq %rdx,(%rcx)

习题3.13

A,B,D

习题3.14

A,B,D

习题3.15

A.4003fe
B.400525
C.
400545
D.看不懂

习题3.16
A.
long result;
if(p & x>*p)
goto a_je_p;

a_je_p:
*p = x;

B.因为虽然看起来只有一个if,实际会判断2次
习题3.17
太傻比不做
习题3.18
%rax = (x+y)
result = x+y+z
if(x< -3){
if(y < z){
result = x * y;
}else{
result = y * z;
}
}else if(x>2){
result = xz
}
习题3.19
Tmp = (Tran-Tok)
2 = 30
30+16 = 46
习题3.20
x = x+7
x >= 0 ? x = x:x = x+7
x = x/2(3)
习题3.21
rax = x+8 错了,rax应该= x*8
long val = x+8;
if(y>0){
if(y>=x){
val= x&y
}else{
return y-x
}
}else if(y >= -2){
val = x+y
}
val(%rax) = y-x
m(%rdx) = x & y
x>=y ? x&y : y-x

%rdi = x+y
y >= -2 ?x+y : x*8

习题3.23
x in %rax y in %rcx n in %rdx

习题3.24
long result = 1;
while(a < b)}
result = (a+b)*result;
a = a+1;
}
习题3.25
long result = b;
while(b>0){
result = result * a;
b = b-a;
}
return result
习题3.26
long val = 0
while(x <= 0 ){
val = val * 2(x);
x >>=1
}
val= val& 1;

习题3.27
long i = 2;
if(i > n{
goto done
)
loop:
result *=i;
i++;
if(i <= n){
goto loop
}

习题3.28
for(long i = 64; ; i--){
x = x &1;
val = val+val;
val = val | x;
x = x >> 1;

}

练习题3.30
0到7

x = 6

0 L9 -1
1 L5 0
2 L6 1
3 L7 2
4 L2 def 3
5 L7 4
6 L8 5
7 L2 def 6
8 L5 7

3.31
switch(7){
case 5
c = 15^b
case 0:
val = (112+c)
case 2:
case 7:
c+b <<2
break
case4
val = a;
break
default
val = b;

}

习题3.32

        PC                    指令      %rdi      %rsi      %rax             %rsp                  *%rsp        

M1 0X400560 callq 10 ----- ----- 0x7fffffffe820 ----
F1 0x400548 lea 10 11 ----- 0x7fffffffe818 400565
F2 0x40054c sub 9 11 ----- 0x7fffffffe818 400565
F3 0x400550 callq 9 11 ---- 0x7fffffffe818 -----
L1 0x400540 mov 9 11 9 0x7fffffffe810 400555
L2 0x40054c imul 9 11 99 0x7fffffffe810 400555
L3 0x400547 ret 9 11 99 0x7fffffffe810 400555
F4 0x400555 ret ----- ---- 99 0x7fffffffe818 400565
M2 0x400565 mov ----- - --- 99 0x7fffffffe820 ------
习题3.33
%rdi,%rsi,%rdx,%rcx
u v
a = (long)a;
*u += a;
*v += b;

a = 6

a(int)
b(short)
u(long)
v(char)
练习题3.35
if(x == 0){
return 0;
}
unsigned long nx = x >>2;
long rv = rfun(nx);
return x+rv;

x>>2
x + rfun(x>>2)
3.36
元素大小 整个数组大小 元素
2 14 X(s)+2i
8 24 X(t)+8i
8 48 x(u)+8i
4 32 x(v)+4i
8 32 x(w)+8i

3.37练习题
S+1 short* xs+2 leaq M{%rdx,%rcx,2},%rax
S[3] short M[xs+6] movw M{%rdx,%rcx,3},%rax
&S[i] short* xs+6 leaq M{%rdx,%rcx,3},%rax
S[4i+1] short M[xs+4i+1] movw M1{%rdx,%rcx,4},%rax
S+i-5 short
xs+2i-10 leaq M{%rdx,%rcx,i-5},%rax

习题3.38
7i+j rdx
5j rax
5j+i rdi

8(5j+i)
8(7i+j)+Q +8(5j+i)+P
习题3.40
int *Aptr = &A[0][0];
int *Bptr = &B[i][i];
int Cptr = &C[N][N];
int val ;
int i = 0;
do{
A[i][i] = val;
i = 68
i;
}while(Bptr!=Cptr)

习题 3.41
A.8,12,16,24
B.24
C.
sp->p;
sp->x
sp->
p

第四章

4.1
irmovq $15,%rbx
30 F3 0f 00 00 00 00 00 00 00
rrmovq %rbx,%rcx
20 31
rmmovq %rcx,-3(%rbx)
40 13 fd ff ff ff ff ff ff ff
addq %rbx,%rcx
60 31
4.2
0x100:30f3fc ff ff ff ff ff ff ff 4064 0008000000000000
0x100:irmovq -4,%rbx
0x10a:rmmovq %rsi,0x800(%rbx)

a06f 80 0c0200 000000000000 30f30 a00000000000000 90
0x200:a06f :pushq %rsi
0x202:800c02000000000000 call proc
0x20b:00 halt
0x20c proc
0x20c 30f30a00000000000000 irmovq $10,%rbx
0x216 90 ret

50540700000000000000 10 f0 b01f
50540700000000000000:mrmovq 7(%rbp),%rsp
10:nop
f0
b01f:popq %rcx

6113 73 000400000000000000
6113:subq %rcx,%rbx
73:je loop

6362a0f0
6362:xorq %rsi,%rdx
a0:pushq %rax(0f改为0f)
f0

4.1
30 F3 0f 00 00 00 00 00 00 00
20 31

40 13 fd ff ff ff ff ff ff ff
60 31
4.2
irmovq (%rbx)
4063 rmmovq %rsi,0x800(%rbx)

pushq %rsi

4.9
(!a && b ) || (a && !b)
4.11
A<=B &&A<=C : A
B <= C : B
1 : C
4.12
A <=B && C <= A A
B<= A && C <= B B
1

.`

你可能感兴趣的:(深入理解计算机系统第三章练习题)