LoginSignup
38
32

More than 5 years have passed since last update.

Xcodeでx86アセンブリを書く

Last updated at Posted at 2015-08-07

Xcode 6です。

ファイル作成

新規プロジェクト作成でCommand Line Toolを選びます。

Screenshot_8_7_15__11_35_PM.png

言語はCを選ぶ。

Screenshot_8_7_15__11_37_PM.png

main.cは消しちゃいます。

Project_navigator_contextual_menu_and_main_c_and_NSDictionary_URLEncoding_swift.png

新規ファイル作成から、

Project_navigator_contextual_menu_and_ams-test_xcodeproj_and_NSDictionary_URLEncoding_swift.png

Assembly Fileを選びます。

ams-test_xcodeproj.png

こういうコードを書きます。

.global _main

.text
_main:
    movl    $1, %eax
    subl    $12, %esp
    int     $0x80

たぶんgasのAT&Tシンタックスというやつだと思います。(自信ない)

Intelシンタックス

Intelシンタックスを使うには .intel_syntax noprefix をつけます。

.intel_syntax noprefix

.global _main

.text
_main:
    mov     eax, 1
    sub     esp, 12
    int     0x80

ビルド

このままだと64bitでコンパイルされると思うので、32bitでコンパイルされるようにします。

ams-test_xcodeproj.png

ビルドしてできるファイルを file コマンドで見てみると、x86_64からi386に変わっているのがわかります。

% file ams-test: Mach-O 64-bit executable x86_64
% file ams-test: Mach-O executable i386

デバッグ

ブレークポイントも置けます。

main_s_—_Edited.png

デバッガーのパネルではAll Variables, Registers, Globals and Statisticsを選択しておくとレジスタの値が見られます。

OtherViews_and_main_s_—_Edited.png

それから、Debug→Debug Workflow→Always Show Disassemblyにチェックを入れておくと、

Debug_Workflow_and_Debug.png

ブレークポイントで止まるときにこういうのが出てきます。

project_xcworkspace.png

ここはソースコードをintelシンタックスで書いたとしてもAT&Tシンタックスで表示されます。

また、そのすぐ下のView Memoryを選ぶとこういう画面が出てきます。

Addressというところに例えばespレジスタ周辺の値を入れると、スタックのメモリレイアウトが見られます。

project_xcworkspace.png

38
32
1

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
38
32