www.digitalmars.com         C & C++   DMDScript  

c++ - Inline Assembler Question

reply "Ed Schroder" <rebel777 home.nl> writes:
I am looking for a way to use the "indirect jump" within a routine, actually
how to create your own "switch case" using inline-assembly. Suppose I have
this:

switch(var)
    {              case  0 : goto label_00;
                    case  1 : goto label_01;
                    case  2 : goto label_02;
                    case  3 : goto label_03;
                    case  4 : goto label_04;
    }

Then how do I create the (jump) addresses for label_00, label_01 ........
label_04 ?

I couldn't find the answer in the documentation.

Thanks for answers.

My best,

Ed
Nov 10 2003
next sibling parent reply "Gisle Vanem" <giva users.sourceforge.net> writes:
"Ed Schroder" <rebel777 home.nl> wrote:

 I am looking for a way to use the "indirect jump" within a routine, actually
 how to create your own "switch case" using inline-assembly. Suppose I have
 this:
 
 switch(var)
     {              case  0 : goto label_00;
                     case  1 : goto label_01;
                     case  2 : goto label_02;
                     case  3 : goto label_03;
                     case  4 : goto label_04;
     }
 
 Then how do I create the (jump) addresses for label_00, label_01 ........
 label_04 ?

Labels cannot be used as addresses in an array or whatever. Only GNU C can do that AFAIK. You better use an array of functions and do an indirect call. Something like: void (*funcs[]) (void) = { func_00, func_01 }; int x = 0; __asm { lea eax, funcs mov ebx, x call [4*ebx+eax] } (depending on model of course). BTW. GNU C allows this dangerous trick: int main (void) { static void *label[] = { &&label_0, &&label_1 }; int x = 0; goto *label[1]; label_0: label_1: return (0); } --gv
Nov 10 2003
parent reply "Ed Schroder" <rebel777 home.nl> writes:
Thanks Gisle for answer.

I am aware of the call-technique and practice it wherever it is useful. But
in the end the "switch-case" command when it is compiled creates such a
jump-table. Meaning, if the compiler can why shouldn't the user?

When you watch the assembler output the indirect jump is present including a
generated jump-table. I am sure there must be some tricky way to create the
jump-table yourself. I just hope Walter is reading this :)

My best,

Ed

==================

"Gisle Vanem" <giva users.sourceforge.net> wrote in message
news:booinr$15um$1 digitaldaemon.com...
 "Ed Schroder" <rebel777 home.nl> wrote:

 I am looking for a way to use the "indirect jump" within a routine,


 how to create your own "switch case" using inline-assembly. Suppose I


 this:

 switch(var)
     {              case  0 : goto label_00;
                     case  1 : goto label_01;
                     case  2 : goto label_02;
                     case  3 : goto label_03;
                     case  4 : goto label_04;
     }

 Then how do I create the (jump) addresses for label_00, label_01


 label_04 ?

Labels cannot be used as addresses in an array or whatever. Only GNU C can do that AFAIK. You better use an array of functions and do an indirect call. Something like: void (*funcs[]) (void) = { func_00, func_01 }; int x = 0; __asm { lea eax, funcs mov ebx, x call [4*ebx+eax] } (depending on model of course). BTW. GNU C allows this dangerous trick: int main (void) { static void *label[] = { &&label_0, &&label_1 }; int x = 0; goto *label[1]; label_0: label_1: return (0); } --gv

Nov 11 2003
parent reply "Walter" <walter digitalmars.com> writes:
At the moment, you can't create a jump table with the inline assembler.
Sorry.

"Ed Schroder" <rebel777 home.nl> wrote in message
news:boqcn4$pk9$1 digitaldaemon.com...
 Thanks Gisle for answer.

 I am aware of the call-technique and practice it wherever it is useful.

 in the end the "switch-case" command when it is compiled creates such a
 jump-table. Meaning, if the compiler can why shouldn't the user?

 When you watch the assembler output the indirect jump is present including

 generated jump-table. I am sure there must be some tricky way to create

 jump-table yourself. I just hope Walter is reading this :)

 My best,

 Ed

 ==================

 "Gisle Vanem" <giva users.sourceforge.net> wrote in message
 news:booinr$15um$1 digitaldaemon.com...
 "Ed Schroder" <rebel777 home.nl> wrote:

 I am looking for a way to use the "indirect jump" within a routine,


 how to create your own "switch case" using inline-assembly. Suppose I


 this:

 switch(var)
     {              case  0 : goto label_00;
                     case  1 : goto label_01;
                     case  2 : goto label_02;
                     case  3 : goto label_03;
                     case  4 : goto label_04;
     }

 Then how do I create the (jump) addresses for label_00, label_01


 label_04 ?

