64位linux下玩32位汇编编程

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

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

 .section .data

 .section .text

 .globl _start
_start:
pushl $
pushl $
call sumer
addl $, %esp
movl %eax, %ebx
movl $, %eax
int $0x80 .type sumer, @function sumer:
pushl %ebp
movl %esp, %ebp
movl (%ebp), %eax
movl (%ebp), %ecx
addl %ecx, %eax
popl %ebp
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

 #include <stdio.h>

 int factorial(int num){
if( == num){
return ;
}
return num * factorial(num - );
} int main(int argc, char **argv)
{
printf("factorial(5): %d\n", factorial()); return ;
}

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

       .file   "test321.c"
.text
.globl factorial
.type factorial, @function
factorial:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset
.cfi_offset , -
movq %rsp, %rbp
.cfi_def_cfa_register
subq $, %rsp
movl %edi, -(%rbp)
cmpl $, -(%rbp)
jne .L2
movl $, %eax
jmp .L3
.L2:
movl -(%rbp), %eax
subl $, %eax
movl %eax, %edi
call factorial
imull -(%rbp), %eax
.L3:
leave
.cfi_def_cfa ,
ret
.cfi_endproc
.LFE0:
.size factorial, .-factorial
.section .rodata
.LC0:
.string "factorial(5): %d\n"
.text
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset
.cfi_offset , -
movq %rsp, %rbp
.cfi_def_cfa_register
subq $, %rsp
movl %edi, -(%rbp)
movq %rsi, -(%rbp)
movl $, %edi
call factorial
movl %eax, %esi
leaq .LC0(%rip), %rdi
movl $, %eax
call printf@PLT
movl $, %eax
leave
.cfi_def_cfa ,
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (GNU) 9.1.0"
.section .note.GNU-stack,"",@progbits
~

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

  .file   "test321.c"
.text
.globl factorial
.type factorial, @function
factorial:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset
.cfi_offset , -
movl %esp, %ebp
.cfi_def_cfa_register
subl $, %esp
call __x86.get_pc_thunk.ax
addl $_GLOBAL_OFFSET_TABLE_, %eax
cmpl $, (%ebp)
jne .L2
movl $, %eax
jmp .L3
.L2:
movl (%ebp), %eax
subl $, %eax
subl $, %esp
pushl %eax
call factorial
addl $, %esp
imull (%ebp), %eax
.L3:
leave
.cfi_restore
.cfi_def_cfa ,
ret
.cfi_endproc
.LFE0:
.size factorial, .-factorial
.section .rodata
.LC0:
.string "factorial(5): %d\n"
.text
.globl main
.type main, @function
main:
.LFB1:
.cfi_startproc
leal (%esp), %ecx
.cfi_def_cfa ,
andl $-, %esp
pushl -(%ecx)
pushl %ebp
.cfi_escape 0x10,0x5,0x2,0x75,
movl %esp, %ebp
pushl %ebx
pushl %ecx
.cfi_escape 0xf,0x3,0x75,0x78,0x6
.cfi_escape 0x10,0x3,0x2,0x75,0x7c
call __x86.get_pc_thunk.bx
addl $_GLOBAL_OFFSET_TABLE_, %ebx
subl $, %esp
pushl $
call factorial
addl $, %esp
subl $, %esp
pushl %eax
leal .LC0@GOTOFF(%ebx), %eax
pushl %eax
call printf@PLT
addl $, %esp
movl $, %eax
leal -(%ebp), %esp
popl %ecx
.cfi_restore
.cfi_def_cfa ,
popl %ebx
.cfi_restore
popl %ebp
.cfi_restore
leal -(%ecx), %esp
.cfi_def_cfa ,
ret
.cfi_endproc
.LFE1:
.size main, .-main
.section .text.__x86.get_pc_thunk.ax,"axG",@progbits,__x86.get_pc_thunk.ax,comdat
.globl __x86.get_pc_thunk.ax
.hidden __x86.get_pc_thunk.ax
.type __x86.get_pc_thunk.ax, @function
__x86.get_pc_thunk.ax:
.LFB2:
.cfi_startproc
movl (%esp), %eax
ret
.cfi_endproc
.LFE2:
.section .text.__x86.get_pc_thunk.bx,"axG",@progbits,__x86.get_pc_thunk.bx,comdat
.globl __x86.get_pc_thunk.bx
.hidden __x86.get_pc_thunk.bx
.type __x86.get_pc_thunk.bx, @function
__x86.get_pc_thunk.bx:
.LFB3:
.cfi_startproc
movl (%esp), %ebx
ret
.cfi_endproc
.LFE3:
.ident "GCC: (GNU) 9.1.0"
.section .note.GNU-stack,"",@progbits

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

上一篇:centos升级支持到C++11, gcc4.8.2


下一篇:linux下 玩转ptrace