简单的asm程序与MacOS Mountain Lion中的yasm

sse*_*ano 3 macos assembly gcc yasm

我想编译并执行一个非常简单的64位程序.

section .text 
global _start 
_start: 
   mov     rdx,len 
   mov     rcx,msg 
   mov     rbx,1 
   mov     rax,4 
   int     0x80 
   mov    rbx,0 
   mov     rax,1 
   int     0x80 
section .data 
msg     db      "Hello, world!",0xa 
len     equ     $ - msg 
Run Code Online (Sandbox Code Playgroud)

编译它的行:

yasm -f elf64 -g dwarf2 example.asm

$ yasm --version
yasm 1.2.0
Run Code Online (Sandbox Code Playgroud)

也尝试了另一种格式macho[|32|64], elf[|32] bin,他们都没有成功.

链接它的行:

gcc -o example example.o

ld: warning: ignoring file example.o, file was built for unsupported file format ( 0x7f 0x45 0x4c 0x46 0x 2 0x 1 0x 1 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 ) which is not the architecture being linked (x86_64): example.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
  start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

$ gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
Run Code Online (Sandbox Code Playgroud)

也尝试了一些选项,gcc-m64 -arch x86_64.

小智 8

我一直在使用OS X的汇编语言yasm.装配线是:

yasm -f macho64 -l file.lst file.asm
Run Code Online (Sandbox Code Playgroud)

然后链接ld:

ld -o file file.o
Run Code Online (Sandbox Code Playgroud)

你应该使用start而不是_startOS X.

之后使用gdb会出现问题.OS X似乎没有任何可用的调试格式.

您可能有兴趣尝试我的名为ebe的IDE.我设法让断点和调试工作得相当好.目前我在调试浮点代码时遇到了一些问题.gdb似乎并不知道ymm寄存器,并且gdb的默认版本后面有xmm寄存器的部分.

你可能会喜欢ebe.使用从sourceforge下载它

git clone git://git.code.sf.net/p/ebe-ide/code ebe
Run Code Online (Sandbox Code Playgroud)

它需要xterm,python,tkinter和pmw(一个python库).


Var*_*der 6

你在这里有几个问题:

1) OS X 不支持 ELF,只支持 Mach-O。

2) 您的链接器正在寻找 x86_64 架构 Mach-O,但没有找到。

ld: warning: ignoring file example.o, file was built for unsupported file format ( 0x7f 0x45 0x4c 0x46 0x 2 0x 1 0x 1 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 ) which is not the architecture being linked (x86_64): example.o

确保您正在为 x86_64 组装,否则尝试传递-m32给 GCC 以使用 x86。

3) 您的链接器试图包含 C 运行时库 crt1.10.6.o,它看起来不像您想要的,因为您没有定义_main函数。也许您应该直接调用链接器而不是通过 GCC 调用它,或者找到正确的标志组合以传递给链接器以避免这种情况。

如果我是你,我会从以下步骤开始:

a) 用 C 编写 hello world,让 clang 输出一个汇编文件 (-s),然后看看是否可以仅使用 clang 来汇编和链接它。这应该很简单。

b) 一旦成功,使用fileotool确定您的目标文件应该是什么样子,并尝试使用 yasm 生成它。