2017-2018-1 20155318 《信息安全系统设计基础》第八周课下作业

2017-2018-1 20155318 《信息安全系统设计基础》第八周课下作业

作业一

  1. 完成家庭作业4.47,4.48,4.49
  2. 相应代码反汇编成X86-64汇编
  3. 把上述X68-64汇编翻译成Y86-64汇编,并给出相应机器码

作业4.47

  • 书写一个C版本的冒泡排序法,void bubble_a(int *data, int count)用指针引用数组元素,main函数中测试代码如下:
void main()
{
    int data[5]={4,3,2,1,0};
    int i;
    bubble_a(data,5);
    for(i=0;i<5;i++)
    {
        printf("%2d",data[i]);
    }
}
  • 代码链接

  • 运行结果

  • 书写并测试一个由这个函数和测试代码组成的Y86-64程序

  • X86-64汇编:

  • 主函数

main:
.LFB2:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    subq    $48, %rsp
    movq    %fs:40, %rax
    movq    %rax, -8(%rbp)
    xorl    %eax, %eax
    movl    $4, -32(%rbp)
    movl    $3, -28(%rbp)
    movl    $2, -24(%rbp)
    movl    $1, -20(%rbp)
    movl    $0, -16(%rbp)
    leaq    -32(%rbp), %rax
    movl    $5, %esi
    movq    %rax, %rdi
    movl    $0, %eax
    call    bubble_a
    movl    $0, -36(%rbp)
    jmp .L2
.L3:
    movl    -36(%rbp), %eax
    cltq
    movl    -32(%rbp,%rax,4), %eax
    movl    %eax, %esi
    movl    $.LC0, %edi
    movl    $0, %eax
    call    printf
    addl    $1, -36(%rbp)
.L2:
    cmpl    $4, -36(%rbp)
    jle .L3
    movl    $10, %edi
    call    putchar
    nop
    movq    -8(%rbp), %rax
    xorq    %fs:40, %rax
    je  .L4
    call    __stack_chk_fail
.L4:
    leave
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE2:
    .size   main, .-main
    .globl  bubble_a
    .type   bubble_a, @function
  • 功能函数:
bubble_a:
.LFB3:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movq    %rdi, -24(%rbp)
    movl    %esi, -28(%rbp)
    movl    $1, -8(%rbp)
    jmp .L6
.L10:
    movl    -8(%rbp), %eax
    subl    $1, %eax
    movl    %eax, -12(%rbp)
    jmp .L7
