【问题标题】:Use NASM to compile 32-bit assembly for x86_64 environment使用 NASM 为 x86_64 环境编译 32 位程序集
【发布时间】:2016-08-06 09:22:33
【问题描述】:

我正在阅读一本书,其中所有汇编代码示例都是为 32 位 Linux 环境编写的,并且我使用的是 64 位 Mac。在将_start 更改为start 后,我能够使用 NASM 编译以下程序。但是,当我运行可执行文件时,它不会像我期望的那样打印hello world。是否可以选择传递给 NASM 以在 64 位 Mac 上运行?

我试过了:

nasm -f macho32 helloworld.asm

nasm -f macho helloworld.asm

接着是:

ld helloworld.o -o helloworld

我的代码是:

section .data       ; data segment
msg     db      "Hello, world!", 0x0a   ; the string and newline char

section .text       ; text segment
global start       ; Default entry point for ELF linking

start:
  ; SYSCALL: write(1, msg, 14) 
  mov eax, 4        ; put 4 into eax, since write is syscall #4
  mov ebx, 1        ; put 1 into ebx, since stdout is 1
  mov ecx, msg      ; put the address of the string into ecx
  mov edx, 14       ; put 14 into edx, since our string is 14 bytes
  int 0x80          ; Call the kernel to make the system call happen

  ; SYSCALL: exit(0)
  mov eax, 1        ; put 1 into eax, since exit is syscall #1
  mov ebx, 0        ; exit with success
  int 0x80          ; do the syscall

【问题讨论】:

  • int 0x80 在 OS/X 和 Linux 上的工作方式不同(系统调用不同)。不建议使用 Linux 代码作为起点。你真的需要 32 位 Mac OS/X 的汇编语言教程一个合理的起点是:filippo.io/making-system-calls-from-assembly-in-mac-os-x
  • 32 位 OS/X 代码将在 64 位 OS/X 中运行。如果您想创建真正的 64 位 OS/X 代码,那么您需要删除 int 0x80 并转而使用 syscall 指令。这与int 0x80 不同。调用约定不同,系统调用号与 32 位 OS/X 代码不匹配。您需要找到针对 64 位 OS/X 代码的教程。您还必须在 NASM 中使用 -f macho64

标签: macos assembly x86 nasm


【解决方案1】:

它根本不会那样工作。获取 VM 并在 VM 中安装 32 位 linux。问题是没有在 x64 上运行 x86_32 代码。问题是在 MAC 上尝试 Linux 系统调用门。没有理由相信这会奏效。

【讨论】:

    猜你喜欢
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    相关资源
    最近更新 更多