【问题标题】:run 32 bit assembly on 64 bit processor with mac os x使用 mac os x 在 64 位处理器上运行 32 位程序集
【发布时间】:2016-01-12 00:21:13
【问题描述】:

我在运行 os x 10.9.5 的 64 位 Mac 上运行 32 位程序集时遇到问题。我也安装了 NASM 2.11.08。我目前正在阅读 Jeff Duntemann 的《Assembly Language Step by Step》。在书中,他详细说明了 linux 操作系统上的 32 位汇编指令。如何在我的 64 位 mac os x 计算机上运行此程序。

; eatsyscall.asm

SECTION .data           ; Section containing initialised data
EatMsg: db "Eat at Joes!",10
EatLen: equ $-EatMsg    

SECTION .bss            ; Section containing uninitialized data 

SECTION .text           ; Section containing code

global  _start          ; Linker needs this to find the entry point!

_start:
    nop         ; This no-op keeps gdb happy...
    mov eax,4       ; Specify sys_write call
    mov ebx,1       ; Specify File Descriptor 1: Standard Output
    mov ecx,EatMsg      ; Pass offset of the message
    mov edx,EatLen      ; Pass the length of the message
    int 80H         ; Make kernel call

    MOV eax,1       ; Code for Exit Syscall
    mov ebx,0       ; Return a code of zero 
    int 80H         ; Make kernel call

我试过用它组装它

nasm -f elf -g -F stabs eatsyscall.asm

然后我尝试将其链接到

ld -o eatsyscall eatsyscall.o

但我收到此错误

ld: warning: -arch not specified
ld: warning: -macosx_version_min not specified, assuming 10.6
ld: warning: ignoring file eatsyscall.o, file was built for unsupported file format ( 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): eatsyscall.o
Undefined symbols for architecture x86_64:
  "start", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64

这应该运行,对吧?我认为英特尔的 64 位处理器能够运行 32 位程序。还是没有办法在 64 位 mac 上运行为 32 位 linux 系统编写的汇编程序?

我是否需要安装一些 32 位库才能链接此文件?我应该使用 NASM 以外的东西,例如 GCC 吗?还是程序本身没有正确编写。感谢您的帮助!

【问题讨论】:

    标签: macos assembly 64-bit x86-64 32-bit


    【解决方案1】:

    这里有两个问题。

    1. 您正在将汇编文件编译为 ELF 二进制文件 (-f elf),Mac OS X ld 不支持该文件。使用 -f macho 为您的系统生成 Mach-O 目标文件,然后使用 -arch i386 将其链接为 32 位二进制文​​件。

    2. 您正在尝试在 Mac OS X 上使用 Linux 系统调用。这不起作用;系统调用号和调用约定是不同的,并且没有公开记录。解决这个问题是可能的,但是,正如弗兰克科特勒所提到的,这不是我推荐给初学者的任务。最好的办法是使用 32 位 Linux 系统来完成这些教程。

    【讨论】:

      【解决方案2】:

      Linux 的可执行文件无法在 Mac 上运行。如果您想运行 Jeff Duntemann 的东西,请在您的 Mac 上的虚拟机上安装 Linux。代码可以很容易地翻译成-f macho64(?),但在-f macho64 上的Nasm-2.11.08 中有一个坏错误:(

      有一个候选版本 - http://www.nasm.us/pub/nasm/releasebuilds/2.11.09rc1/macosx/ - “可能”修复它。有人需要测试它。对于初学者来说,这可能不是一份好工作。您应该能够在 Mac 上使用 gcc 进行编程,但不能使用“逐步”。 Nasm 可以在你的 Mac 上运行......但现在不行......如果可以的话,现在安装 Linux。

      【讨论】:

        猜你喜欢
        • 2013-02-23
        • 2012-10-03
        • 1970-01-01
        • 2010-12-31
        • 2012-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-01
        相关资源
        最近更新 更多