2

私はアセンブリの初心者です(実際、これは私の初めての試みです)。NASMとldリンカーを使用してこのx86アセンブリコードをMacで実行するにはどうすればよいか疑問に思いました。

SECTION .data           ; Section containing initialised data

    EatMsg: db "Eat at Joe's!",10
    EatLen: equ $-EatMsg    

SECTION .bss            ; Section containing uninitialized data 

SECTION .text           ; Section containing code

global  _start          ; Linker needs this to find the entry point!

_start:
    nop         ; This no-op keeps gdb happy...
    mov eax,4       ; Specify sys_write call
    mov ebx,1       ; Specify File Descriptor 1: Standard Output
    mov ecx,EatMsg      ; Pass offset of the message
    mov edx,EatLen      ; Pass the length of the message
    int 80H         ; Make kernel call

    MOV eax,1       ; Code for Exit Syscall
    mov ebx,0       ; Return a code of zero 
    int 80H         ; Make kernel call

このアセンブリコードはLinux用に作成された本からのものですが、LinuxとMacはどちらもUNIXベースのオペレーティングシステムであるため、アセンブリコードは一般的に同じであると想定しました。nasmとldを介してこれをmac実行可能ファイルに取得できない可能性があることに気付きましたが、可能であれば、どうすればよいでしょうか。そうでない場合は、このアセンブリコードをどのように変更して機能するようにしますが、一般的に同じことを行いますか?

4

2 に答える 2

4

次のサンプル コードは、nasm でアセンブルし、gcc を使用してリンクすると動作するはずです (標準の c ライブラリとリンクする必要があります!)。

SECTION .data           ; Section containing initialised data

    EatMsg: db "Eat at Joe's!",10
    EatLen: equ $-EatMsg

SECTION .bss            ; Section containing uninitialized data

SECTION .text           ; Section containing code

global main
extern write

main:
    sub esp, 12                 ; allocate space for locals/arguments
    mov dword [esp], 1          ; stdout
    mov dword [esp+4], EatMsg   ; Pass offset of the message
    mov dword [esp+8], EatLen   ; Pass the length of the message
    call write                  ; Make library call

    mov eax,0                   ; Return a code of zero
    add esp, 12                 ; restore stack
    ret

「Hello world」は最初の asm プログラムとしては特に良くないので、一般的に asm から I/O やその他の高レベルの処理を行うことはお勧めしません。

于 2013-01-08T22:58:46.817 に答える
3

int 80HOS Xでは非推奨であり、いずれの場合もLinuxとは異なるセレクターを使用します。syscall代わりに、標準Cライブラリを使用または呼び出す必要があります。

参照:thexploit.com/secdev/mac-os-x-64-bit-assembly-system-callsで、MacOSXでのnasmを使用した便利な「HelloWorld」チュートリアルを参照してください。

nasmを使用してOSX実行可能ファイルを構築する方法の詳細については、daveeveritt.tumblr.com / post / 67819832/webstraction-2-from-unix-history-and-assembly-languageも参照してください。

于 2013-01-08T22:53:54.423 に答える