write ahead log

ロールフォワード用

gccを使って64bit Linux環境で32bit実行ファイルのビルド

動かん

アセンブラを吐かせたいのでGASがちょっと気になって以下のページを見ながらちょこちょこ触っていたら、一部プログラムが動かない.

Capm Network - GAS

なんでかなー、とか思ったけど、自分のマシンが64bitになったのを思い出す.

ということで64bit環境のLinux(Ubuntu)で32bitビルドする方法をメモる.

その前に(蛇足)

64bit版のレジスタとか命令とかちょっと気になったのでいろいろ試した.
以下になるっぽい.

32bit 64bit なに?
movl movq 代入
eax rax アキュムレータレジスタ

へーって感じ.
qはQuad(word:16の4倍だから?) rは何なんだろう.よーわからん.誰かおしえてくれー.
[追記] rはregularの略っぽい. Yahoo知恵袋 - Intel CPUのレジスタについて

ぷらぷらしてたらここらへんがざっくり読めて楽しそうだった.あとで読もう.
x64 アセンブリーの概要

ビルドしたい

ともかくビルドしたい.試せないのは面白くない.

こんなコードを書いても

.file "arithmetic.s"
.data
MSG: .string "%d\n"

.text
.global main
main:
    push %ebp
    movl %esp, %ebp

    movl $2, %eax # eax = 2
    addl $2, %eax # eax = 2 + 2
    subl $2, %eax # eax = 4 - 2

    movl $3, %ebx # ebx = 3
    mull %ebx     # eax = 2 * 3

    movl $0, %edx # 余り領域の初期化
    divl %ebx     # eax = 6 / 3

    /* printfで出力する */
    pushl %eax
    pushl $MSG
    call printf
    leave
    ret

こうなる

twinbird@:~/lab/gas$ gcc arithmetics.s -o arithmetics
arithmetics.s: Assembler messages:
arithmetics.s:8: Error: invalid instruction suffix for `push'
arithmetics.s:21: Error: invalid instruction suffix for `push'

と、調べるとこうすれば良いとある.

gcc -m32 source.s -o executable

-m32オプションとやらがGASにもgccにもあるらしい.

が、そんなオプションは無いと言われる.

ここで同じように困った人を見つけた.

ありがたく試させてもらう.

sudo dpkg --add-architecture i386
sudo apt-get update
sudo apt-get install libc6:i386
sudo apt-get install gcc-multilib

もう一度上記の32bitビルドコマンドを通す.

gcc -m32 source.s -o executable

ビルド出来た!

動かすと

twinbird@:~/lab/gas$ ./arithmetics 
2

味気ないけど、動いて満足.64bitについてももうちょっと調べたいかも.