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.
Also, on the Intel Macs, can I use generic x86 asm? Or is there a modified instruction set? Any information about post Intel.
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?
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.
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.
But You can do it without XCode as well.
brew install nasm
nasm -f macho64 myFile.asm
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.
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
Recently I wanted to learn how to compile Intel x86 on Mac OS X:
-o hello.tmp - outfile -f macho - specify format Linux - elf or elf64 Mac OSX - macho
-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
./hello.o - execution
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:
For a more verbose explanation, I explained on my Github here:
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.
It's the same instruction set; it's the same chips.
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.
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.
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.
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
Don't forget that unlike Windows, all Unix based system need to have the source before destination unlike Windows
On Windows its:
mov $source , %destination
but on the Mac its the other way around.
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).
As far as tools go, I think your best bet is a good text editor that 'understands' assembly.