zoukankan      html  css  js  c++  java
  • 64位linux下玩32位汇编编程

      利用下假期,打算把linux下的汇编语言给熟悉下,结果是以32位为版本的,只能在办公室的机器上跑了个opensuse的32位版本,家里的suse挂了,无法输入中文。打算再安装下32位系统,今天找到了个解决方法,记录如下:

    代码如下,文件名位test32.s:  

     1 .section .data
     2   
     3 .section .text
     4 
     5 .globl _start
     6 _start:
     7     pushl $2
     8     pushl $1
     9     call sumer
    10     addl $8, %esp
    11     movl %eax, %ebx
    12     movl $1, %eax
    13     int $0x80
    14 
    15 .type sumer, @function
    16 
    17 sumer:                                                                                                                                             
    18     pushl %ebp
    19     movl %esp, %ebp
    20     movl 8(%ebp), %eax
    21     movl 12(%ebp), %ecx
    22     addl %ecx, %eax
    23     popl %ebp
    24     ret

      无法按照原来的方式,直接用as  test32.s  -o  test32.o汇编

                直接用ld  test32.o -o test32链接

      直接报错,由于我的linux是64位,解决方法就是在两个命令选项中加上适当的选项即可。

      正确的命令是这样的,直接用as  test32.s  -o  test32.o  --32 汇编

                直接用ld -m  elf_i386  test32.o -o test32链接  

           其中:-m参数是让ld模仿后面跟的连接器,也就是elf_i386格式的连接器,

                     --32参数是使用32位个是的汇编进行代码汇编,

    如果有以下代码test321.c

     1 #include <stdio.h>
     2 
     3 int factorial(int num){
     4     if(1 == num){
     5         return 1;
     6     }
     7     return num * factorial(num - 1);
     8 }                                                                               
     9 
    10 int main(int argc, char **argv)
    11 {
    12     printf("factorial(5): %d
    ", factorial(5));
    13 
    14     return 0;
    15 }

    在64位系统中,直接使用gcc test321.c  -S  test321.s,64位汇编代码如下

      1     .file   "test321.c"                                                                                                                                                                                      
      2     .text
      3     .globl  factorial
      4     .type   factorial, @function
      5 factorial:
      6 .LFB0:
      7     .cfi_startproc
      8     pushq   %rbp
      9     .cfi_def_cfa_offset 16
     10     .cfi_offset 6, -16
     11     movq    %rsp, %rbp
     12     .cfi_def_cfa_register 6
     13     subq    $16, %rsp
     14     movl    %edi, -4(%rbp)
     15     cmpl    $1, -4(%rbp)
     16     jne .L2
     17     movl    $1, %eax
     18     jmp .L3
     19 .L2:
     20     movl    -4(%rbp), %eax
     21     subl    $1, %eax
     22     movl    %eax, %edi
     23     call    factorial
     24     imull   -4(%rbp), %eax
     25 .L3:
     26     leave
     27     .cfi_def_cfa 7, 8
     28     ret
     29     .cfi_endproc
     30 .LFE0:
     31     .size   factorial, .-factorial
     32     .section    .rodata
     33 .LC0:
     34     .string "factorial(5): %d
    "
     35     .text
     36     .globl  main
     37     .type   main, @function
     38 main:
     39 .LFB1:
     40     .cfi_startproc
     41     pushq   %rbp
     42     .cfi_def_cfa_offset 16
     43     .cfi_offset 6, -16
     44     movq    %rsp, %rbp
     45     .cfi_def_cfa_register 6
     46     subq    $16, %rsp
     47     movl    %edi, -4(%rbp)
     48     movq    %rsi, -16(%rbp)
     49     movl    $5, %edi
     50     call    factorial
     51     movl    %eax, %esi
     52     leaq    .LC0(%rip), %rdi
     53     movl    $0, %eax
     54     call    printf@PLT
     55     movl    $0, %eax
     56     leave
     57     .cfi_def_cfa 7, 8
     58     ret
     59     .cfi_endproc
     60 .LFE1:
     61     .size   main, .-main
     62     .ident  "GCC: (GNU) 9.1.0"
     63     .section    .note.GNU-stack,"",@progbits
    ~                                                 

    在64位系统中,使用gcc test321.c  -S  -m32  test321.s,32位汇编代码如下

      1  .file   "test321.c"                                                                                                                                                                                          
      2     .text
      3     .globl  factorial
      4     .type   factorial, @function
      5 factorial:
      6 .LFB0:
      7     .cfi_startproc
      8     pushl   %ebp
      9     .cfi_def_cfa_offset 8
     10     .cfi_offset 5, -8
     11     movl    %esp, %ebp
     12     .cfi_def_cfa_register 5
     13     subl    $8, %esp
     14     call    __x86.get_pc_thunk.ax
     15     addl    $_GLOBAL_OFFSET_TABLE_, %eax
     16     cmpl    $1, 8(%ebp)
     17     jne .L2
     18     movl    $1, %eax
     19     jmp .L3
     20 .L2:
     21     movl    8(%ebp), %eax
     22     subl    $1, %eax
     23     subl    $12, %esp
     24     pushl   %eax
     25     call    factorial
     26     addl    $16, %esp
     27     imull   8(%ebp), %eax
     28 .L3:
     29     leave
     30     .cfi_restore 5
     31     .cfi_def_cfa 4, 4
     32     ret
     33     .cfi_endproc
     34 .LFE0:
     35     .size   factorial, .-factorial
     36     .section    .rodata
     37 .LC0:
     38     .string "factorial(5): %d
    "
     39     .text
     40     .globl  main
     41     .type   main, @function
     42 main:
     43 .LFB1:
     44     .cfi_startproc
     45     leal    4(%esp), %ecx
     46     .cfi_def_cfa 1, 0
     47     andl    $-16, %esp
     48     pushl   -4(%ecx)
     49     pushl   %ebp
     50     .cfi_escape 0x10,0x5,0x2,0x75,0
     51     movl    %esp, %ebp
     52     pushl   %ebx
     53     pushl   %ecx
     54     .cfi_escape 0xf,0x3,0x75,0x78,0x6
     55     .cfi_escape 0x10,0x3,0x2,0x75,0x7c
     56     call    __x86.get_pc_thunk.bx
     57     addl    $_GLOBAL_OFFSET_TABLE_, %ebx
     58     subl    $12, %esp
     59     pushl   $5
     60     call    factorial
     61     addl    $16, %esp
     62     subl    $8, %esp
     63     pushl   %eax
     64     leal    .LC0@GOTOFF(%ebx), %eax
     65     pushl   %eax
     66     call    printf@PLT
     67     addl    $16, %esp
     68     movl    $0, %eax
     69     leal    -8(%ebp), %esp
     70     popl    %ecx
     71     .cfi_restore 1
     72     .cfi_def_cfa 1, 0
     73     popl    %ebx
     74     .cfi_restore 3
     75     popl    %ebp
     76     .cfi_restore 5
     77     leal    -4(%ecx), %esp
     78     .cfi_def_cfa 4, 4
     79     ret
     80     .cfi_endproc
     81 .LFE1:
     82     .size   main, .-main
     83     .section    .text.__x86.get_pc_thunk.ax,"axG",@progbits,__x86.get_pc_thunk.ax,comdat
     84     .globl  __x86.get_pc_thunk.ax
     85     .hidden __x86.get_pc_thunk.ax
     86     .type   __x86.get_pc_thunk.ax, @function
     87 __x86.get_pc_thunk.ax:
     88 .LFB2:
     89     .cfi_startproc
     90     movl    (%esp), %eax
     91     ret
     92     .cfi_endproc
     93 .LFE2:
     94     .section    .text.__x86.get_pc_thunk.bx,"axG",@progbits,__x86.get_pc_thunk.bx,comdat
     95     .globl  __x86.get_pc_thunk.bx
     96     .hidden __x86.get_pc_thunk.bx
     97     .type   __x86.get_pc_thunk.bx, @function
     98 __x86.get_pc_thunk.bx:
     99 .LFB3:
    100     .cfi_startproc
    101     movl    (%esp), %ebx
    102     ret
    103     .cfi_endproc
    104 .LFE3:
    105     .ident  "GCC: (GNU) 9.1.0"
    106     .section    .note.GNU-stack,"",@progbits   

      linux下命令的选项比命令更重要

           

  • 相关阅读:
    java的final变量理解
    springMVC如何判断入参是默认参数还是请求传过来的参数?
    java反射新的应用
    冒烟测试和回归测试的区别
    git变基、冲突解决
    mockServer学习
    mongodb查询之模糊查询
    springxml配置构造函数入参
    mockito学习
    solr官方文档翻译系列之schema.xml配置介绍
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/11223253.html
Copyright © 2011-2022 走看看