JMP Is Optimized?

From: Bryan Parkoff (bryan.nospam.parkoff_at_nospam.com)
Date: 03/27/04


Date: Sat, 27 Mar 2004 21:14:02 +0000 (UTC)


    I understand that Matt Taylor has advised me that switch (...) is always
optimized by C/C++ Compiler rather than doing optimization by my hand.
Please look at my machine code below.

    push esi
    xor esi,esi
    cmp esi,4
    ja $L608+0Fh
    jmp dword ptr [esi*4+401038h]
    push 0
    jmp $L608+2 // goto printf
    push 1
    jmp $L608+2 // goto printf
    push 2
    jmp $L608+2 // goto printf
    push 3
    jmp $L608+2 // goto printf
    push 4
    push offset string "%d"
    call _printf
    add esp,8
    inc esi
    cmp esi,5
    jl main+3
    pop esi
    ret

    Notice that my first machine code has "jmp $L608+2 // goto
printf" is executed each time it encounters then it goes back again to
execute next "jmp $L608+2 // goto printf". It is the fact that
switch (...) is inside while (x < y).
    Look at my second machine code.

    push esi
    xor esi,esi
    cmp esi,4
    ja $L608+0Fh
    jmp dword ptr [esi*4+401038h]
    push 0
    push offset string "%d"
    call _printf
    add esp,8
    inc esi
    cmp esi,5
    jl main+3
    jmp dword ptr [esi*4+401038h]

    push 1
    push offset string "%d"
    call _printf
    add esp,8
    inc esi
    cmp esi,5
    jl main+3
    jmp dword ptr [esi*4+401038h]

    push 2
    push offset string "%d"
    call _printf
    add esp,8
    inc esi
    cmp esi,5
    jl main+3
    jmp dword ptr [esi*4+401038h]

    push 3
    push offset string "%d"
    call _printf
    add esp,8
    inc esi
    cmp esi,5
    jl main+3
    jmp dword ptr [esi*4+401038h]

    push 4
    push offset string "%d"
    call _printf
    add esp,8
    inc esi
    cmp esi,5
    jl main+3
    jmp dword ptr [esi*4+401038h]

    pop esi
    ret

    Notice "jmp $L608+2 // goto printf" is replaced to "jmp
dword ptr [esi*4+401038h]". Each "case xxx" has its own individual code
rather than single code through JMP $L608+2". "JMP [esi*4+xxxxxxxx]" helps
to handle individual code.

    I believe that second machine code can be very optimized better than
first machine code by running much faster, but I am aware that second
machine code can increase the size like duplicated JMP routine.

    Please tell me what you think is best to use first machine code or
second machine code. Thanks...

-- 
Bryan Parkoff


Relevant Pages

  • Re: switch or if?
    ... > to use a switch statement? ... I tested two scenarios, dense switches, and sparse switches. ... The machine code generated for if... ... There are Ojump instructions, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Special forms vs. Macros
    ... you need to know which expressions are normal function ... implementation-dependent expressions that are treated specially, ... ordinary forms is generally just an optimization. ... fact that is compiled directly into machine code rather than a ...
    (comp.lang.lisp)
  • Re: Hundreds of cases in a switch, optimization?
    ... > In terms of generated machine code, how does hundreds of cases in a ... Do compilers or processors ... > do any optimization on such structured code? ... binary search-like approach. ...
    (comp.lang.c)
  • Re: switch or if?
    ... >> to use a switch statement? ... There are Ojump instructions, ... > The machine code generated for switchdepends on whether we do a dense ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: a modest proposal
    ... machine code, or in this case, c to machine code). ... writing fortran, translating it to c, then optimizing c code? ... language for further processing and optimization. ...
    (comp.lang.fortran)