完成家庭作业4.47,4.48,4.49
相应代码反汇编成X86-64汇编
把上述X86-64汇编翻译成Y86汇编,并给出相应机器码
4.47:
书写一个C版本的冒泡排序法,用指针引用数组元素,而不是数组索引。
书写并测试这个函数和测试代码组成的Y86-64程序。
#include<stdio.h>
void bubble_a(int *data, int count){
int i,next;
for(next = 1; next < count; next++){
for(i = next - 1; i >= 0; i--)
if(*(data + i + 1) < *(data + i)){
int t = *(data + i + 1);
*(data + i + 1) = *(data + i);
*(data + i) = t;
}
}
}
void main()
{
int data[5]={3,90,1,6,0};
int i;
bubble_a(data,5);
for(i=0;i<5;i++)
{
printf("%2d\n",data[i]);
}
}
0x0000: | Disassembly of section .text:
|
0x0000: | 0000000000000000 <bubble_a>:
0x0000: | 0: 83 fe 01 cmp $0x1,%esi
0x0000: | 3: 7e 41 jle 46 <bubble_a+0x46>
0x0000: | 5: 44 8d 4e fe lea -0x2(%rsi),%r9d
0x0000: | 9: 4c 8d 47 04 lea 0x4(%rdi),%r8
0x0000: | d: 31 f6 xor %esi,%esi
0x0000: | f: 49 83 c1 01 add $0x1,%r9
0x0000: | 13: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
0x0000: | 18: 85 f6 test %esi,%esi
0x0000: | 1a: 4c 89 c0 mov %r8,%rax
0x0000: | 1d: 78 18 js 37 <bubble_a+0x37>
0x0000: | 1f: 90 nop
0x0000: | 20: 8b 10 mov (%rax),%edx
0x0000: | 22: 8b 48 fc mov -0x4(%rax),%ecx
0x0000: | 25: 39 ca cmp %ecx,%edx
0x0000: | 27: 7d 05 jge 2e <bubble_a+0x2e>
0x0000: | 29: 89 08 mov %ecx,(%rax)
0x0000: | 2b: 89 50 fc mov %edx,-0x4(%rax)
0x0000: | 2e: 48 83 e8 04 sub $0x4,%rax
0x0000: | 32: 48 39 f8 cmp %rdi,%rax
0x0000: | 35: 75 e9 jne 20 <bubble_a+0x20>
0x0000: | 37: 48 83 c6 01 add $0x1,%rsi
0x0000: | 3b: 49 83 c0 04 add $0x4,%r8
0x0000: | 3f: 4c 39 ce cmp %r9,%rsi
0x0000: | 42: 75 d4 jne 18 <bubble_a+0x18>
|
|
0x0000: | Disassembly of section .text.startup:
|
0x0000: | 0000000000000000 <main>:
0x0000: | 0: 55 push %rbp
0x0000: | 1: 53 push %rbx
|
0x0000: | 7: 48 83 ec 28 sub $0x28,%rsp
0x0000: | b: 48 8d 6c 24 14 lea 0x14(%rsp),%rbp
0x0000: | 10: 48 89 e7 mov %rsp,%rdi
0x0000: | 13: 48 89 e3 mov %rsp,%rbx
0x0000: | 16: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
0x0000: | 1d: 00 00
0x0000: | 1f: 48 89 44 24 18 mov %rax,0x18(%rsp)
0x0000: | 24: 31 c0 xor %eax,%eax
|
0x0000: | 34: 00
|
0x0000: | 3c: 00
|
0x0000: | 44: 00
|
0x0000: | 60: 48 83 c3 04 add $0x4,%rbx
|
0x0000: | 69: 48 39 eb cmp %rbp,%rbx
0x0000: | 6c: 75 e4 jne 52 <main+0x52>
0x0000: | 4c: 00
|
0x0000: | 52: 8b 13 mov (%rbx),%edx
0x0000: | 54: 31 c0 xor %eax,%eax
|
0x0000: | 6e: 48 8b 44 24 18 mov 0x18(%rsp),%rax
0x0000: | 73: 64 48 33 04 25 28 00 xor %fs:0x28,%rax
0x0000: | 7a: 00 00
0x0000: | 7c: 75 07 jne 85 <main+0x85>
0x0000: | 7e: 48 83 c4 28 add $0x28,%rsp
4.48:实现冒泡排序,要求不使用跳转,且最多使用3次条件传送。
0x0000: | Disassembly of section .text:
|
0x0000: | 0000000000000000 <bubble_a>:
0x0000: | 0: 83 fe 01 cmp $0x1,%esi
0x0000: | 3: 7e 48 jle 4d <bubble_a+0x4d>
0x0000: | 5: 44 8d 56 fe lea -0x2(%rsi),%r10d
0x0000: | 9: 4c 8d 47 fc lea -0x4(%rdi),%r8
0x0000: | d: 45 31 c9 xor %r9d,%r9d
0x0000: | 10: 49 83 c2 01 add $0x1,%r10
0x0000: | 14: 0f 1f 40 00 nopl 0x0(%rax)
0x0000: | 18: 45 85 c9 test %r9d,%r9d
0x0000: | 1b: 48 89 f8 mov %rdi,%rax
0x0000: | 1e: 78 1e js 3e <bubble_a+0x3e>
0x0000: | 20: 8b 10 mov (%rax),%edx
0x0000: | 22: 8b 48 04 mov 0x4(%rax),%ecx
0x0000: | 25: 39 ca cmp %ecx,%edx
0x0000: | 27: 89 ce mov %ecx,%esi
0x0000: | 29: 0f 4e f2 cmovle %edx,%esi
0x0000: | 2c: 0f 4c d1 cmovl %ecx,%edx
0x0000: | 2f: 48 83 e8 04 sub $0x4,%rax
0x0000: | 33: 89 70 04 mov %esi,0x4(%rax)
0x0000: | 36: 89 50 08 mov %edx,0x8(%rax)
0x0000: | 39: 4c 39 c0 cmp %r8,%rax
0x0000: | 3c: 75 e2 jne 20 <bubble_a+0x20>
0x0000: | 3e: 49 83 c1 01 add $0x1,%r9
0x0000: | 42: 48 83 c7 04 add $0x4,%rdi
0x0000: | 46: 4d 39 d1 cmp %r10,%r9
0x0000: | 49: 75 cd jne 18 <bubble_a+0x18>
|
0x0000: | Disassembly of section .text.startup:
|
0x0000: | 0000000000000000 <main>:
0x0000: | 0: 55 push %rbp
0x0000: | 1: 53 push %rbx
|
0x0000: | 7: 48 83 ec 28 sub $0x28,%rsp
0x0000: | b: 48 8d 6c 24 14 lea 0x14(%rsp),%rbp
0x0000: | 10: 48 89 e7 mov %rsp,%rdi
0x0000: | 13: 48 89 e3 mov %rsp,%rbx
0x0000: | 16: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax
0x0000: | 1d: 00 00
0x0000: | 1f: 48 89 44 24 18 mov %rax,0x18(%rsp)
0x0000: | 24: 31 c0 xor %eax,%eax
|
0x0000: | 34: 00
0x0000: | 35:
0x0000: | 3c: 00
0x0000: | 3d:
0x0000: | 44: 00
0x0000: | 45:
0x0000: | 4c: 00
|
0x0000: | 52: 8b 13 mov (%rbx),%edx
0x0000: | 54: 31 c0 xor %eax,%eax
|
0x0000: | 60: 48 83 c3 04 add $0x4,%rbx
|
0x0000: | 69: 48 39 eb cmp %rbp,%rbx
0x0000: | 6c: 75 e4 jne 52 <main+0x52>
0x0000: | 6e: 48 8b 44 24 18 mov 0x18(%rsp),%rax
0x0000: | 73: 64 48 33 04 25 28 00 xor %fs:0x28,%rax
0x0000: | 7a: 00 00
0x0000: | 7c: 75 07 jne 85 <main+0x85>
0x0000: | 7e: 48 83 c4 28 add $0x28,%rsp
0x0000: | 82: 5b pop %rbx
0x0000: | 83: 5d pop %rbp
4.49:实现冒泡排序,要求不使用跳转,且最多使用1次条件传送。