割り算りとシフト演算

仕事にかまけてブログの更新を行っておりませんでした。

本日は仕事にてチューニングしなければいけない状態になった際に、試しに割り算とシフト演算を逆アセンブルした結果を乗せたいと思います。

実行環境:Windows Vista
コンパイルgcc(MinGW)

割り算
#include

void main() {
    int val = 512;

    val /= 2;

    printf( "%d\n", val );
}

main.o: file format pe-i386


Disassembly of section .text:

00000000 <_main>:
 0:  55              push   %ebp
 1:  89 e5            mov    %esp,%ebp
 3:  83 e4 f0          and    $0xfffffff0,%esp
 6:  83 ec 20          sub    $0x20,%esp
 9:  e8 00 00 00 00      call   e <_main+0xe>
 e:  c7 44 24 1c 00 02 00  movl   $0x200,0x1c(%esp)
 15:  00
 16:  8b 44 24 1c        mov    0x1c(%esp),%eax
 1a:  89 c2            mov    %eax,%edx
 1c:  c1 ea 1f          shr    $0x1f,%edx
 1f:  8d 04 02          lea    (%edx,%eax,1),%eax
 22:  d1 f8            sar    %eax
 24:  89 44 24 1c        mov    %eax,0x1c(%esp)
 28:  8b 44 24 1c        mov    0x1c(%esp),%eax
 2c:  89 44 24 04        mov    %eax,0x4(%esp)
 30:  c7 04 24 00 00 00 00  movl    $0x0,(%esp)
 37:  e8 00 00 00 00      call    3c <_main+0x3c>
 3c:  c9              leave
 3d:  c3              ret
 3e:  90              nop
 3f:  90              nop


シフト演算
#include

void main() {
    int val = 512;

    val >>= 1;

    printf( "%d\n", val );
}


main2.o: file format pe-i386

Disassembly of section .text:

00000000 <_main>:
 0:   55               push  %ebp
 1:   89 e5             mov   %esp,%ebp
 3:   83 e4 f0           and   $0xfffffff0,%esp
 6:   83 ec 20           sub   $0x20,%esp
 9:   e8 00 00 00 00       call  e <_main+0xe>
 e:   c7 44 24 1c 00 02 00   movl  $0x200,0x1c(%esp)
 15:   00
 16:   d1 7c 24 1c         sarl  0x1c(%esp)
 1a:   8b 44 24 1c         mov   0x1c(%esp),%eax
 1e:   89 44 24 04         mov   %eax,0x4(%esp)
 22:   c7 04 24 00 00 00 00   movl  $0x0,(%esp)
 29:   e8 00 00 00 00       call  2e <_main+0x2e>
 2e:   c9               leave
 2f:   c3               ret


アセンブラの実行命令数が減るようなので早くはなるようです。
しかし今回、逆アセンブルしたのがPC上であり、私が仕事でチューニングしなければいけないものは別環境となってしまうので、その場合にはまた違う結果が出るかと思います。

アセンブル方法のメモ
gcc -c main.c
objdump -d main.o >> main.txt