利用下假期,打算把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下命令的选项比命令更重要

64位linux下玩32位汇编编程的更多相关文章

  1. 在64位linux下编译32位程序

    在64位linux下编译32位程序 http://blog.csdn.net/xsckernel/article/details/38045783

  2. 64位系统下注册32位dll文件

    64位系统下注册32位dll文件 在64位系统里注册32位软件所需的一些dll会提示不兼容,大概因为32 位进程不能加载64位Dll,64位进程也不可以加载32的导致. 若要支持的32 位和64 位C ...

  3. 64位系统下注册32位dll、ax文件

    64位系统下注册32位dll.ax文件. 换了64位系统遇到的新问题,目前常用的影音处理软件多数为32位. 注册这些32的滤镜会提示不兼容,大概因为32 位进程不能加载64位Dll,64位进程也不可以 ...

  4. <摘录>如何在64位linux强制编译32位应用程序

    GDC注:因为需要解决在linux64机上编译32位的mongodb(没办法,因为编译的php是32位,然后我想将mongdb扩展添加到php中),在网上搜了很多文章,感觉这篇好懂,而且好用.我使用的 ...

  5. 64位操作系统下调用32位com的问题

    Hello Guys! I am trying to create a simple VBS script to automatically open some .tif images from a ...

  6. WinDbg 在64位系统下转储32位进程

    在64位系统下,首先要判断进程是32位,还是64位 在Win8之前,进程名后带星号(*)则是32位进程.但Win8.1后,则不显示星号.需要选出“平台”列,来确认32位,还是64位. 在64位系统下的 ...

  7. pyinstaller在64位系统下打包32位程序

    使用环境说明:win10 64位,已安装python3.6-64位版本 遇到的问题:win10 64位打包成exe文件后,不能在32位系统运行 需求:使用python打包生成exe文件,win64位和 ...

  8. 在64位linux上编译32位程序 for i386 intel

    编辑中 # ld -V GNU ld version 2.15.92.0.2 20040927 Supported emulations: elf_x86_64 elf_i386 i386linux ...

  9. 在64位Linux上安装32位gmp大数库

    前期准备: 如果没有安装32位gcc和g++环境的话,可能会导致安装失败,此时请参考上一篇博文 http://www.cnblogs.com/weir007/p/5977759.html,根据系统版本 ...

随机推荐

  1. SQL Server 输出 XML

    一.概述 SELECT 查询将结果作为行集返回.在 SQL 查询中指定 FOR XML 子句,从而将该查询的正式结果作为 XML 来检索.FOR XML 子句可以用在顶级查询和子查询中.顶级 FOR ...

  2. VBA常量(八)

    常量是一个命名的内存位置,用于保存脚本执行期间固定(无法更改)的值.如果用户试图更改常量值,则脚本执行结束时会出现错误.常量声明与声明变量相同. 以下是命名常量的规则 - 常量名称必须使用一个字母作为 ...

  3. Hibernate-validate工具类,手动调用校验返回结果

    引言:在常见的工程中,一般是在Controller中校验入参,校验入参的方式有多种,这里介绍的使用hibernate-validate来验证,其中分为手动和自动校验,自动校验可以联合spring,使用 ...

  4. 判断对象是否为null

    Person p=teacher as person; If (p!=null ) dostring (); 使用这样的方法效率高 使用 is时 进行两次判断效率低

  5. jQuery属性遍历、HTML操作

    jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery 遍历函数 jQuery 遍历函数包括了用于筛选.查找和串联元素的方法.    .add() 将元素添加到匹配元素的集合中. . ...

  6. 多线程 multiprocessing 的几个小例子

    1.Pipe import multiprocessing as multip,time from multiprocessing import Process,Pipe,Event,Conditio ...

  7. css实现背景全透明样式

    background-color:transparent; filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#26FFF ...

  8. pipenv安装报错ReadTimeoutError

    原因:pip下载源默认设置为国外 解决:将pip下载源切换为国内地址 操作: 在C:\Users\Administrator\下新建pip文件夹,在创建pip.ini文件,拷贝下面代码进去,保存 切换 ...

  9. 【转】关于 Goroutine Channel Select 的用法和理解

    原文:https://blog.csdn.net/jfkidear/article/details/88661693 ----------------------------------------- ...

  10. .npmrc 实用小技巧

    小技巧 因为每次执行 npm adduser 的时候都需要输入用户名.密码和email 很麻烦,我们都可以配置在.npmrc 文件中,在命令行中执行如下脚本 echo -n 'myuser:mypas ...