CSAPP第4章家庭作业参考答案
作者:互联网
4.45
不正确
push %rsp的指令会将rsp减8之前的old value压栈
应该是如下代码:
movq REG, -8(%rsp)
sub $8, %rsp
4.46
不正确
应该改为
add $8, %rsp
movq -8(%rsp), REG
4.47
#include<stdio.h>
long int a[100] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
void bubble_b(long *data, long count){
long i, last;
long *p0; //data[i]
long *p1; //data[i+1]
for(last = count-1; last > 0; last--){
p0 = data;
p1 = data + 1;
for(i = 0; i < last; i++){
if(*p1 < *p0){
long t = *p1;
*p1 = *p0;
*p0 = t;
}
p0++;
p1++;
}
}
}
int main(){
printf("Before sorting:");
for(int i = 0; i < 10; i++){
printf("%ld ", a[i]);
}
printf("\n");
bubble_b(a, 10);
printf("After sorting:");
for(int i = 0; i < 10; i++){
printf("%ld ", a[i]);
}
printf("\n");
return 0;
}
x86-64汇编程序:
0000000000001169 <bubble_b>:
1169: f3 0f 1e fa endbr64
116d: 55 push %rbp
116e: 48 89 e5 mov %rsp,%rbp
1171: 48 89 7d c8 mov %rdi,-0x38(%rbp)
1175: 48 89 75 c0 mov %rsi,-0x40(%rbp)
1179: 48 8b 45 c0 mov -0x40(%rbp),%rax
117d: 48 83 e8 01 sub $0x1,%rax
1181: 48 89 45 e0 mov %rax,-0x20(%rbp)
1185: eb 73 jmp 11fa <bubble_b+0x91>
1187: 48 8b 45 c8 mov -0x38(%rbp),%rax
118b: 48 89 45 e8 mov %rax,-0x18(%rbp)
118f: 48 8b 45 c8 mov -0x38(%rbp),%rax
1193: 48 83 c0 08 add $0x8,%rax
1197: 48 89 45 f0 mov %rax,-0x10(%rbp)
119b: 48 c7 45 d8 00 00 00 movq $0x0,-0x28(%rbp)
11a2: 00
11a3: eb 46 jmp 11eb <bubble_b+0x82>
11a5: 48 8b 45 f0 mov -0x10(%rbp),%rax
11a9: 48 8b 10 mov (%rax),%rdx
11ac: 48 8b 45 e8 mov -0x18(%rbp),%rax
11b0: 48 8b 00 mov (%rax),%rax
11b3: 48 39 c2 cmp %rax,%rdx
11b6: 7d 24 jge 11dc <bubble_b+0x73>
11b8: 48 8b 45 f0 mov -0x10(%rbp),%rax
11bc: 48 8b 00 mov (%rax),%rax
11bf: 48 89 45 f8 mov %rax,-0x8(%rbp)
11c3: 48 8b 45 e8 mov -0x18(%rbp),%rax
11c7: 48 8b 10 mov (%rax),%rdx
11ca: 48 8b 45 f0 mov -0x10(%rbp),%rax
11ce: 48 89 10 mov %rdx,(%rax)
11d1: 48 8b 45 e8 mov -0x18(%rbp),%rax
11d5: 48 8b 55 f8 mov -0x8(%rbp),%rdx
11d9: 48 89 10 mov %rdx,(%rax)
11dc: 48 83 45 e8 08 addq $0x8,-0x18(%rbp)
11e1: 48 83 45 f0 08 addq $0x8,-0x10(%rbp)
11e6: 48 83 45 d8 01 addq $0x1,-0x28(%rbp)
11eb: 48 8b 45 d8 mov -0x28(%rbp),%rax
11ef: 48 3b 45 e0 cmp -0x20(%rbp),%rax
11f3: 7c b0 jl 11a5 <bubble_b+0x3c>
11f5: 48 83 6d e0 01 subq $0x1,-0x20(%rbp)
11fa: 48 83 7d e0 00 cmpq $0x0,-0x20(%rbp)
11ff: 7f 86 jg 1187 <bubble_b+0x1e>
1201: 90 nop
1202: 90 nop
1203: 5d pop %rbp
1204: c3 retq
Y86-64汇编:
pushq %rbp
rrmovq %rsp, %rbp
rmmovq %rdi,-0x38(%rbp)
rmmovq %rsi,-0x40(%rbp)
mrmovq -0x40(%rbp),%rax
irmovq $0x1, %r8
subq %r8, %rax
rmmovq %rax,-0x20(%rbp)
jmp L1
.L5:
mrmovq -0x38(%rbp),%rax
rmmovq %rax,-0x18(%rbp)
mrmovq -0x38(%rbp),%rax
irmovq $0x8, %r8
addq %r8, %rax
rmmovq %rax,-0x10(%rbp)
irmovq $0x0, %r8
rmmovq %r8, -0x28(%rbp)
jmp L2
.L4:
mrmovq -0x10(%rbp),%rax
mrmovq (%rax),%rdx
mrmovq -0x18(%rbp),%rax
mrmovq (%rax),%rax
rrmovq %rdx, %r8
subq %rax,%r8
jge L3
mrmovq -0x10(%rbp),%rax
mrmovq (%rax),%rax
rmmovq %rax,-0x8(%rbp)
mrmovq -0x18(%rbp),%rax
mrmovq (%rax),%rdx
mrmovq -0x10(%rbp),%rax
rmmovq %rdx,(%rax)
mrmovq -0x18(%rbp),%rax
mrmovq -0x8(%rbp),%rdx
rmmovq %rdx,(%rax)
.L3:
irmovq $0x8, %r8
mrmovq -0x18(%rbp), %r9
addq %r8, %r9
rmmovq %r9 -0x18(%rbp)
irmovq $0x8, %r8
mrmovq -0x10(%rbp), %r9
addq %r8, %r9
rmmovq %r9, -0x10(%rbp)
irmovq $0x1, %r8
mrmovq -0x28(%rbp), %r9
addq %r8, %r9
rmmovq %r9, -0x28(%rbp)
.L2:
mrmovq -0x28(%rbp),%rax
mrmovq -0x20(%rbp), %r8
rrmovq %rax, %r9
subq %r8, %r9
jl L4
irmovq $0x1, %r8
mrmovq -0x20(%rbp), %r9
subq %r8, %r9
rmmovq %r9, -0x20(%rbp)
.L1:
mrmovq -0x20(%rbp), %r9
irmovq $0x0, %r8
subq %r8, %r9
jg L5
popq %rbp
ret
4.51
iaddq V, rB c 0 F rB V
取指:
icode:ifun
←
\leftarrow
← M1[PC]
rA:rB
←
\leftarrow
← M1[PC+1]
valC
←
\leftarrow
← M8[PC+2]
valP
←
\leftarrow
← PC + 10
译码:
valB
←
\leftarrow
← R[rB]
执行:
valE
←
\leftarrow
← valB + valC
Set CC
访存:
写回:
R[rB]
←
\leftarrow
← valE
更新PC:
PC
←
\leftarrow
← valP
4.55
标签:mrmovq,CSAPP,48,mov,r8,家庭作业,rbp,参考答案,rax 来源: https://blog.csdn.net/Carrot_kexin/article/details/120677056