X86 Build on Mac

Does anyone know of any good tools (I'm looking for an IDE) for writing builds on a Mac. Xcode is a little cumbersome for me.

Also, on an Intel Mac, is it possible to use a generic x86 asm? Or is there a modified instruction set? Any information on the Intel post.

Also: I know that on windows, asm can run in an emulated environment created by the OS to let the code think that it works on its own specialized machine. Does OS X Provide the Same?

+46
assembly x86 xcode macos
Aug 08 '08 at 4:25
source share
8 answers

After installing any version of Intel-based Xcode, you should be able to write assembly code. Xcode is a toolkit, only one of which is an IDE, so you don’t need to use it if you don’t want it. (However, if there are certain things that you find awkward, write an error to Apple's error reporter - every error goes into engineering.) In addition, the Xcode installation will install both Netwide Assembler (NASM) and GNU Assembler (GAS) ; which will allow you to use any assembly syntax with which you are most comfortable.

You will also want to take a look at the Compiler and Debugging Guides , as these documents are conventions that are used for various architectures that run Mac OS X, and how the binary format and bootloader work. IA-32 (x86-32) conventions, in particular, may differ slightly from what you are used to.

Another thing to keep in mind is that the system call interface on Mac OS X is different from what you could use for DOS / Windows, Linux, or other BSD accessories. System calls are not considered a stable API on Mac OS X; instead, you always go through libSystem. This ensures that you write code that is portable from one OS release to another.

Lastly, keep in mind that Mac OS X goes through a fairly wide range of hardware - everything from the 32-bit Core Single to the high-end quad-core Xeon processor. When coding in an assembly, you may not optimize as much as you think; what is optimal on one machine may be pessimal on another. Apple regularly measures its compilers and adjusts its products with the “-O” optimization flag to be worthy throughout its line, and there are extensive vector / matrix processing libraries that you can use to get high performance with processor-specific settings, with manual setting.

The assembly for fun is excellent. Switching to assembly for speed is not for the faint of heart these days.

+61
Aug 08 '08 at 7:07
source share

As stated above, do not use syscall. However, you can use standard C library calls, but keep in mind that the stack MUST be 16-byte aligned on Apple 's IA32 function call in IA32 function .

If you do not align the stack, your program will __dyld_misaligned_stack_error in __dyld_misaligned_stack_error when making a call to any of the libraries or frameworks.

The following snippet builds 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 
+25
Dec 10 '08 at 18:46
source share

For a nice step-by-step introduction to the x86 Mac, see http://peter.michaux.ca/articles/assembly-hello-world-for-os-x . Other links I've tried have some non-Mac pitfalls.

+8
Jul 15 '11 at 3:12
source share

I recently wanted to learn how to compile Intel x86 on Mac OS X:

For nasm:

 -o hello.tmp - outfile -f macho - specify format Linux - elf or elf64 Mac OSX - macho 

For 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 

Single line:

 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 this on my blog:

http://blog.burrowsapps.com/2013/07/how-to-compile-helloworld-in-intel-x86.html

For a more detailed explanation, I explained my Github here:

https://github.com/jaredsburrows/Assembly

+7
Jul 23 '13 at 3:12
source share

Also, on an Intel Mac, is it possible to use a generic x86 asm? or is there a modified instruction set? Any info on building Intel Mac posts helps.

This is the same set of commands; these are the same chips.

+3
Aug 08 '08 at 4:33
source share

Available features depend on your processor. Apple uses the same Intel stuff as everyone else. So yes, generic x86 should be good (unless you use PPC: D).

As for the tools, I think your best bet is a good text editor that “understands” the assembly.

+1
Aug 08 '08 at 4:35
source share

Forget about IDE detection for writing / running / compiling assembler on Mac. But, remember that mac is UNIX. See http://asm.sourceforge.net/articles/linasm.html . A decent guide (albeit a short one) to run assembler via GCC on Linux. You can imitate this. Mac uses Intel processors, so you want to look at Intel syntax.

0
Aug 08 '08 at 5:18
source share

Do not forget that, unlike Windows, all Unix-based systems must have a source before destination, unlike Windows

On Windows it:

mov $ source,% destination

but on a Mac it's the other way around.

0
Aug 28 '11 at 4:29
source share



All Articles