Hola :D
este es mi primer code hecho en C++ :3
me base en una pregunta que se hizo hace poco en el foro y me dio ganas de hacer el codigo, mi humilde codigo
es una simulacion de como trabaja la pila en asm :3 mi codigo soporta desplazamientos de punteros, en asm seria
de los registros ESP, EBP, para que apunten a diferentes lugares de la pila tambien tiene el registro EBP.
tambien pido a los grandes en c++ consejos, recomendaciones, todo lo que sea para poder mejorar ;D
// .MODEL 386,486,586 xD
// .STACK 100h // Size Segment STACK
class ClsSTACK
{
public:
ClsSTACK():ClsESP(&SegSTACK[0]),ClsEBP(0) {} // Inicialize Value
void ClsPUSH(int Reg); // Instruction PUSH Operator
int ClsPOP(); // Instruction POP Operator
int SegSTACK[0x100]; // Segment STACK
int* ClsESP; // STACK POINTER
int* ClsEBP; // BASE POINTER
};
void ClsSTACK::ClsPUSH(int Reg)
{
*ClsESP = Reg;
ClsESP++;
}
int ClsSTACK::ClsPOP()
{
return *ClsESP--;
}
// .DATA
// ValS DWORD 0
// .CODE
// inicio:
// MOV EDX,@DATA
// MOV DS,EDX
int main()
{
ClsSTACK MSTACK; //Make STACK
int ValS = 0;
MSTACK.ClsPUSH(0x10);
MSTACK.ClsPUSH(0x0);
MSTACK.ClsPUSH(0x0);
MSTACK.ClsPUSH(0x10);
MSTACK.ClsPUSH(0x100);
MSTACK.ClsPUSH(0x200);
MSTACK.ClsESP--; // ADD ESP,04h
ValS = MSTACK.ClsPOP(); // POP EAX
// MOV DS:ValS,EAX ; ValS = 100h
// Test BASE POINTER
MSTACK.ClsEBP = MSTACK.ClsESP; // MOV EBP,ESP
MSTACK.ClsEBP = MSTACK.ClsEBP-4; // ADD EBP,8h
ValS = *MSTACK.ClsEBP; // MOV EAX,EBP
ValS = MSTACK.ClsPOP(); // POP EAX ; Not Change
return 0;
// end inicio
}
;D
int ClsSTACK::ClsPOP()
{
return *ClsESP;
ClsESP--;
}
El return debería ir después del decremento, no?
Saludos
Hey :xD gracias
de hecho debe ser asi:
int ClsSTACK::ClsPOP()
{
return *ClsESP--;
}
si lo escribo antes el ESP devolvera un DWORD incorrecta al que se espera ;D
void ClsSTACK::ClsPUSH(int Reg)
{
ClsESP++;
*ClsESP = Reg;
}
El incremento al parecer debe ir despues de la asignacion...
void ClsSTACK::ClsPUSH(int Reg)
{
*ClsESP = Reg;
ClsESP++;
}
tambien puedes hacerlo asi:
void ClsSTACK::ClsPUSH(int Reg)
{
*(ClsESP++) = Reg;
}
@RHL
yo lo haria algo asi... aun ya existe una libreria en c++ para esto...
#include <cstring>
class CStack {
private:
unsigned m_uSize;
int *m_puSTACK; // Segment STACK
unsigned m_uESP; // STACK POINTER
unsigned m_uEBP; // BASE POINTER
public:
CStack(unsigned size); // Inicialize Value
virtual ~CStack();
void push(int iReg); // Instruction PUSH Operator
int pop(); // Instruction POP Operator
bool isFull();
unsigned ESP();
unsigned ESP(unsigned ESP);
unsigned EBP();
unsigned EBP(unsigned EBP);
};
CStack::CStack(unsigned size) : m_uESP(NULL), m_uEBP(0) {
m_puSTACK = new int[m_uSize = size];
memset(m_puSTACK, 0, m_uSize * sizeof(int));
}
CStack::~CStack() {
delete m_puSTACK;
}
unsigned CStack::ESP() {
return m_uESP;
}
unsigned CStack::ESP(unsigned ESP) {
return m_uESP = ESP;
}
unsigned CStack::EBP() {
return m_uEBP;
}
unsigned CStack::EBP(unsigned EBP) {
if (EBP > m_uSize)
EBP = m_uSize;
return m_uEBP = EBP;
}
bool CStack::isFull(){
return m_uSize == m_uESP ? true:false;
}
void CStack::push(int iReg) {
if (m_uESP > m_uSize) return;
m_puSTACK[m_uESP++] = iReg;
}
int CStack::pop(){
int iRet;
if (m_uESP == m_uEBP)
return m_puSTACK[m_uEBP];
iRet = m_puSTACK[m_uESP - 1];
m_puSTACK[--m_uESP] = 0;
return iRet;
}
#include <iostream>
#include "include/aa.h"
using namespace std;
int main()
{
CStack oStack(12);
oStack.push(45);
oStack.push(46);
oStack.push(47);
oStack.push(48);
cout << oStack.pop() << endl;
oStack.push(49);
cout << oStack.pop() << endl;
cout << oStack.isFull() << endl;
cout << oStack.ESP() << endl;
oStack.ESP(10);
cout << oStack.isFull() << endl;
cout << oStack.ESP() << endl;
while (oStack.ESP())
cout << oStack.pop() << endl;
return 0;
}
Dulces Lunas!¡.