2017-2018-1 20155201 《信息安全系统设计基础》第八周课下作业
补充课上测试:
sum.c
#include <stdio.h>
int main(int argc,char *argv[]){
int a,b;
int i=0;
int sum=0;
for(i=1;i<100;i++){
if(argv[i]==' '){
break;
}
sum+=atoi(argv[i]);
}
printf("%d
",sum);
return 0;
}
sum.s
_main: ## @main
pushq %rbp
Ltmp0:
Ltmp1:
movq %rsp, %rbp
Ltmp2:
subq $48, %rsp
movl $0, -4(%rbp)
movl %edi, -8(%rbp)
movq %rsi, -16(%rbp)
movl $0, -28(%rbp)
movl $0, -32(%rbp)
movl $1, -28(%rbp)
LBB0_1:
cmpl $100, -28(%rbp)
jge LBB0_6
movslq -28(%rbp), %rax
movq -16(%rbp), %rcx
cmpq $0, (%rcx,%rax,8)
jne LBB0_4
jmp LBB0_6
LBB0_4:
movslq -28(%rbp), %rax
movq -16(%rbp), %rcx
movq (%rcx,%rax,8), %rdi
movb $0, %al
callq _atoi
addl -32(%rbp), %eax
movl %eax, -32(%rbp)
movl -28(%rbp), %eax
addl $1, %eax
movl %eax, -28(%rbp)
jmp LBB0_1
LBB0_6:
leaq L_.str(%rip), %rdi
movl -32(%rbp), %esi
movb $0, %al
callq _printf
xorl %esi, %esi
movl %eax, -36(%rbp)
movl %esi, %eax
addq $48, %rsp
popq %rbp
retq
L_.str:
反汇编代码:
objdump -d sum.o
y86代码:
课下作业
之前用成了第二版的教材。。发现两个版本的家庭作业不一样 又找时间重新做了下,以下是课下作业:
修改后的bubble sort:
void bubble_p(long* data, long count) {
long *i, *last;
for (last = data+count-1; last > data; last--) {
for (i = data; i < last; i++) {
if ( *(i+1) < *i) {
long t = *(i+1);
*(i+1) = *i;
*i = t;
}
}
}
}
测试代码
void bubble_p(long* data, long count) {
long *i, *last;
for (last = data+count-1; last > data; last--) {
for (i = data; i < last; i++) {
if (*(i+1) < *i) {
long t = *(i+1);
*(i+1) = *i;
*i = t;
}
}
}
}
int main(){
long i;
long data[]={1,0,3,5,2,6,8,9,7,4};
printf("待排序序列:");
for(i=0;i<10;i++){
printf("%ld ",*(data+i));
}
bubble_p(data,10);
printf("
排序后序列:");
for(i=0;i<10;i++){
printf("%ld ",*(data+i));
}
}
测试结果:
objdump -d sort.o
sort.o: file format Mach-O 64-bit x86-64
Disassembly of section __TEXT,__text:
_bubble_p:
0: 55 pushq %rbp
1: 48 89 e5 movq %rsp, %rbp
4: 48 89 7d f8 movq %rdi, -8(%rbp)
8: 48 89 75 f0 movq %rsi, -16(%rbp)
c: 48 8b 75 f8 movq -8(%rbp), %rsi
10: 48 8b 7d f0 movq -16(%rbp), %rdi
14: 48 c1 e7 03 shlq $3, %rdi
18: 48 01 fe addq %rdi, %rsi
1b: 48 83 c6 f8 addq $-8, %rsi
1f: 48 89 75 e0 movq %rsi, -32(%rbp)
23: 48 8b 45 e0 movq -32(%rbp), %rax
27: 48 3b 45 f8 cmpq -8(%rbp), %rax
2b: 0f 86 7d 00 00 00 jbe 125 <_bubble_p+0xAE>
31: 48 8b 45 f8 movq -8(%rbp), %rax
35: 48 89 45 e8 movq %rax, -24(%rbp)
39: 48 8b 45 e8 movq -24(%rbp), %rax
3d: 48 3b 45 e0 cmpq -32(%rbp), %rax
41: 0f 83 51 00 00 00 jae 81 <_bubble_p+0x98>
47: 48 8b 45 e8 movq -24(%rbp), %rax
4b: 48 8b 40 08 movq 8(%rax), %rax
4f: 48 8b 4d e8 movq -24(%rbp), %rcx
53: 48 3b 01 cmpq (%rcx), %rax
56: 0f 8d 26 00 00 00 jge 38 <_bubble_p+0x82>
5c: 48 8b 45 e8 movq -24(%rbp), %rax
60: 48 8b 40 08 movq 8(%rax), %rax
64: 48 89 45 d8 movq %rax, -40(%rbp)
68: 48 8b 45 e8 movq -24(%rbp), %rax
6c: 48 8b 00 movq (%rax), %rax
6f: 48 8b 4d e8 movq -24(%rbp), %rcx
73: 48 89 41 08 movq %rax, 8(%rcx)
77: 48 8b 45 d8 movq -40(%rbp), %rax
7b: 48 8b 4d e8 movq -24(%rbp), %rcx
7f: 48 89 01 movq %rax, (%rcx)
82: e9 00 00 00 00 jmp 0 <_bubble_p+0x87>
87: 48 8b 45 e8 movq -24(%rbp), %rax
8b: 48 83 c0 08 addq $8, %rax
8f: 48 89 45 e8 movq %rax, -24(%rbp)
93: e9 a1 ff ff ff jmp -95 <_bubble_p+0x39>
98: e9 00 00 00 00 jmp 0 <_bubble_p+0x9D>
9d: 48 8b 45 e0 movq -32(%rbp), %rax
a1: 48 83 c0 f8 addq $-8, %rax
a5: 48 89 45 e0 movq %rax, -32(%rbp)
a9: e9 75 ff ff ff jmp -139 <_bubble_p+0x23>
ae: 5d popq %rbp
af: c3 retq
_main:
b0: 55 pushq %rbp
b1: 48 89 e5 movq %rsp, %rbp
b4: 48 81 ec 90 00 00 00 subq $144, %rsp
bb: 48 8d 3d 6e 01 00 00 leaq 366(%rip), %rdi
c2: 48 8d 05 00 00 00 00 leaq (%rip), %rax
c9: b9 50 00 00 00 movl $80, %ecx
ce: 89 ca movl %ecx, %edx
d0: 48 8d 75 a0 leaq -96(%rbp), %rsi
d4: 4c 8b 05 00 00 00 00 movq (%rip), %r8
db: 4d 8b 00 movq (%r8), %r8
de: 4c 89 45 f8 movq %r8, -8(%rbp)
e2: c7 45 9c 00 00 00 00 movl $0, -100(%rbp)
e9: 48 89 7d 88 movq %rdi, -120(%rbp)
ed: 48 89 f7 movq %rsi, %rdi
f0: 48 89 c6 movq %rax, %rsi
f3: e8 00 00 00 00 callq 0 <_main+0x48>
f8: 48 8b 7d 88 movq -120(%rbp), %rdi
fc: b0 00 movb $0, %al
fe: e8 00 00 00 00 callq 0 <_main+0x53>
103: 48 c7 45 90 00 00 00 00 movq $0, -112(%rbp)
10b: 89 45 84 movl %eax, -124(%rbp)
10e: 48 83 7d 90 0a cmpq $10, -112(%rbp)
113: 0f 8d 2b 00 00 00 jge 43 <_main+0x94>
119: 48 8d 3d 21 01 00 00 leaq 289(%rip), %rdi
120: 48 8b 45 90 movq -112(%rbp), %rax
124: 48 8b 74 c5 a0 movq -96(%rbp,%rax,8), %rsi
129: b0 00 movb $0, %al
12b: e8 00 00 00 00 callq 0 <_main+0x80>
130: 89 45 80 movl %eax, -128(%rbp)
133: 48 8b 45 90 movq -112(%rbp), %rax
137: 48 83 c0 01 addq $1, %rax
13b: 48 89 45 90 movq %rax, -112(%rbp)
13f: e9 ca ff ff ff jmp -54 <_main+0x5E>
144: b8 0a 00 00 00 movl $10, %eax
149: 89 c6 movl %eax, %esi
14b: 48 8d 7d a0 leaq -96(%rbp), %rdi
14f: e8 00 00 00 00 callq 0 <_main+0xA4>
154: 48 8d 3d eb 00 00 00 leaq 235(%rip), %rdi
15b: b0 00 movb $0, %al
15d: e8 00 00 00 00 callq 0 <_main+0xB2>
162: 48 c7 45 90 00 00 00 00 movq $0, -112(%rbp)
16a: 89 85 7c ff ff ff movl %eax, -132(%rbp)
170: 48 83 7d 90 0a cmpq $10, -112(%rbp)
175: 0f 8d 2e 00 00 00 jge 46 <_main+0xF9>
17b: 48 8d 3d bf 00 00 00 leaq 191(%rip), %rdi
182: 48 8b 45 90 movq -112(%rbp), %rax
186: 48 8b 74 c5 a0 movq -96(%rbp,%rax,8), %rsi
18b: b0 00 movb $0, %al
18d: e8 00 00 00 00 callq 0 <_main+0xE2>
192: 89 85 78 ff ff ff movl %eax, -136(%rbp)
198: 48 8b 45 90 movq -112(%rbp), %rax
19c: 48 83 c0 01 addq $1, %rax
1a0: 48 89 45 90 movq %rax, -112(%rbp)
1a4: e9 c7 ff ff ff jmp -57 <_main+0xC0>
1a9: 8b 45 9c movl -100(%rbp), %eax
1ac: 48 8b 0d 00 00 00 00 movq (%rip), %rcx
1b3: 48 8b 09 movq (%rcx), %rcx
1b6: 48 8b 55 f8 movq -8(%rbp), %rdx
1ba: 48 39 d1 cmpq %rdx, %rcx
1bd: 89 85 74 ff ff ff movl %eax, -140(%rbp)
1c3: 0f 85 0f 00 00 00 jne 15 <_main+0x128>
1c9: 8b 85 74 ff ff ff movl -140(%rbp), %eax
1cf: 48 81 c4 90 00 00 00 addq $144, %rsp
1d6: 5d popq %rbp
1d7: c3 retq
1d8: e8 00 00 00 00 callq 0 <_main+0x12D>
Y86程序:
- 首先完成mov指令的对应翻译,irmovq,rrmovq,mrmovq,rmmovq。立即数i,寄存器r,内存m,指令的第一个字母表明了源的类型。目的可以是寄存器或内存。
- 四个整数操作指令:addq,subq,andq,xorq,只对寄存器数据进行操作。
- 七个跳转指令jmp,jle,jl,je,jne,jge,jg分支条件和x86_64一样
- 六个条件传送指令:cmovle,cmovl,cmove,cmovne,cmovge,cmovq
- call,ret,pushq,popq同x86_64一样
- halt指令停止指令的执行。
main:
irmovq data,%rdi
irmovq data_end,%rsi
call BubbleP
ret
BubbleP:
jmp L2
L4:
rrmovq 8(%rax), %r9
rrmovq (%rax), %r10
rrmovq %r9, %r8
subq %r10, %r8
jge L3
rmmovq %r10, 8(%rax)
rmmovq %r9, (%rax)
L3:
rrmovq $8, %r8
addq %r8, %rax
jmp L5
L6:
rrmovq %rdi, %rax
L5:
rrmovq %rsi, %r8
subq %rax, %r8
jg L4
irmovq $8, %r8
subq %r8, %rsi
L2:
rrmovq %rsi, %r8
subq %rdi, %r8
jg L6
ret