2017-2018-1 20155312 《信息安全系统设计基础》第八周学习总结
2017-2018-1 20155312 《信息安全系统设计基础》第八周学习总结
题目要求
- 1 完成家庭作业4.47,4.48,4.49
- 2 相应代码反汇编成X86-64汇编
- 3 把上述X86-64汇编翻译成Y86汇编,并给出相应机器码
解答过程
4.47
A
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;
}
}
}
B
bubble_b:
.LFB22:
.cfi_startproc
pushl %edi
.cfi_def_cfa_offset 8
.cfi_offset 7, -8
pushl %esi
.cfi_def_cfa_offset 12
.cfi_offset 6, -12
pushl %ebx
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
movl 16(%esp), %edx
movl 20(%esp), %edi
cmpl $1, %edi
jle .L1
subl $1, %edi
movl $0, %esi
.L6:
movl %esi, %eax
testl %esi, %esi
js .L3
.L7:
movl 4(%edx,%eax,4), %ecx
movl (%edx,%eax,4), %ebx
cmpl %ebx, %ecx
jge .L4
movl %ebx, 4(%edx,%eax,4)
movl %ecx, (%edx,%eax,4)
.L4:
subl $1, %eax
cmpl $-1, %eax
jne .L7
.L3:
addl $1, %esi
cmpl %edi, %esi
jne .L6
.L1:
popl %ebx
.cfi_def_cfa_offset 12
.cfi_restore 3
popl %esi
.cfi_def_cfa_offset 8
.cfi_restore 6
popl %edi
.cfi_def_cfa_offset 4
.cfi_restore 7
ret
.cfi_endproc
.LFE22:
.size bubble_b, .-bubble_b
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d "
.text
.globl main
.type main, @function
Y86
bubble_b:
.LFB22:
.cfi_startproc
pushl %edi
.cfi_def_cfa_offset 8
.cfi_offset 7, -8
pushl %esi
.cfi_def_cfa_offset 12
.cfi_offset 6, -12
pushl %ebx
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
mrmovl 16(%esp), %edx
mrmovl 20(%esp), %edi
irmovl $1, %eax
subl %eax, %edi
jle .L1
subl $1, %edi
irmovl $0, %esi
.L6:
rrmovl %esi, %eax
irmovl $0 , ebx
subl %ebx, %esi
jl .L3
.L7:
rrmovl %eax, %ecx
addl %ecx, %ecx
addl %ecx, %ecx
addl %edx, %ecx
mrmovl 4(%ecx), %ecx
rrmovl %eax, %ebx
addl %ecx, %ebx
addl %ecx, %ebx
addl %edx, %ebx
mrmovl (%ebx), %ebx
subl %ebx, %ecx
jge .L4
addl %eax, %eax
addl %eax, %eax
addl %edx, %eax
rmmovl %ebx, 4(%eax)
addl %eax, %eax
addl %eax, %eax
addl %edx, %eax
rmmovl %ecx, 4(%eax)
.L4:
subl $1, %eax
irmovl $-1, %edx
subl %edx, %eax
jne .L7
.L3:
addl $1, %esi
subl %edi, %esi
jne .L6
.L1:
popl %ebx
.cfi_def_cfa_offset 12
.cfi_restore 3
popl %esi
.cfi_def_cfa_offset 8
.cfi_restore 6
popl %edi
.cfi_def_cfa_offset 4
.cfi_restore 7
ret
.cfi_endproc
.LFE22:
.size bubble_b, .-bubble_b
.section .rodata.str1.1,"aMS",@prog bits,1
4.48
修改4.47代码,实现冒泡排序函数的测试和交换,要求不使用跳转,且最多使用3次条件传送。
void bubble_c(int *data,int count)
{
int i , next;
int pre_ele,next_ele;
for(next = 1;next < count;next++)
{
for(i = next -1;i >= 0;i--)
{
pre_ele = *(data + i);
next_ele = *(data + i + 1);
*(data + i) = next_ele < pre_ele ? next_ele : pre_ele;
*(data + i + 1) = next_ele < pre_ele ? pre_ele : next_ele;
}
}
}
Y86代码
.L6:
movl (%ebx,%eax,4), %edx
movl 4(%ebx,%eax,4), %ecx
cmpl %edx, %ecx
movl %edx, %ebp
cmovle %ecx, %ebp
movl %ebp, (%ebx,%eax,4)
cmovge %ecx, %edx
movl %edx, 4(%ebx,%eax,4)
subl $1, %eax
cmpl $-1, %eax
jne .L6
4.49
void bubble(int *data, int count)
{
if(count == 0) return;
int i, j;
int *p, *q;
for(i=count-1; i!=0; i--){
p = data, q = data + 1;
for(j=0; j!=i; ++j)
{
if( *p > *q )
{
int t = *p;
*p = *q;
*q = t;
}
p++, q++;
}
}
}