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
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
class VirtualStack
{
int WordsAmmount;
WORD *StackBase;
WORD *SP;
public:
//Constructor Destructor
VirtualStack(int);
~VirtualStack();
//Additional
void DisplayStack();
void ResetStackBytes();
void ChangeStackElementByID(WORD,WORD);
void ChangeSpRegisterByElementID(WORD);
//Basic
void Push(WORD);
void Pop(WORD&);
};
VirtualStack.cpp
#include "VirtualStack.h"
//Shit static variable related to some graphic stuff
static int SchemeNumber = 1;
//Cool shit that makes loading dots hehe
void LoadingDotsBySleepTime(int SleepingTime,int DotsAmmount)
{
for(int x = 1; x<=DotsAmmount; x++)
{
cout << ".";
Sleep(SleepingTime);
}
}
VirtualStack::VirtualStack(int Size)
{
WordsAmmount = Size;
cout << "Creating Virtual Stack";LoadingDotsBySleepTime(500,5);
cout << endl << endl;
cout << "Allocating Virtual Bytes"; LoadingDotsBySleepTime(200,4);
cout << endl << endl;
StackBase = new WORD[Size];
SP = &StackBase[WordsAmmount-1];
Sleep(1000);
}
VirtualStack::~VirtualStack()
{
delete[] StackBase;
MessageBox(0,"Stack Destroyed","Stack Information",MB_OK);
}
void VirtualStack::DisplayStack()
{
cout << "Preparing to Draw Scheme Number: " << SchemeNumber; LoadingDotsBySleepTime(400,5);
cout << endl << endl;
cout << "SP register: " << SP << endl << endl;
for(int Obj = (WordsAmmount-1); Obj >= 0; Obj--)
{
cout << "[" << Obj << "] ";
cout << "[" << &StackBase[Obj] << "]" << " Cell Value: " << StackBase[Obj];
if( SP == &StackBase[Obj])
cout << " ---> SP Register Points Here";
cout << endl;
}
cout << endl << endl << endl;
SchemeNumber++;
}
void VirtualStack::ResetStackBytes()
{
for(int Obj = 0; Obj <= (WordsAmmount-1); Obj++)
{
StackBase[Obj]=0;
}
}
void VirtualStack::Push(WORD WordValue)
{
SP--;
*SP = WordValue;
}
void VirtualStack::Pop(WORD &Destination)
{
Destination = *SP;
SP++;
}
void VirtualStack::ChangeStackElementByID(WORD ElementID,WORD ElementValue)
{
StackBase[ElementID] = ElementValue;
}
void VirtualStack::ChangeSpRegisterByElementID(WORD ElementID)
{
SP = &StackBase[ElementID];
}
main.cpp
#include "VirtualStack.h"
int main()
{
VirtualStack VSObject(8);
VSObject.ResetStackBytes();
VSObject.Push(100);
VSObject.Push(50);
VSObject.Push(10);
VSObject.DisplayStack();
VSObject.ChangeSpRegisterByElementID(3);
VSObject.DisplayStack();
VSObject.ChangeStackElementByID(3,90);
VSObject.DisplayStack();
system("PAUSE");
return 0;
}
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! :)