Em 5/11/2011 20:36, Harold Hallikainen escreveu: > I'm mostly doing PIC32 stuff now with C32, the linker, etc. I do a little > assembly for it, but very little. I find linker scripts very cryptic. It'= d > sure be nice if they were commented a bit more. It'd also be nice to > easily define the address where a function is to be placed. The new > version of C32 has an attribute that allows this, but it appears there ar= e > various issues with this new version (especially in printf). > > In my bootloader, I'd like to have a jump table at a specific address tha= t > does not move as I make code changes. I see how I could do it in assembly > (including use of org, which, it appears, is based on the section start, > not the absolute address), but it'd be nice to be able to do it in C. > Something like: > > org SomeAbsoluteVirtualAddress > function1: goto LocalFunction1; > Function2: goto LocalFunction2; > Function3: goto LocalFunction3; > > The "LocalFunctions" would still do all the stack operations (getting > parameters, allocating local variables, cleaning up the stack, etc.). The > jump table would just provide a fixed address to access the function. > > I did this on the PIC24, building my own interrupt jump table since the > PIC24 places the interrupt vector table in the boot section. I just had > all those original interrupt vectors point into my jump table, which then > pointed to the actual interrupt function. I THINK I did it in C (I'll hav= e > to look at the code), but MAY have done it in assembly. > > I tried the above stuff ( > function1: goto LocalFunction1; > Function2: goto LocalFunction2; > Function3: goto LocalFunction3; > ), but the C32 compiler did not like it. I don't think it liked the label= s > (expecting a return value type instead). For now, I've put functions that > call local functions at the top of the section, but that uses more stack > space and is pretty inefficient. > > So... how do I do a jump table in C32? Or do I go back to assembly? > > THANKS! > > Harold First, you have to deal with linker-scripts to be able to put your jump table at the fixed and predefined position, creating a named section with 'protected' attribute. Then you have to use function-pointers, which is the C way of calling functions by pointer. Example: int (*fp1)( int a, int b, int c ); // fp1 is a function-pointer variable that may point to a function returning int and taking 3 ints as arguments. void (*fp2)( void ); // fp2 is a function-pointer that points to a function returning void and taking no arguments. fp1 =3D LocalFunction1; // Please not the lack of parenthesis and arguments. You are taking the address of the function. fp2 =3D LocalFunction2; x =3D fp1( 1, 2, 3 ); fp2(); fp1 =3D LocalFunction3; y =3D fp1( 4, 5, 6 ); ALTERNATIVE ( build-time table creation): typedef int (*functionpointer_t)( int a, int b, int c ); functionpointer_t FunctionTable[NUMBER_OF_ENTRIES] =3D { lf1, lf2, lf3, .... }; x =3D FunctionTable[i]( a, b, c ); // Call function number 'i' with arguments 'a', 'b', 'c'. Best regards, Isaac --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .