您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在64位Linux和64位处理器上运行32位汇编代码:解释异常

在64位Linux和64位处理器上运行32位汇编代码:解释异常

请记住,认情况下,64位OS上的所有内容都倾向于采用64位。您需要确保(a)在适当的地方使用#include的32位版本(b)与32位库的链接以及(c)构建32位可执行文件。如果您显示makefile的内容(如果有的话)或用于构建此示例的命令,则可能会有所帮助。

FWIW我稍微更改了您的代码(_start-> main):

#include <asm/unistd.h>
#include <syscall.h>
#define STDOUT 1

    .data
hellostr:
    .ascii "hello wolrd\n" ;
helloend:

    .text
    .globl main

main:
    movl $(SYS_write) , %eax  //ssize_t write(int fd, const void *buf, size_t count);
    movl $(STDOUT) , %ebx
    movl $hellostr , %ecx
    movl $(helloend-hellostr) , %edx
    int $0x80

    movl $(SYS_exit), %eax //void _exit(int status);
    xorl %ebx, %ebx
    int $0x80

    ret

并像这样构建它:

$ gcc -Wall test.S -m32 -o test

确认我们具有32位可执行文件

$ file test
test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), not stripped

它似乎运行正常:

$ ./test
hello wolrd
其他 2022/1/1 18:17:06 有636人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