Archived

This topic is now archived and is closed to further replies.

hello2k1

An array of jumps

Recommended Posts

After something like 2 hours of searching, and talking to my partener, I just can''t figure out how to do something like: jumps jArray[]={j1, j2, j3, j4}; goto jArray[2]; j1: j2: j3: j4: I have tried using defines, treating it as raw memory, etc.. But nothing seems to compile. Right now I am considering using x86, but am trying not to do so because of the new 64 bits standards being introduced now.

Share this post


Link to post
Share on other sites
The reason you cannot do what you want is simple ... a goto jumps to a location denoted by a LABEL ... which only has a meaning at compile time. There is no data type for a label, with which you can put it in a (run time) array, and then use it.

The closest thing you are looking for is simply pointers, often even, function pointers .... and you make a jump table, by filing an array of function pointers with pointers which make sense based on a particular situation, such as (sloppy example)


  
//I can''t remember function ptr systax, but assume you have

// functions: foo(), doSomething(), third(), which all follow

// the prototype which matches a typedef alias ''funcPtrType''

// then you might do this:


// prepare the array of function pointers

funcPtrType functions[3] = {foo,doSomething,third};
...
// get the index of the command to execute

cmd = GetNextCommand();
// get and execute the corresponding function pointer

functions[cmd]();


hope that helps

Share this post


Link to post
Share on other sites
Another alternative: look at longjmp() and setjmp()

The problem as Xai points out is that the labels dissolve into nothing. The optimiser might even reorganise the program flow.

You don''t really specify any context, but what you''ve posted is nothing more than a switch...case, even if you intend changing the contents of jArray, surely it could still be done that way.

Other than that, an array of function pointers would get my next vote

A few other slightly sick & twisted methods spring to mind, such as locally defined structures with the labels inside etc...

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
There is nothing I can think of that''ll do what you want. What I would suggest respectfully though, is that you make a study of structured programming. Using structured programming techniques you should find that you will never need to use "goto" in your programs. In the long run, structured programming techniques will help you to design and implement bigger and more complex programs. The continued use of "goto" will (imho) only serve to hinder your progress as a programmer.

Roo, Pantheon Software

Share this post


Link to post
Share on other sites
quote:
Original post by hello2k1
After something like 2 hours of searching, and talking to my partener, I just can''t figure out how to do something like:

jumps jArray[]={j1, j2, j3, j4};
goto jArray[2];
j1:
j2:
j3:
j4:

Oh, come off it...

  
void j1() { /* ... */ }
void j2() { /* ... */ }
void j3() { /* ... */ }
void j4() { /* ... */
}

int main()
{
j2();
}

quote:

Right now I am considering using x86, but am trying not to do so because of the new 64 bits standards being introduced now.

It looks to me like you *are* using assembly language, just via a C compiler.

Share this post


Link to post
Share on other sites
Ok, first of all, comments like that annoy me. I have been programming for 4 years now, own a gaming company, and am currently doing contract work for another gaming company. Goto statements are simply what popped into my head at the moment, and I am well aware the problems associated with them. Structured programming is exactly what I''m trying to avoid in this instance, simply because it will cost me a very large amount of cpu cycles - which we simply can''t afford to waste.

Also, it may seem as though I''m using asm, but in fact I am not since none of the parils are associated with it. x86 assembly is quickly becoming de-standartized, and I would not want to be stuck in the middle of the 64-bit problem.

Even though x86 may become problematic (and ROM material), I feel that it may be the only way to surpass this problem. If anyone has any suggestions, I would really like to hear them.

Thank you.

Share this post


Link to post
Share on other sites
quote:
Original post by hello2k1
I could use a switch, but the reason I don''t want to is because there could easily become 100s of items in the list. Is there anything you guys can think of thatd wor in this case?

A good compiler will make a jump table out of a switch.


Mike

Share this post


Link to post
Share on other sites
It''s difficult to suggest a solution if you don''t say why exactly you don''t want to use switch/case or function pointers.

I don''t see the major difference in what you mention in your original post and an array of function pointers or a switch statement.

Share this post


Link to post
Share on other sites
hello2k1: Have you even TRIED it yet using a switch statement (which is, after all, just a high-level jump table)?

Don''t pre-optimize. You''ll just end up pulling your hair out. Get the damn thing working first.

Share this post


Link to post
Share on other sites
Never looked at switch disassembly, so...

      
int foo (int a)
{
switch (a)
{
case 0:
return 42;
case 1:
return 667;
case 2:
return 0x2b;
case 3:
return ~0x2b;
default:
return 0;
}
}

//disassembly:

PUBLIC ?foo@@YAHH@Z ; foo
; COMDAT ?foo@@YAHH@Z
_TEXT SEGMENT
_a$ = 8
?foo@@YAHH@Z PROC NEAR ; foo, COMDAT

; 3 : switch (a)
; 4 : {

mov eax, DWORD PTR _a$[esp-4]
cmp eax, 3
ja SHORT $L276
jmp DWORD PTR $L281[eax*4]
$L272:

; 5 : case 0:
; 6 : return 42;

mov eax, 42 ; 0000002aH

; 15 : }
; 16 : }

ret 0
$L273:

; 7 : case 1:
; 8 : return 667;

mov eax, 667 ; 0000029bH

; 15 : }
; 16 : }

ret 0
$L274:

; 9 : case 2:
; 10 : return 0x2b;

mov eax, 43 ; 0000002bH

; 15 : }
; 16 : }

ret 0
$L275:

; 11 : case 3:
; 12 : return ~0x2b;

mov eax, -44 ; ffffffd4H

; 15 : }
; 16 : }

ret 0
$L276:

; 13 : default:
; 14 : return 0;

xor eax, eax

; 15 : }
; 16 : }

ret 0
npad 1
$L281:
DD $L272
DD $L273
DD $L274
DD $L275
?foo@@YAHH@Z ENDP ; foo
_TEXT ENDS
END

"jmp DWORD PTR $L281[eax*4]" jumps to the place indicated by the jump table at the end of the function ($L281). It appears the answer to your question is, "switch".


[edited by - Stoffel on August 13, 2002 6:43:32 PM]

Share this post


Link to post
Share on other sites