Shinobi Style

Footprint To Marvelous Hacker

アセンブラの関数をC言語から実行する

アセンブラの関数をC言語から実行する。

Procedure

  1. 実行させたい関数を用意する(test.S) ".intel_syntax"ディレクティブを利用することでintel記法を使用できるようになる。
.intel_syntax noprefix
.global asm3

asm3:
    push   ebp
    mov    ebp,esp
    xor    eax,eax
    mov    ah,BYTE PTR [ebp+0x8]
    shl    ax,0x10
    sub    al,BYTE PTR [ebp+0xe]
    add    ah,BYTE PTR [ebp+0xc]
    xor    ax,WORD PTR [ebp+0x10]
    nop
    pop    ebp
    ret
  1. 関数を呼ぶメインを準備(main.c)
#include <stdio.h>

int asm3(int, int, int);
int main(int argc, char* argv[])
{
    printf("0x%x\n", asm3(0xcdc485c1,0xd6bd5e88,0xe4c1548d));
    return 0;
}
  1. コンパイルする

"-m32"で32bitを指定

$ gcc -m32 -c main.c -o main.o

"-masm=intel"でintel記法を指定

$ gcc -masm=intel -m32 -c test.S -o test.o

実行ファイルをコンパイル、実行

$gcc -m32 test.o main.o -o main
$ ./main
0xdcce

Closing

実際にコンパイルするのが面倒だったら emulator を使うのが良いのかも。