x86 Assembly on a Mac

前端 未结 9 1444
温柔的废话
温柔的废话 2020-11-28 01:15

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 gener

相关标签:
9条回答
  • 2020-11-28 01:55

    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.

    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.

    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.

    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.

    Going to assembly for fun is great. Going to assembly for speed is not for the faint of heart these days.

    0 讨论(0)
  • 2020-11-28 01:55

    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.

    0 讨论(0)
  • 2020-11-28 01:58

    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.

    0 讨论(0)
  • 2020-11-28 02:00

    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.

    Just Follow:

    1. First Install NASM using Homebrew brew install nasm
    2. convert .asm file into Obj File using this command nasm -f macho64 myFile.asm
    3. Run Obj File to see OutPut using command 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
    
    0 讨论(0)
  • 2020-11-28 02:03

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题