On 8/1/2017 9:25 AM, Rick C. Hodgin wrote:
> It would work with repeated code on a consistent cadence:
A proper implementation that works in Visual Studio 2015:
#include <stdlib.h>
#include <stdio.h>
char switchdefault[] = "switch default";
char switch0[] = "switch 0";
char switch1[] = "switch 1";
char switch2[] = "switch 2";
char switch3[] = "switch 3";
char switch4[] = "switch 4";
char switch5[] = "switch 5";
char switch6[] = "switch 6";
char switch7[] = "switch 7";
int main(int argc, char* argv[])
{
unsigned char x, lnI;
char* tptr;
// Loop for testing all possible values
for (lnI = 0; lnI < 8; lnI++)
{
if (lnI == 0) x = 0;
else x = 1 << (lnI - 1);
_asm {
movzx edx, byte ptr x
shr edx,4
movzx ebx, byte ptr x
and ebx,0xf
// 5-bytes for each jmp DWORD below
lea edx, [edx*4 + edx]
add edx, offset high_00
jmp edx
high_00:
jmp DWORD ptr test_low_nibble // No bits
jmp DWORD ptr switch_statement_4 // 0001
jmp DWORD ptr switch_statement_5 // 001x
jmp DWORD ptr switch_statement_5 // 001x
jmp DWORD ptr switch_statement_6 // 01xx
jmp DWORD ptr switch_statement_6 // 01xx
jmp DWORD ptr switch_statement_6 // 01xx
jmp DWORD ptr switch_statement_6 // 01xx
jmp DWORD ptr switch_statement_7 // 1xxx
jmp DWORD ptr switch_statement_7 // 1xxx
jmp DWORD ptr switch_statement_7 // 1xxx
jmp DWORD ptr switch_statement_7 // 1xxx
jmp DWORD ptr switch_statement_7 // 1xxx
jmp DWORD ptr switch_statement_7 // 1xxx
jmp DWORD ptr switch_statement_7 // 1xxx
jmp DWORD ptr switch_statement_7 // 1xxx
test_low_nibble:
// 5-bytes for each jmp DWORD below
lea ebx, [ebx*4 + ebx]
add ebx, offset low_00
jmp ebx
low_00:
jmp DWORD ptr switch_statement_default // No bits
jmp DWORD ptr switch_statement_0 // 0001
jmp DWORD ptr switch_statement_1 // 001x
jmp DWORD ptr switch_statement_1 // 001x
jmp DWORD ptr switch_statement_2 // 01xx
jmp DWORD ptr switch_statement_2 // 01xx
jmp DWORD ptr switch_statement_2 // 01xx
jmp DWORD ptr switch_statement_2 // 01xx
jmp DWORD ptr switch_statement_3 // 1xxx
jmp DWORD ptr switch_statement_3 // 1xxx
jmp DWORD ptr switch_statement_3 // 1xxx
jmp DWORD ptr switch_statement_3 // 1xxx
jmp DWORD ptr switch_statement_3 // 1xxx
jmp DWORD ptr switch_statement_3 // 1xxx
jmp DWORD ptr switch_statement_3 // 1xxx
jmp DWORD ptr switch_statement_3 // 1xxx
switch_statement_default:
mov tptr,offset switchdefault
jmp done
switch_statement_0:
mov tptr,offset switch0
jmp done
switch_statement_1:
mov tptr,offset switch1
jmp done
switch_statement_2:
mov tptr,offset switch2
jmp done
switch_statement_3:
mov tptr,offset switch3
jmp done
switch_statement_4:
mov tptr,offset switch4
jmp done
switch_statement_5:
mov tptr,offset switch5
jmp done
switch_statement_6:
mov tptr,offset switch6
jmp done
switch_statement_7:
mov tptr,offset switch7
done:
}
printf("%s\n", tptr);
}
return 0;
}
A minor extension would allow a WORD or DWORD or QWORD value to be
utilized. In this way, at each use the custom lookup table required
for that switch () statement usage would be derived. And if you
simply needed the value, a function could be used when the BSR and
BSF instructions are not available.