did you know you can do this?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

did you know you can do this?

 
0
  #1
Mar 13th, 2009
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6.  
  7. int a;
  8. int b;
  9.  
  10. asm("jmp c\n\t");
  11.  
  12. a=7;
  13. b=12;
  14.  
  15. asm("d:\n\t");
  16.  
  17. cout<< a<< endl<< b<< endl;
  18.  
  19. return 0;
  20.  
  21. }
  22.  
  23. int pewp(){
  24.  
  25. int a;
  26. int b;
  27.  
  28. asm("c:\n\t");
  29.  
  30. a = 3;
  31. b = 2;
  32.  
  33. asm("jmp d\n\t");
  34.  
  35. }

this will output

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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 142
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 34
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: did you know you can do this?

 
0
  #2
Mar 13th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 971
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is online now Online
Posting Shark

Re: did you know you can do this?

 
0
  #3
Mar 13th, 2009
Good-luck keeping your sanity that way. You should just leave that as behind the scenes stuff.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: did you know you can do this?

 
0
  #4
Mar 13th, 2009
I tried compiling that with vc++ 2008 Express, and the Microsoft compiler hates it. It doesn't like the "c\n\t" part.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: did you know you can do this?

 
0
  #5
Mar 13th, 2009
Didn't know this but in C++ you can use pointers or references to achieve the same as you did ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: did you know you can do this?

 
0
  #6
Mar 14th, 2009
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 __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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC