C++ Virtual Stack

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

Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

C++ Virtual Stack

 
0
  #1
Jul 16th, 2009
My first school project after first year of c++ studying in 10 grade for summer to code virtual stack emulation in C++..

Well i've done it perfectly finished just today !!
It supposed to be for all summer I finished in fast in 1 day lol not nerd

Just wana share it because its very interesting and beginners just like me can play with it and gain cool knowledge how ASM stack works..

My Virtual Stack works just like ASM method LIFO and its Draw it perfectly you will see..

VirtualStack.h
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <windows.h>
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9. class VirtualStack
  10. {
  11. int WordsAmmount;
  12. WORD *StackBase;
  13. WORD *SP;
  14.  
  15.  
  16.  
  17. public:
  18. //Constructor Destructor
  19. VirtualStack(int);
  20. ~VirtualStack();
  21.  
  22. //Additional
  23. void DisplayStack();
  24. void ResetStackBytes();
  25. void ChangeStackElementByID(WORD,WORD);
  26. void ChangeSpRegisterByElementID(WORD);
  27.  
  28.  
  29. //Basic
  30. void Push(WORD);
  31. void Pop(WORD&);
  32.  
  33. };

VirtualStack.cpp
  1. #include "VirtualStack.h"
  2.  
  3. //Shit static variable related to some graphic stuff
  4. static int SchemeNumber = 1;
  5. //Cool shit that makes loading dots hehe
  6. void LoadingDotsBySleepTime(int SleepingTime,int DotsAmmount)
  7. {
  8. for(int x = 1; x<=DotsAmmount; x++)
  9. {
  10. cout << ".";
  11. Sleep(SleepingTime);
  12. }
  13. }
  14. VirtualStack::VirtualStack(int Size)
  15. {
  16.  
  17. WordsAmmount = Size;
  18. cout << "Creating Virtual Stack";LoadingDotsBySleepTime(500,5);
  19. cout << endl << endl;
  20. cout << "Allocating Virtual Bytes"; LoadingDotsBySleepTime(200,4);
  21. cout << endl << endl;
  22. StackBase = new WORD[Size];
  23. SP = &StackBase[WordsAmmount-1];
  24. Sleep(1000);
  25.  
  26.  
  27.  
  28. }
  29. VirtualStack::~VirtualStack()
  30. {
  31. delete[] StackBase;
  32. MessageBox(0,"Stack Destroyed","Stack Information",MB_OK);
  33. }
  34.  
  35. void VirtualStack::DisplayStack()
  36. {
  37. cout << "Preparing to Draw Scheme Number: " << SchemeNumber; LoadingDotsBySleepTime(400,5);
  38. cout << endl << endl;
  39. cout << "SP register: " << SP << endl << endl;
  40.  
  41. for(int Obj = (WordsAmmount-1); Obj >= 0; Obj--)
  42. {
  43. cout << "[" << Obj << "] ";
  44. cout << "[" << &StackBase[Obj] << "]" << " Cell Value: " << StackBase[Obj];
  45. if( SP == &StackBase[Obj])
  46. cout << " ---> SP Register Points Here";
  47. cout << endl;
  48.  
  49. }
  50. cout << endl << endl << endl;
  51. SchemeNumber++;
  52. }
  53.  
  54. void VirtualStack::ResetStackBytes()
  55. {
  56. for(int Obj = 0; Obj <= (WordsAmmount-1); Obj++)
  57. {
  58. StackBase[Obj]=0;
  59. }
  60. }
  61.  
  62. void VirtualStack::Push(WORD WordValue)
  63. {
  64. SP--;
  65. *SP = WordValue;
  66.  
  67. }
  68.  
  69. void VirtualStack::Pop(WORD &Destination)
  70. {
  71. Destination = *SP;
  72. SP++;
  73.  
  74. }
  75.  
  76. void VirtualStack::ChangeStackElementByID(WORD ElementID,WORD ElementValue)
  77. {
  78. StackBase[ElementID] = ElementValue;
  79. }
  80.  
  81. void VirtualStack::ChangeSpRegisterByElementID(WORD ElementID)
  82. {
  83. SP = &StackBase[ElementID];
  84. }

main.cpp
  1.  
  2.  
  3. #include "VirtualStack.h"
  4.  
  5.  
  6. int main()
  7. {
  8.  
  9. VirtualStack VSObject(8);
  10. VSObject.ResetStackBytes();
  11.  
  12.  
  13. VSObject.Push(100);
  14. VSObject.Push(50);
  15. VSObject.Push(10);
  16. VSObject.DisplayStack();
  17. VSObject.ChangeSpRegisterByElementID(3);
  18. VSObject.DisplayStack();
  19. VSObject.ChangeStackElementByID(3,90);
  20. VSObject.DisplayStack();
  21.  
  22.  
  23.  
  24.  
  25.  
  26. system("PAUSE");
  27. return 0;
  28. }

You can play with it use all basic funcs like push pop and even more advanced i made to emulate and get more femiliar with stack

isnt that cool lol i only learned 1 year !!!! i'm so l33t!
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
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