Fascinating new switch mechanism in assembler
Walter Bright
newshound at digitalmars.com
Sat Mar 18 11:08:24 PST 2006
"Dan Lewis" <Dan_member at pathlink.com> wrote in message
news:dvfnur$a9$1 at digitaldaemon.com...
> Hi guys,
> I've been working on a lexical analyzer for my new scripting engine, and I
> stumbled upon an interesting algorithm.
>
> Essentially:
>
> goto ((charCode << 2)+subroutinePointerArray);
>
> The advantage of this strategy is the elimination of some 90% of the
> conditional
> testing for each character in a string being interpreted (Yay for code
> that runs
> in 40% the time!).
Given:
int test(int i)
{
switch (i)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
return i;
default:
return i + 1;
}
}
The assembler produced is:
?test@@YAHH at Z:
push EBX
mov EBX,8[ESP]
sub EBX,1
cmp EBX,6
ja L1A
jmp dword ptr FLAT:_DATA[00h][EBX*4]
mov EAX,8[ESP]
pop EBX
ret
L1A: mov EAX,8[ESP]
inc EAX
pop EBX
ret
_TEXT ends
_DATA segment
dd offset FLAT:?test@@YAHH at Z[014h]
dd offset FLAT:?test@@YAHH at Z[014h]
dd offset FLAT:?test@@YAHH at Z[014h]
dd offset FLAT:?test@@YAHH at Z[014h]
dd offset FLAT:?test@@YAHH at Z[014h]
dd offset FLAT:?test@@YAHH at Z[014h]
dd offset FLAT:?test@@YAHH at Z[014h]
_DATA ends
More information about the Digitalmars-d
mailing list