【问题标题】:how to make .O from .ASM on mac如何在mac上从.ASM制作.O
【发布时间】:2014-03-20 19:31:13
【问题描述】:

我有以下文件 hello.asm:

section .text
global _start   ;must be declared for linker (ld)
_start:             ;tells linker entry point
mov edx,len     ;message length
mov ecx,msg     ;message to write
mov ebx,1       ;file descriptor (stdout)
mov eax,4       ;system call number (sys_write)
int 0x80        ;call kernel

mov eax,1       ;system call number (sys_exit)
int 0x80        ;call kernel

 section    .data
 msg db 'Hello, world!', 0xa  ;our dear string
 len equ $ - msg              ;length of our dear string

在 mac 上,我如何将其转换为 .o 文件。在 linux 上我会这样做

 nasm -f elf64 -o hello.o hello.asm
 ld -o hello hello.o

那么它可以被调用

 ./hello

我已经安装了 Xcode 和 MacPorts,谢谢

【问题讨论】:

  • 你必须安装 nasm; port install nasm。之后的过程应该是相似的,尽管 OS X 使用 Mach-O 二进制格式而不是 ELF,所以你需要查找要给 -f 的参数。 (尽管它可能根本不需要那个开关。)
  • @echristopherson 我已经做了 $ sudo port install nasm

标签: macos assembly nasm


【解决方案1】:

使用:

nasm -o hello.o hello.asm

应该可以工作,即生成一个 .o 文件。通常在 OS X 上,您会执行以下操作:

nasm -f macho -o hello.o hello.asm

【讨论】:

  • 没有机会。 nasm -f macho32 hello.asm(或者可能是 macho64)我认为您可能需要 0x2000004 来为 sys_read(?)有一个很好的手册,但对于 Mac 的信息不多。这里的示例:forum.nasm.us/index.php?topic=912.0(不确定它是否真的有效)没有多少人在做这 - 需要反馈!
  • 其实我觉得用macho就行了,不需要最后32或64,所以是nasm -f macho -o hello.o hello.asm。不过他什么也没说,只是想要一个.o 文件——我认为即使编译它也不会在 OSX 上运行。
  • -f macho == -f macho32(就像-f elf == -f elf32)为了清楚起见,我建议使用“长格式”。
  • 这就是我得到的结果 > nasm -f macho32 -o hello.o hello.asm ... nasm: fatal: unrecognised output format macho32' - 使用 -hf 作为列表类型 nasm -h' for help
  • -f 的有效输出格式为(* 表示默认):* bin, aout, aoutb, coff, elf, as86 ,obj, win32, rdf, ieee, macho
猜你喜欢
  • 1970-01-01
  • 2011-10-06
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2018-04-11
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多