| | |
did you know you can do this?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
cpp Syntax (Toggle Plain Text)
#include<iostream> using namespace std; int main(){ int a; int b; asm("jmp c\n\t"); a=7; b=12; asm("d:\n\t"); cout<< a<< endl<< b<< endl; return 0; } int pewp(){ int a; int b; asm("c:\n\t"); a = 3; b = 2; asm("jmp d\n\t"); }
this will output
C++ Syntax (Toggle Plain Text)
3 2
what I don't get is why although it jumps to the middle of the pewp() functions, it still sets the variables properly...
does this mean that, when the rest of the code gets compiled, the a= and b= in pewp() get sent to the same spot in memory that the a= and b= in main() would have? If I name the variables in pewp() c and d it still sets a and b in main(), so are these variables put on the stack or what? I guess I have lots to learn about what happens to the data in different declarations -.-
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Yes, a and b in both main() and pewp() are declared on the stack in the same way. As you have identical declarations (just a and b) for each function you can jump into pewp() and it uses the stack allocated by main(). This may be an interesting exercise, but I hope you don't need me to tell you that doing anything close to this in software you might want to use is a complete no-no. Have fun experimenting though!
yeah, I know it's definitely a no-no, I just thought it was kind of cool, being able to cheat the compiler. Get back at it, for all these years of yelling at me, and telling me how wrong I am. Take that!
And the inline-assembly portion of it is compiler specific, since I just use a text editor and not an IDE I had to look up how to do it with my compiler, I'm using gcc- I believe vc++ uses
and yeah, it would be better done with references but I just thought it was awesome. I wonder what would happen if the a and b in pewp() were defined as strings, or something else--how that would affect the stack in main().
And the inline-assembly portion of it is compiler specific, since I just use a text editor and not an IDE I had to look up how to do it with my compiler, I'm using gcc- I believe vc++ uses
__asm__ either followed by an instruction or followed by several, on separate lines between braces (without the \n\t or quotes)--also, I believe gcc uses at&t assembly format while vc++ uses something else...and yeah, it would be better done with references but I just thought it was awesome. I wonder what would happen if the a and b in pewp() were defined as strings, or something else--how that would affect the stack in main().
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
![]() |
Other Threads in the C++ Forum
- Previous Thread: Recursion in C++
- Next Thread: solving a series
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






