アセンブリ言語の教科書 第3章 数値のカウント(再帰で)

Scheme勉強中なので、GASで再帰します。

.text
.global main

main:   push %ebp
        mov  %esp,%ebp

        mov  $10, %eax
        push %eax
        call count
        pop %eax

        mov  $0, %eax
        leave
        ret
 
count:  push %ebp
        mov  %esp,%ebp

        mov 8(%ebp),%eax

        push %eax
        push $count_s
        call printf
        pop %eax
        pop %eax

        dec %eax
        cmp $0, %eax
        je  1f

        push %eax
        call count
        pop %eax

1:      leave
        ret

.data
count_s: .string "count : %d\n"
% gcc -o count count.s
% ./count
count : 10
count : 9
...(略
count : 2
count : 1
  • 再帰はできたけど、printfを外に出したい。
  • 再帰で書くと、push,popがやたら増えますね。いかに減らすかが最適化の鍵なんですかねぇ・・・。
  • GASもSchemeも修行が必要。

追記

あ、decで再代入してる。