Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

No. On every implementation I've seen, switch/case is implemented almost exactly like perl's goto-EXPR, except that the target label is actually a machine instruction address known at compile time.

This simple C program

#include <stdio.h> int main() { switch ( 4 ) { case 1: printf( "case1\n" ); break; case 2: printf( "case2\n" ); break; case 3: printf( "case3\n" ); break; case 4: printf( "case4\n" ); break; case 5: printf( "case5\n" ); break; case 6: printf( "case6\n" ); break; } }

Is translated into this assembler

.386p ifdef ??version if ??version GT 500H .mmx endif endif model flat ifndef ??version ?debug macro endm endif ?debug S "test.c" ?debug T "test.c" _TEXT segment dword public use32 'CODE' _TEXT ends _DATA segment dword public use32 'DATA' _DATA ends _BSS segment dword public use32 'BSS' _BSS ends DGROUP group _BSS,_DATA _TEXT segment dword public use32 'CODE' _main proc near ?live1@0: ; ; int main() { ; push ebp mov ebp,esp ; ; ; switch ( 4 ) { ; @1: mov eax,4 cmp eax,6 ja short @2 jmp dword ptr [@10+4*eax] @10: dd @2 dd @9 dd @8 dd @7 dd @6 dd @5 dd @4 ; ; case 1: printf( "case1\n" ); break; ; @9: push offset s@ call _printf pop ecx @13: pop ebp ret ; ; case 2: printf( "case2\n" ); break; ; @8: push offset s@+7 call _printf pop ecx @14: pop ebp ret ; ; case 3: printf( "case3\n" ); break; ; @7: push offset s@+14 call _printf pop ecx @15: pop ebp ret ; ; case 4: printf( "case4\n" ); break; ; @6: push offset s@+21 call _printf pop ecx @16: pop ebp ret ; ; case 5: printf( "case5\n" ); break; ; @5: push offset s@+28 call _printf pop ecx @17: pop ebp ret ; ; case 6: printf( "case6\n" ); break; ; @4: push offset s@+35 call _printf pop ecx ; ; } ; } ; @2: @11: @12: pop ebp ret _main endp _TEXT ends _DATA segment dword public use32 'DATA' s@ label byte ; s@+0: db "case1",10,0 ; s@+7: db "case2",10,0 ; s@+14: db "case3",10,0 ; s@+21: db "case4",10,0 ; s@+28: db "case5",10,0 ; s@+35: db "case6",10,0 align 4 _DATA ends _TEXT segment dword public use32 'CODE' _TEXT ends public _main extrn _printf:near ?debug D "e:\Bcc55\include\_nfile.h" 10459 12320 ?debug D "e:\Bcc55\include\_null.h" 10459 12320 ?debug D "e:\Bcc55\include\_defs.h" 10459 12320 ?debug D "e:\Bcc55\include\_stddef.h" 10459 12320 ?debug D "e:\Bcc55\include\stdio.h" 10459 12320 ?debug D "test.c" 12119 1102 end

The salient part of which is

; switch ( 4 ) { ; @1: mov eax,4 // Load the selector expression (4) cmp eax,6 // Test if its outside the range of options (6) ja short @2 // If it is, jump to the default /* Otherwise, add the value * the size of the lookup table entries (4bytes) and add it to the base address of the dispatch table, then jump to the address help at that location in the table. */ jmp dword ptr [@10+4*eax] @10: dd @2 dd @9 dd @8 dd @7 dd @6 dd @5 dd @4 ; ; case 1: printf( "case1\n" ); break; ; @9:

Originally, the offsets were byte offsets and the table size had a maximum of 255. These days, the table entries are absolute 32-bit addresses and the table size can theoretically be 2 GB in size. In all cases, it is very fast.

The interesting part is coding the switch expression so that it converts your range of possible matches to an integer. String lookups can be index using str(i)(n)cmp() quite easily, but I've had to code some more esoteric versions in the past.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: Re: Re: Re: Re: Perl Idioms Explained: && and || "Short Circuit" operators by BrowserUk
in thread Perl Idioms Explained - && and || "Short Circuit" operators by davido

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and not a whimper to be heard...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2017-07-22 06:18 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (337 votes). Check out past polls.