gnu as tips

OSのbootプロセスを見る日々です。 オペコードオペランドはパスで。

1.マクロ1

/* マクロの定義 */
.macro macro_name
...
.macro

macro_name # マクロの展開

2.マクロ2

#define three(sym) sym ## sym ## sym

gccで使うようなマクロの定義。

#include "xxx.h"

を使うなら、gccアセンブルする。##で結合できる。関数みたいに定義できたりする。gccを使うときは拡張子は.Sにしないとダメっぽい。

3.ラベルジャンプ

jmp 1f

1:
    push %eax

jmp 1b

ラベル名+f, ラベル名+b で前後どっちのラベルかを指定できる。forwardとbackwardっぽい。

サンプル

/* sample.h */
#define A 1

#if defined (A)
#define LOCAL(sym) _ ## sym

#define FUNCTION(x) .global LOCAL(x) ; LOCAL(x):
#endif
.code32
#include "sample.h"

FUNCTION(abc)
    push %eax

1:
    .macro sample
    push %eax
    push %ebx
    .endm

    sample

    jmp 1b

    jmp 1f

1:
    nop

アセンブル

sample.o:     file format elf32-i386


Disassembly of section .text:

00000000 <_abc>:
   0:   50                      push   %eax
   1:   50                      push   %eax
   2:   53                      push   %ebx
   3:   eb fc                   jmp    1 <_abc+0x1>
   5:   eb 00                   jmp    7 <_abc+0x7>
   7:   90                      nop