.L9:
    movl    -12(%rbp), %eax
    cltq
    addq    $1, %rax
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %edx
    movl    -12(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rcx
    movq    -24(%rbp), %rax
    addq    %rcx, %rax
    movl    (%rax), %eax
    cmpl    %eax, %edx
    jge .L8
    movl    -12(%rbp), %eax
    cltq
    addq    $1, %rax
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %eax
    movl    %eax, -4(%rbp)
    movl    -12(%rbp), %eax
    cltq
    addq    $1, %rax
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rax, %rdx
    movl    -12(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rcx
    movq    -24(%rbp), %rax
    addq    %rcx, %rax
    movl    (%rax), %eax
    movl    %eax, (%rdx)
    movl    -12(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rax, %rdx
    movl    -4(%rbp), %eax
    movl    %eax, (%rdx)
.L8:
    subl    $1, -12(%rbp)
.L7:
    cmpl    $0, -12(%rbp)
    jns .L9
    addl    $1, -8(%rbp)
.L6:
    movl    -8(%rbp), %eax
    cmpl    -28(%rbp), %eax
    jl  .L10
    nop
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE3:
    .size   bubble_a, .-bubble_a
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609"
    .section    .note.GNU-stack,"",@progbits
  • X86-64机器码:
000000000000009e <bubble_a>:
  9e:   55                      push   %rbp
  9f:   48 89 e5                mov    %rsp,%rbp
  a2:   48 89 7d e8             mov    %rdi,-0x18(%rbp)
  a6:   89 75 e4                mov    %esi,-0x1c(%rbp)
  a9:   c7 45 f8 01 00 00 00    movl   $0x1,-0x8(%rbp)
  b0:   e9 ba 00 00 00          jmpq   16f <bubble_a+0xd1>
  b5:   8b 45 f8                mov    -0x8(%rbp),%eax
  b8:   83 e8 01                sub    $0x1,%eax
  bb:   89 45 f4                mov    %eax,-0xc(%rbp)
  be:   e9 9e 00 00 00          jmpq   161 <bubble_a+0xc3>
  c3:   8b 45 f4                mov    -0xc(%rbp),%eax
  c6:   48 98                   cltq   
  c8:   48 83 c0 01             add    $0x1,%rax
  cc:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
  d3:   00 
  d4:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  d8:   48 01 d0                add    %rdx,%rax
  db:   8b 10                   mov    (%rax),%edx
  dd:   8b 45 f4                mov    -0xc(%rbp),%eax
  e0:   48 98                   cltq   
  e2:   48 8d 0c 85 00 00 00    lea    0x0(,%rax,4),%rcx
  e9:   00 
  ea:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  ee:   48 01 c8                add    %rcx,%rax
  f1:   8b 00                   mov    (%rax),%eax
  f3:   39 c2                   cmp    %eax,%edx
  f5:   7d 66                   jge    15d <bubble_a+0xbf>
  f7:   8b 45 f4                mov    -0xc(%rbp),%eax
  fa:   48 98                   cltq   
  fc:   48 83 c0 01             add    $0x1,%rax
 100:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
 107:   00 
 108:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 10c:   48 01 d0                add    %rdx,%rax
 10f:   8b 00                   mov    (%rax),%eax
 111:   89 45 fc                mov    %eax,-0x4(%rbp)
 114:   8b 45 f4                mov    -0xc(%rbp),%eax
 117:   48 98                   cltq   
 119:   48 83 c0 01             add    $0x1,%rax
 11d:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
 124:   00 
 125:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 129:   48 01 c2                add    %rax,%rdx
 12c:   8b 45 f4                mov    -0xc(%rbp),%eax
 12f:   48 98                   cltq   
 131:   48 8d 0c 85 00 00 00    lea    0x0(,%rax,4),%rcx
 138:   00 
 139:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 13d:   48 01 c8                add    %rcx,%rax
 140:   8b 00                   mov    (%rax),%eax
 142:   89 02                   mov    %eax,(%rdx)
 144:   8b 45 f4                mov    -0xc(%rbp),%eax
 147:   48 98                   cltq   
 149:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
 150:   00 
 151:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 155:   48 01 c2                add    %rax,%rdx
 158:   8b 45 fc                mov    -0x4(%rbp),%eax
 15b:   89 02                   mov    %eax,(%rdx)
 15d:   83 6d f4 01             subl   $0x1,-0xc(%rbp)
 161:   83 7d f4 00             cmpl   $0x0,-0xc(%rbp)
 165:   0f 89 58 ff ff ff       jns    c3 <bubble_a+0x25>
 16b:   83 45 f8 01             addl   $0x1,-0x8(%rbp)
 16f:   8b 45 f8                mov    -0x8(%rbp),%eax
 172:   3b 45 e4                cmp    -0x1c(%rbp),%eax
 175:   0f 8c 3a ff ff ff       jl     b5 <bubble_a+0x17>
 17b:   90                      nop
 17c:   5d                      pop    %rbp
 17d:   c3                      retq
  • Y86-64汇编:
bubble_c:  
.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_c, .-bubble_c  
    .section    .rodata.str1.1,"aMS",@progbits,1

作业4.48

不使用跳转,最多使用3次条件传送

  1 #include<stdio.h>
  2 
  3 void bubble_a(long *data,long count)
  4 {
  5     long i,last,t,j;
  6     for(last = count-1;last>0;last--)
  7     {
  8         for(i = 0;i<last;i++)
  9         {
 10            t=*(data+i)-*(data+i+1); 
 11            if(t>0){
 12                 *(data+i)=t;
 13                 *(data+i)=*(data+i+1);
 14                 *(data+i+1)=t;
 15             }
 16         }
 17     }
 18 }
 19 int main()
 20 {
 21     int i;
 22     long data[10] = {2,3,4,5,6,7,8,9,0,1};
 23     bubble_a(data,10);
 24     for(i=0;i<10;i++)
 25     {
 26         printf("%d\n",*(data+i));
 27     }
 28     return 0;
 29 }

X86-64汇编:

bubble_c:
.LFB3:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movq    %rdi, -24(%rbp)
    movl    %esi, -28(%rbp)
    movl    $1, -12(%rbp)
    jmp .L6
.L9:
    movl    -12(%rbp), %eax
    subl    $1, %eax
    movl    %eax, -16(%rbp)
    jmp .L7
.L8:
    movl    -16(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %eax
    movl    %eax, -8(%rbp)
    movl    -16(%rbp), %eax
    cltq
    addq    $1, %rax
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %eax
    movl    %eax, -4(%rbp)
    movl    -16(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rax, %rdx
    movl    -4(%rbp), %eax
    cmpl    %eax, -8(%rbp)
    cmovle  -8(%rbp), %eax
    movl    %eax, (%rdx)
    movl    -16(%rbp), %eax
    cltq
    addq    $1, %rax
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rax, %rdx
    movl    -8(%rbp), %eax
    cmpl    %eax, -4(%rbp)
    cmovge  -4(%rbp), %eax
    movl    %eax, (%rdx)
    subl    $1, -16(%rbp)
.L7:
    cmpl    $0, -16(%rbp)
    jns .L8
    addl    $1, -12(%rbp)
.L6:
    movl    -12(%rbp), %eax
    cmpl    -28(%rbp), %eax
    jl  .L9
    nop
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE3:
    .size   bubble_c, .-bubble_c
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609"
    .section    .note.GNU-stack,"",@progbits

X86-64机器码:

000000000000009e <bubble_c>:
  9e:   55                      push   %rbp
  9f:   48 89 e5                mov    %rsp,%rbp
  a2:   48 89 7d e8             mov    %rdi,-0x18(%rbp)
  a6:   89 75 e4                mov    %esi,-0x1c(%rbp)
  a9:   c7 45 f4 01 00 00 00    movl   $0x1,-0xc(%rbp)
  b0:   e9 97 00 00 00          jmpq   14c <bubble_c+0xae>
  b5:   8b 45 f4                mov    -0xc(%rbp),%eax
  b8:   83 e8 01                sub    $0x1,%eax
  bb:   89 45 f0                mov    %eax,-0x10(%rbp)
  be:   eb 7e                   jmp    13e <bubble_c+0xa0>
  c0:   8b 45 f0                mov    -0x10(%rbp),%eax
  c3:   48 98                   cltq   
  c5:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
  cc:   00 
  cd:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  d1:   48 01 d0                add    %rdx,%rax
  d4:   8b 00                   mov    (%rax),%eax
  d6:   89 45 f8                mov    %eax,-0x8(%rbp)
  d9:   8b 45 f0                mov    -0x10(%rbp),%eax
  dc:   48 98                   cltq   
  de:   48 83 c0 01             add    $0x1,%rax
  e2:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
  e9:   00 
  ea:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  ee:   48 01 d0                add    %rdx,%rax
  f1:   8b 00                   mov    (%rax),%eax
  f3:   89 45 fc                mov    %eax,-0x4(%rbp)
  f6:   8b 45 f0                mov    -0x10(%rbp),%eax
  f9:   48 98                   cltq   
  fb:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
 102:   00 
 103:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 107:   48 01 c2                add    %rax,%rdx
 10a:   8b 45 fc                mov    -0x4(%rbp),%eax
 10d:   39 45 f8                cmp    %eax,-0x8(%rbp)
 110:   0f 4e 45 f8             cmovle -0x8(%rbp),%eax
 114:   89 02                   mov    %eax,(%rdx)
 116:   8b 45 f0                mov    -0x10(%rbp),%eax
 119:   48 98                   cltq   
 11b:   48 83 c0 01             add    $0x1,%rax
 11f:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
 126:   00 
 127:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 12b:   48 01 c2                add    %rax,%rdx
 12e:   8b 45 f8                mov    -0x8(%rbp),%eax
 131:   39 45 fc                cmp    %eax,-0x4(%rbp)
 134:   0f 4d 45 fc             cmovge -0x4(%rbp),%eax
 138:   89 02                   mov    %eax,(%rdx)
 13a:   83 6d f0 01             subl   $0x1,-0x10(%rbp)
 13e:   83 7d f0 00             cmpl   $0x0,-0x10(%rbp)
 142:   0f 89 78 ff ff ff       jns    c0 <bubble_c+0x22>
 148:   83 45 f4 01             addl   $0x1,-0xc(%rbp)
 14c:   8b 45 f4                mov    -0xc(%rbp),%eax
 14f:   3b 45 e4                cmp    -0x1c(%rbp),%eax
 152:   0f 8c 5d ff ff ff       jl     b5 <bubble_c+0x17>
 158:   90                      nop
 159:   5d                      pop    %rbp
 15a:   c3                      retq   

Y86-64汇编:

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:  
    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  
.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",@progbits,1

作业4.49

不使用跳转,最多使用1次条件传送,需借助栈来保存其中一个操作数的值。代码如下:

  1 #include<stdio.h>
  2 #include"duizhan.h"
  3 
  4 void bubble_a(long *data,long count)
  5 {
  6     long i,last,t,j;
  7     struct Stack* ps;
  8     init(ps);
  9     for(last = count-1;last>0;last--)
 10     {
 11         for(i = 0;i<last;i++)
 12         {
 13              j=*(data+i)-*(data+i+1);
 14             if(j>0)
 15             {
 16                 push(ps,*(data+i+1));
 17                 *(data+i+1)=*(data+i);
 18                 pop(ps,data+i);
 19             }
 20         }
 21     }
 22 }
 23 int main()
 24 {
 25     int i;
 26     long data[10] = {2,3,4,5,6,7,8,9,0,1};
 27     bubble_a(data,10);
 28     for(i=0;i<10;i++)
 29     {
 30         printf("%d\n",*(data+i));
 31     }
 32     return 0;
 33 }

需要先编辑一个包含栈操作的头文件duizhan.h,其中包含栈的初始化函数init(),入栈函数push(),弹栈函数pop()

  • X86-64汇编:
bubble_c:
.LFB3:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movq    %rdi, -24(%rbp)
    movl    %esi, -28(%rbp)
    movl    $1, -12(%rbp)
    jmp .L6
.L9:
    movl    -12(%rbp), %eax
    subl    $1, %eax
    movl    %eax, -16(%rbp)
    jmp .L7
.L8:
    movl    -16(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %eax
    movl    %eax, -8(%rbp)
    movl    -16(%rbp), %eax
    cltq
    addq    $1, %rax
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rdx, %rax
    movl    (%rax), %eax
    movl    %eax, -4(%rbp)
    movl    -16(%rbp), %eax
    cltq
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rax, %rdx
    movl    -4(%rbp), %eax
    cmpl    %eax, -8(%rbp)
    cmovle  -8(%rbp), %eax
    movl    %eax, (%rdx)
    movl    -16(%rbp), %eax
    cltq
    addq    $1, %rax
    leaq    0(,%rax,4), %rdx
    movq    -24(%rbp), %rax
    addq    %rax, %rdx
    movl    -8(%rbp), %eax
    movl    %eax, (%rdx)
    subl    $1, -16(%rbp)
.L7:
    cmpl    $0, -16(%rbp)
    jns .L8
    addl    $1, -12(%rbp)
.L6:
    movl    -12(%rbp), %eax
    cmpl    -28(%rbp), %eax
    jl  .L9
    nop
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE3:
    .size   bubble_c, .-bubble_c
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609"
    .section    .note.GNU-stack,"",@progbits
  • X86-64机器码:
0000000000000094 <bubble_c>:
  94:   55                      push   %rbp
  95:   48 89 e5                mov    %rsp,%rbp
  98:   48 89 7d e8             mov    %rdi,-0x18(%rbp)
  9c:   89 75 e4                mov    %esi,-0x1c(%rbp)
  9f:   c7 45 f4 01 00 00 00    movl   $0x1,-0xc(%rbp)
  a6:   e9 8c 00 00 00          jmpq   137 <bubble_c+0xa3>
  ab:   8b 45 f4                mov    -0xc(%rbp),%eax
  ae:   83 e8 01                sub    $0x1,%eax
  b1:   89 45 f0                mov    %eax,-0x10(%rbp)
  b4:   eb 77                   jmp    12d <bubble_c+0x99>
  b6:   8b 45 f0                mov    -0x10(%rbp),%eax
  b9:   48 98                   cltq   
  bb:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
  c2:   00 
  c3:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  c7:   48 01 d0                add    %rdx,%rax
  ca:   8b 00                   mov    (%rax),%eax
  cc:   89 45 f8                mov    %eax,-0x8(%rbp)
  cf:   8b 45 f0                mov    -0x10(%rbp),%eax
  d2:   48 98                   cltq   
  d4:   48 83 c0 01             add    $0x1,%rax
  d8:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
  df:   00 
  e0:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  e4:   48 01 d0                add    %rdx,%rax
  e7:   8b 00                   mov    (%rax),%eax
  e9:   89 45 fc                mov    %eax,-0x4(%rbp)
  ec:   8b 45 f0                mov    -0x10(%rbp),%eax
  ef:   48 98                   cltq   
  f1:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
  f8:   00 
  f9:   48 8b 45 e8             mov    -0x18(%rbp),%rax
  fd:   48 01 c2                add    %rax,%rdx
 100:   8b 45 fc                mov    -0x4(%rbp),%eax
 103:   39 45 f8                cmp    %eax,-0x8(%rbp)
 106:   0f 4e 45 f8             cmovle -0x8(%rbp),%eax
 10a:   89 02                   mov    %eax,(%rdx)
 10c:   8b 45 f0                mov    -0x10(%rbp),%eax
 10f:   48 98                   cltq   
 111:   48 83 c0 01             add    $0x1,%rax
 115:   48 8d 14 85 00 00 00    lea    0x0(,%rax,4),%rdx
 11c:   00 
 11d:   48 8b 45 e8             mov    -0x18(%rbp),%rax
 121:   48 01 c2                add    %rax,%rdx
 124:   8b 45 f8                mov    -0x8(%rbp),%eax
 127:   89 02                   mov    %eax,(%rdx)
 129:   83 6d f0 01             subl   $0x1,-0x10(%rbp)
 12d:   83 7d f0 00             cmpl   $0x0,-0x10(%rbp)
 131:   79 83                   jns    b6 <bubble_c+0x22>
 133:   83 45 f4 01             addl   $0x1,-0xc(%rbp)
 137:   8b 45 f4                mov    -0xc(%rbp),%eax
 13a:   3b 45 e4                cmp    -0x1c(%rbp),%eax
 13d:   0f 8c 68 ff ff ff       jl     ab <bubble_c+0x17>
 143:   90                      nop
 144:   5d                      pop    %rbp
 145:   c3                      retq
  • Y86-64汇编:
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:  
    movl    (%ebx,%eax,4), %edx  
    movl    4(%ebx,%eax,4), %ecx  
    cmpl    %edx, %ecx  
    movl    %edx, %ebp  
    cmovle  %ecx, %ebp  
    movl    %edx, 4(%ebx,%eax,4)  
    subl    $1, %eax  
    cmpl    $-1, %eax  
    jne .L6  
.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",@progbits,1

多进程实现daytime服务器

  • 服务器
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sys/socket.h>
#include<sys/types.h>       //pthread_t , pthread_attr_t and so on.
#include<netinet/in.h>      //structure sockaddr_in
#include<arpa/inet.h>       //Func : htonl; htons; ntohl; ntohs
#include<assert.h>          //Func :assert
#include<string.h>          //Func :memset
#include<unistd.h>          //Func :close,write,read
#include<ctype.h>
#include<arpa/inet.h>
#include <time.h>
#define SOCK_PORT 9988
#define BUFFER_LENGTH 1024
#define MAX_CONN_LIMIT 5    //MAX connection limit

static void Data_handle(void * sock_fd);
struct sockaddr_in s_addr_in;
struct sockaddr_in s_addr_client;
int sockfd;

int main()
{
int sockfd_server;
int fd_temp;
int client_length;

sockfd_server = socket(AF_INET,SOCK_STREAM,0);  //ipv4,TCP
assert(sockfd_server != -1);

//before bind(), set the attr of structure sockaddr.
memset(&s_addr_in,0,sizeof(s_addr_in));
s_addr_in.sin_family = AF_INET;
s_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);  //trans addr from uint32_t host byte order to network byte order.
s_addr_in.sin_port = htons(SOCK_PORT);          //trans port from uint16_t host byte order to network byte order.

fd_temp = bind(sockfd_server,(struct scokaddr*)(&s_addr_in),sizeof(s_addr_in));

if(fd_temp == -1)
{
fprintf(stderr,"bind error!\n");
exit(1);
}

fd_temp = listen(sockfd_server,MAX_CONN_LIMIT);

if(fd_temp == -1)
{
fprintf(stderr,"listen error!\n");
exit(1);
}

while(1)
{
//printf("waiting for new connection...\n");
pthread_t thread_id;
client_length = sizeof(s_addr_client);

//Block here. Until server accpets a new connection.
sockfd = accept(sockfd_server,(struct sockaddr*)(&s_addr_client),&client_length);

time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A\n\t", localtime(&t) );
send(sockfd,tmp,strlen(tmp),0);
close(sockfd);  //close a file descriptor.
if(sockfd == -1)
{
fprintf(stderr,"Accept error!\n");
continue;                               //ignore current socket ,continue while loop.
}

if(pthread_create(&thread_id,NULL,(void *)(&Data_handle),(void *)(&sockfd)) == -1)
{
fprintf(stderr,"pthread_create error!\n");
break;                                  //break while loop
}
}

//Clear
int ret = shutdown(sockfd_server,SHUT_WR); //shut down the all or part of a full-duplex connection.
assert(ret != -1);

return 0;
}

static void Data_handle(void * sock_fd)
{
int fd = *((int *)sock_fd);
int i_recvBytes;
char data_recv[BUFFER_LENGTH];

printf("服务器实现者20155318\t");
printf("客户端IP:%s\n",inet_ntoa(s_addr_client.sin_addr));
pthread_exit(NULL);   //terminate calling thread!
}
  • 客户端
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<ctype.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/wait.h>
#include<signal.h>
#include<errno.h>
#include<pthread.h>
#include <pthread.h>

#define SERV_PORT 13321
#define SERV_IP "127.0.0.1"
#define NUM 3

int std_err(const char* name)
{
    perror(name);
	    exit(1);
}
int main(void)
{
	int cfd, ret;
	char buf[BUFSIZ];
	pid_t pid;
	int i;
	//创建套节字
	cfd = socket(AF_INET, SOCK_STREAM, 0);
	if(cfd == -1)
		std_err("socket");
	//定义IP , 端口
	struct sockaddr_in clie_addr;
	clie_addr.sin_family = AF_INET;
	clie_addr.sin_port = htons(SERV_PORT);
	//转换IP 字符串的地址
	ret = inet_pton(AF_INET, SERV_IP, &clie_addr.sin_addr.s_addr);
	if(ret != 1)
		std_err("inet_pton");
	//链接服务器
	ret = connect(cfd, (struct sockaddr*)&clie_addr, sizeof(clie_addr));
	if(ret == -1)
	std_err("connect");
	char buff[256];
	int nRecv=recv(cfd,buff,256,0);
	if(nRecv>0)
	{   
		buff[nRecv]='\0';
		printf("当前时间:%s\n",buff);
	} 
	
	//关闭套节字
	close(cfd);
	return 0;

}
  • 运行截图
posted @ 2017-11-12 23:52  lxy1997  阅读(164)  评论(0编辑  收藏  举报