Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
942 views
in Technique[技术] by (71.8m points)

assembly - How to "goto" into different function in c?

Basically I am trying to simulate assembly code in C.

Here is the C code:

int main()
{
   test();
main_next:
   printf("Hello, World!");
}

void test()
{
     goto main_next;
}

Trying to compile this code (Linux 32 bit, gcc 4.6.3), I got this error:

 error: label ‘main_randomtag_next’ used but not defined

Does anyone know how to do this kind of inter-procedural goto in C?

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

But what about the children? stack?

goto between functions doesn't make any sense if you think about the stack. What will be on the stack when you jump? The source and destination functions could potentially have different arguments and a different return value. Who will the new function return to? Will its return value even make sense to the caller? The caller called the source function, not the destination function.

Return to caller?

Consider your example closely:

int main()
{
   test();
main_next:
   printf("hello, world);
}

void test()
{
     goto main_next;
}

What happens when the goto executes? I presume you'd want this to jump up the stack back to the calling main() function. The goto would effectively be the same as a return, changing the call stack from:

main()                            main()
|                   to            
+--> test()                       

But what if you wanted to jump to a function that isn't in the call stack? What then?

Or replace the current function?

A different interpretation is that the goto would replace the existing test() call with one to main(). The call stack would change from:

main()                            main()
|                   to            |
+--> test()                       +--> main()

Now main() is recursively calling itself, and the lower main() will return to the upper main()—who, by the way, is expecting a void return value but is going to receive an int.

setjmp and longjmp

The closest you can get is with setjmp / longjmp. These allow you to save and restore the stack context for nonlocal goto, allowing you to jump between function calls.

setjmp and longjmp get around the problems I described by (a) saving and restoring the full stack context when jumping, and (b) not allowing jumps if the stack context is no longer valid. I quote from the man page (emphasis mine):

setjmp() and longjmp(3) are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. setjmp() saves the stack context/environment in env for later use by longjmp(3). The stack context will be invalidated if the function which called setjmp() returns.

To put it another way, longjmp is basically the C equivalent of throwing an exception. A low-level function can unwind the call stack and resume execution at a much higher level function.

It's also awfully tricky to use, and rarely a good idea. Again, from the man page:

setjmp() and sigsetjmp() make programs hard to understand and maintain. If possible an alternative should be used.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...