LoginSignup
21
19

More than 5 years have passed since last update.

Mac OSX Mountain Lion で 64bit(x86-64) アセンブリ言語の HelloWorld プログラム

Posted at

XCode 4.4 と Command Line Tools をインストールしている前提。GNU Assemblar で 64bit (x86-64) の Hello World プログラムを書いてみた。Lion でも同じ方法で動く。

hello.s
        # textセクション(書き込み不可領域)を指定
        .text

        # startを外部から参照可能にする (linux では _start)
        .globl start

        # 表示する文字列のデータ
msg:    .ascii      "hello, world.\n"

start:  
        # まず,write システムコールで文字列を表示する。

        # システムコール番号 (write=0x2000004) を %rax レジスタにセット
        # (linux では 0x01)
        movq    $0x2000004, %rax

        # ファイルディスクリプタ (標準出力=1) を %rdi レジスタにセット
        movq    $1, %rdi

        # 文字列のメモリアドレスを %rsi レジスタにセット
        # x86-64 では直接メモリアドレスを代入できないらしいので rip アドレッシングを使って,
        # プログラムの位置からの相対値でメモリを指定する。
        leaq    msg(%rip), %rsi

        # 文字列の長さ(14バイト)を %rdx レジスタにセット
        movq    $14, %rdx

        # write システムコールを呼び出す。
        syscall

        # 次に,exit システムコールでプロセスを終了する。

        # exit のシステムコール番号は 0x2000001 (linux では 0x3c)
        movq    $0x2000001, %rax

        # プロセスの終了状態を指定 (正常終了=0)
        movq    $0, %rdi

        # exit システムコールを呼び出す。
        syscall

以下のコマンドでアセンブル,リンク,実行。

$ as -o hello.o hello.s
$ ld -o hello -macosx_version_min 10.7 hello.o
$ ./hello
hello, world.

参考にしたブログ記事:
http://thexploit.com/secdev/mac-os-x-64-bit-assembly-system-calls/

21
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
19