Labels cannot be used as addresses in an array or whatever. Only GNU C can do that AFAIK. You better use an array of functions and do an indirect call. Something like: void (*funcs[]) (void) = { func_00, func_01 }; int x = 0; __asm { lea eax, funcs mov ebx, x call [4*ebx+eax] } (depending on model of course). BTW. GNU C allows this dangerous trick: int main (void) { static void *label[] = { &&label_0, &&label_1 }; int x = 0; goto *label[1]; label_0: label_1: return (0); } --gv


Nov 11 2003
parent reply "Ed Schroder" <rebel777 home.nl> writes:
"Walter" <walter digitalmars.com> wrote in message
news:bor6u2$211k$1 digitaldaemon.com...
 At the moment, you can't create a jump table with the inline assembler.
 Sorry.

Thanks for clarification, so I can stop my odyssey looking for it :) Ed
 "Ed Schroder" <rebel777 home.nl> wrote in message
 news:boqcn4$pk9$1 digitaldaemon.com...
 Thanks Gisle for answer.

 I am aware of the call-technique and practice it wherever it is useful.

 in the end the "switch-case" command when it is compiled creates such a
 jump-table. Meaning, if the compiler can why shouldn't the user?

 When you watch the assembler output the indirect jump is present


 a
 generated jump-table. I am sure there must be some tricky way to create

 jump-table yourself. I just hope Walter is reading this :)

 My best,

 Ed

 ==================

 "Gisle Vanem" <giva users.sourceforge.net> wrote in message
 news:booinr$15um$1 digitaldaemon.com...
 "Ed Schroder" <rebel777 home.nl> wrote:

 I am looking for a way to use the "indirect jump" within a routine,


 how to create your own "switch case" using inline-assembly. Suppose




 have
 this:

 switch(var)
     {              case  0 : goto label_00;
                     case  1 : goto label_01;
                     case  2 : goto label_02;
                     case  3 : goto label_03;
                     case  4 : goto label_04;
     }

 Then how do I create the (jump) addresses for label_00, label_01


 label_04 ?

Labels cannot be used as addresses in an array or whatever. Only GNU C can do that AFAIK. You better use an array of functions and do an indirect call. Something like: void (*funcs[]) (void) = { func_00, func_01 }; int x = 0; __asm { lea eax, funcs mov ebx, x call [4*ebx+eax] } (depending on model of course). BTW. GNU C allows this dangerous trick: int main (void) { static void *label[] = { &&label_0, &&label_1 }; int x = 0; goto *label[1]; label_0: label_1: return (0); } --gv



Nov 12 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Ed Schroder" <rebel777 home.nl> wrote in message
news:bost9i$1mjv$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:bor6u2$211k$1 digitaldaemon.com...
 At the moment, you can't create a jump table with the inline assembler.
 Sorry.

Thanks for clarification, so I can stop my odyssey looking for it :)

Probably the best solution is to write the switch statement in C, then write the statements following each case in inline assembly.
Nov 12 2003
parent "Ed Schroder" <rebel777 home.nl> writes:
"Walter" <walter digitalmars.com> wrote in message
news:bot23j$1tmm$1 digitaldaemon.com...
 "Ed Schroder" <rebel777 home.nl> wrote in message
 news:bost9i$1mjv$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:bor6u2$211k$1 digitaldaemon.com...
 At the moment, you can't create a jump table with the inline



 Sorry.

Thanks for clarification, so I can stop my odyssey looking for it :)

Probably the best solution is to write the switch statement in C, then

 the statements following each case in inline assembly.

That was my solution too. Thanks Walter for support and keep up the good work. Ed
Nov 14 2003
prev sibling parent Keith Fuller <Keith_member pathlink.com> writes:
Just by way of asking a dumb question,
you can't have the switch statement in C and have it jump to some inline
assembler code?

Keith Fuller
keithfx H*tmail.com

In article <boofcq$10sl$1 digitaldaemon.com>, Ed Schroder says...
I am looking for a way to use the "indirect jump" within a routine, actually
how to create your own "switch case" using inline-assembly. Suppose I have
this:

switch(var)
    {              case  0 : goto label_00;
                    case  1 : goto label_01;
                    case  2 : goto label_02;
                    case  3 : goto label_03;
                    case  4 : goto label_04;
    }

Then how do I create the (jump) addresses for label_00, label_01 ........
label_04 ?

I couldn't find the answer in the documentation.

Thanks for answers.

My best,

Ed

Nov 11 2003