xcode Mac 上的 x86 程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
x86 Assembly on a Mac
提问by matthewdunnam
Does anyone know of any good tools (I'm looking for IDEs) to write assembly on the Mac. Xcode is a little cumbersome to me.
有谁知道在 Mac 上编写程序集的任何好工具(我正在寻找 IDE)。Xcode 对我来说有点麻烦。
Also, on the Intel Macs, can I use generic x86 asm? Or is there a modified instruction set? Any information about post Intel.
另外,在 Intel Mac 上,我可以使用通用的 x86 asm 吗?或者是否有修改后的指令集?有关发布英特尔的任何信息。
Also: I know that on windows, asm can run in an emulated environment created by the OS to let the code think it's running on its own dedicated machine. Does OS X provide the same thing?
另外:我知道在 Windows 上,asm 可以在操作系统创建的模拟环境中运行,让代码认为它是在自己的专用机器上运行的。OS X 是否提供同样的东西?
回答by Chris Hanson
After installing any version of Xcode targeting Intel-based Macs, you should be able to write assembly code. Xcode is a suite of tools, only one of which is the IDE, so you don't have to use it if you don't want to. (That said, if there are specific things you find clunky, please file a bug at Apple's bug reporter- every bug goes to engineering.) Furthermore, installing Xcode will install both the Netwide Assembler (NASM) and the GNU Assembler (GAS); that will let you use whatever assembly syntax you're most comfortable with.
在安装了针对基于 Intel 的 Mac 的任何版本的 Xcode 后,您应该能够编写汇编代码。Xcode 是一套工具,其中只有一个是 IDE,所以如果您不想使用它,则不必使用它。(也就是说,如果您发现某些特定的东西很笨拙,请向Apple 的错误报告者提交错误- 每个错误都归工程部门。)此外,安装 Xcode 将同时安装 Netwide Assembler (NASM) 和 GNU Assembler (GAS);这将让您使用您最熟悉的任何程序集语法。
You'll also want to take a look at the Compiler & Debugging Guides, because those document the calling conventions used for the various architectures that Mac OS X runs on, as well as how the binary format and the loader work. The IA-32 (x86-32) calling conventions in particular may be slightly different from what you're used to.
您还需要查看Compiler & Debugging Guides,因为这些指南记录了用于运行 Mac OS X 的各种体系结构的调用约定,以及二进制格式和加载程序的工作方式。特别是 IA-32 (x86-32) 调用约定可能与您习惯的略有不同。
Another thing to keep in mind is that the system call interface on Mac OS X is different from what you might be used to on DOS/Windows, Linux, or the other BSD flavors. System calls aren't considered a stable API on Mac OS X; instead, you always go through libSystem. That will ensure you're writing code that's portable from one release of the OS to the next.
要记住的另一件事是 Mac OS X 上的系统调用接口与您可能在 DOS/Windows、Linux 或其他 BSD 风格上使用的系统调用接口不同。系统调用在 Mac OS X 上不被视为稳定的 API;相反,你总是通过 libSystem。这将确保您编写的代码可从操作系统的一个版本移植到下一个版本。
Finally, keep in mind that Mac OS X runs across a pretty wide array of hardware - everything from the 32-bit Core Single through the high-end quad-core Xeon. By coding in assembly you might not be optimizing as much as you think; what's optimal on one machine may be pessimal on another. Apple regularly measures its compilers and tunes their output with the "-Os" optimization flag to be decent across its line, and there are extensive vector/matrix-processing libraries that you can use to get high performance with hand-tuned CPU-specific implementations.
最后,请记住,Mac OS X 运行在相当广泛的硬件上——从 32 位 Core Single 到高端四核 Xeon。通过在汇编中编码,您可能不会像您想象的那样优化;在一台机器上最佳的在另一台机器上可能是悲观的。Apple 定期测量其编译器并使用“-Os”优化标志调整其输出以使其在整个生产线中都不错,并且您可以使用广泛的向量/矩阵处理库通过手动调整的特定于 CPU 的实现来获得高性能.
Going to assembly for fun is great. Going to assembly for speed is not for the faint of heart these days.
去集会玩是很棒的。如今,为了速度而去组装不适合胆小的人。
回答by David DeHaven
As stated before, don't use syscall. You can use standard C library calls though, but be aware that the stack MUST be 16 byte aligned per Apple's IA32 function call ABI.
如前所述,不要使用系统调用。不过,您可以使用标准 C 库调用,但请注意,堆栈必须按 Apple 的IA32 函数调用 ABI进行 16 字节对齐。
If you don't align the stack, your program will crash in __dyld_misaligned_stack_error
when you make a call into any of the libraries or frameworks.
如果您不对齐堆栈,则__dyld_misaligned_stack_error
当您调用任何库或框架时,您的程序将崩溃。
The following snippet assembles and runs on my system:
以下代码段在我的系统上组装并运行:
; File: hello.asm
; Build: nasm -f macho hello.asm && gcc -o hello hello.o
SECTION .rodata
hello.msg db 'Hello, World!',0x0a,0x00
SECTION .text
extern _printf ; could also use _puts...
GLOBAL _main
; aligns esp to 16 bytes in preparation for calling a C library function
; arg is number of bytes to pad for function arguments, this should be a multiple of 16
; unless you are using push/pop to load args
%macro clib_prolog 1
mov ebx, esp ; remember current esp
and esp, 0xFFFFFFF0 ; align to next 16 byte boundary (could be zero offset!)
sub esp, 12 ; skip ahead 12 so we can store original esp
push ebx ; store esp (16 bytes aligned again)
sub esp, %1 ; pad for arguments (make conditional?)
%endmacro
; arg must match most recent call to clib_prolog
%macro clib_epilog 1
add esp, %1 ; remove arg padding
pop ebx ; get original esp
mov esp, ebx ; restore
%endmacro
_main:
; set up stack frame
push ebp
mov ebp, esp
push ebx
clib_prolog 16
mov dword [esp], hello.msg
call _printf
; can make more clib calls here...
clib_epilog 16
; tear down stack frame
pop ebx
mov esp, ebp
pop ebp
mov eax, 0 ; set return code
ret
回答by Jared Burrows
Recently I wanted to learn how to compile Intel x86 on Mac OS X:
最近想学习如何在 Mac OS X 上编译 Intel x86:
For nasm:
对于 nasm:
-o hello.tmp - outfile
-f macho - specify format
Linux - elf or elf64
Mac OSX - macho
For ld:
对于 ld:
-arch i386 - specify architecture (32 bit assembly)
-macosx_version_min 10.6 (Mac OSX - complains about default specification)
-no_pie (Mac OSX - removes ld warning)
-e main - specify main symbol name (Mac OSX - default is start)
-o hello.o - outfile
For Shell:
对于壳牌:
./hello.o - execution
One-liner:
单线:
nasm -o hello.tmp -f macho hello.s && ld -arch i386 -macosx_version_min 10.6 -no_pie -e _main -o hello.o hello.tmp && ./hello.o
Let me know if this helps!
让我知道这是否有帮助!
I wrote how to do it on my blog here:
我在我的博客上写了如何做到这一点:
http://blog.burrowsapps.com/2013/07/how-to-compile-helloworld-in-intel-x86.html
http://blog.burrowsapps.com/2013/07/how-to-compile-helloworld-in-intel-x86.html
For a more verbose explanation, I explained on my Github here:
对于更详细的解释,我在我的 Github 上进行了解释:
回答by Flash Sheridan
For a nice step-by-step x86 Mac-specific introduction see http://peter.michaux.ca/articles/assembly-hello-world-for-os-x. The other links I've tried have some non-Mac pitfalls.
有关 x86 Mac 特定的分步介绍,请参阅http://peter.michaux.ca/articles/assembly-hello-world-for-os-x。我尝试过的其他链接有一些非 Mac 陷阱。
回答by Josh
Also, on the Intel Macs, can I use generic x86 asm? or is there a modified instruction set? Any information about post Intel Mac assembly helps.
另外,在 Intel Mac 上,我可以使用通用的 x86 asm 吗?或者是否有修改后的指令集?任何有关后英特尔 Mac 组装的信息都有帮助。
It's the same instruction set; it's the same chips.
这是相同的指令集;这是相同的芯片。
回答by Akhzar Nazir
Running assembly Code on Mac is just 3 steps away from you. It could be done using XCODE but better is to use NASM Command Line Tool. For My Ease I have already installed Xcode, if you have Xcode installed its good.
在 Mac 上运行汇编代码离您仅 3 步之遥。可以使用 XCODE 完成,但更好的是使用 NASM 命令行工具。为方便起见,我已经安装了 Xcode,如果您安装了 Xcode,则它很好。
But You can do it without XCode as well.
但是你也可以在没有 XCode 的情况下做到这一点。
Just Follow:
只需关注:
- First Install NASM using Homebrew
brew install nasm
- convert .asm file into Obj File using this command
nasm -f macho64 myFile.asm
- Run Obj File to see OutPut using command
ld -macosx_version_min 10.7.0 -lSystem -o OutPutFile myFile.o && ./64
- 首先使用 Homebrew 安装 NASM
brew install nasm
- 使用此命令将 .asm 文件转换为 Obj 文件
nasm -f macho64 myFile.asm
- 运行 Obj File 以使用命令查看 OutPut
ld -macosx_version_min 10.7.0 -lSystem -o OutPutFile myFile.o && ./64
Simple Text File named myFile.asm is written below for your convenience.
为方便起见,下面编写了名为 myFile.asm 的简单文本文件。
global start
section .text
start:
mov rax, 0x2000004 ; write
mov rdi, 1 ; stdout
mov rsi, msg
mov rdx, msg.len
syscall
mov rax, 0x2000001 ; exit
mov rdi, 0
syscall
section .data
msg: db "Assalam O Alaikum Dear", 10
.len: equ $ - msg
回答by Bernard
The features available to use are dependent on your processor. Apple uses the same Intel stuff as everybody else. So yes, generic x86 should be fine (assuming you're not on a PPC :D).
可用的功能取决于您的处理器。Apple 使用与其他所有人相同的英特尔产品。所以是的,通用 x86 应该没问题(假设你不是在 PPC 上:D)。
As far as tools go, I think your best bet is a good text editor that 'understands' assembly.
就工具而言,我认为最好的选择是“理解”汇编的优秀文本编辑器。
回答by Stephen Cox
Forget about finding a IDE to write/run/compile assembler on Mac. But, remember mac is UNIX. See http://asm.sourceforge.net/articles/linasm.html. A decent guide (though short) to running assembler via GCC on Linux. You can mimic this. Macs use Intel chips so you want to look at Intel syntax.
忘记在 Mac 上寻找一个 IDE 来编写/运行/编译汇编程序。但是,请记住 mac 是 UNIX。请参阅http://asm.sourceforge.net/articles/linasm.html。在 Linux 上通过 GCC 运行汇编器的不错指南(虽然很短)。你可以模仿这个。Mac 使用 Intel 芯片,因此您需要查看 Intel 语法。
回答by Res.
Don't forget that unlike Windows, all Unix based system need to have the source before destination unlike Windows
不要忘记,与 Windows 不同的是,所有基于 Unix 的系统都需要在目标之前拥有源,这与 Windows 不同
On Windows its:
在 Windows 上:
mov $source , %destination
mov $source , %destination
but on the Mac its the other way around.
但在 Mac 上则相反。