elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Tutorial básico de Quickjs


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  [SRC] STACK for C++
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [SRC] STACK for C++  (Leído 3,345 veces)
x64core


Desconectado Desconectado

Mensajes: 1.908


Ver Perfil
[SRC] STACK for C++
« en: 25 Noviembre 2011, 02:51 am »

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

Código
  1. // .MODEL 386,486,586 xD
  2. // .STACK 100h // Size Segment STACK
  3. class ClsSTACK
  4. {
  5. public:
  6. ClsSTACK():ClsESP(&SegSTACK[0]),ClsEBP(0) {} // Inicialize Value
  7. void ClsPUSH(int Reg); // Instruction PUSH Operator
  8. int ClsPOP(); // Instruction POP Operator
  9. int SegSTACK[0x100]; // Segment STACK
  10. int* ClsESP; // STACK POINTER
  11. int* ClsEBP; // BASE POINTER
  12. };
  13.  
  14. void ClsSTACK::ClsPUSH(int Reg)
  15. {
  16. *ClsESP = Reg;
  17. ClsESP++;
  18. }
  19.  
  20. int ClsSTACK::ClsPOP()
  21. {
  22. return *ClsESP--;
  23. }
  24.  
  25. // .DATA
  26. // ValS DWORD 0
  27. // .CODE
  28. // inicio:
  29. // MOV EDX,@DATA
  30. // MOV DS,EDX
  31. int main()
  32. {
  33. ClsSTACK MSTACK; //Make STACK
  34. int ValS = 0;
  35.  
  36. MSTACK.ClsPUSH(0x10);
  37. MSTACK.ClsPUSH(0x0);
  38. MSTACK.ClsPUSH(0x0);
  39. MSTACK.ClsPUSH(0x10);
  40. MSTACK.ClsPUSH(0x100);
  41. MSTACK.ClsPUSH(0x200);
  42. MSTACK.ClsESP--; // ADD ESP,04h
  43. ValS = MSTACK.ClsPOP(); // POP EAX
  44. // MOV DS:ValS,EAX ; ValS = 100h
  45. // Test BASE POINTER
  46. MSTACK.ClsEBP = MSTACK.ClsESP;        // MOV EBP,ESP
  47. MSTACK.ClsEBP = MSTACK.ClsEBP-4; // ADD EBP,8h
  48. ValS = *MSTACK.ClsEBP; // MOV EAX,EBP
  49. ValS = MSTACK.ClsPOP(); // POP EAX ; Not Change
  50.  
  51. return 0;
  52. // end inicio
  53. }

;D


« Última modificación: 25 Noviembre 2011, 19:10 pm por RHL » En línea

RyogiShiki


Desconectado Desconectado

Mensajes: 745


げんしけん - Hikkikomori FTW!!!


Ver Perfil WWW
Re: [SRC] STACK for C++
« Respuesta #1 en: 25 Noviembre 2011, 03:09 am »

Código
  1. int ClsSTACK::ClsPOP()
  2. {
  3. return *ClsESP;
  4. ClsESP--;
  5. }

El return debería ir después del decremento, no?

Saludos


En línea

x64core


Desconectado Desconectado

Mensajes: 1.908


Ver Perfil
Re: [SRC] STACK for C++
« Respuesta #2 en: 25 Noviembre 2011, 03:33 am »

Hey :xD gracias
de hecho debe ser asi:

Código
  1. int ClsSTACK::ClsPOP()
  2. {
  3. return *ClsESP--;
  4. }

si lo escribo antes el ESP devolvera un DWORD incorrecta al que se espera  ;D
En línea

BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: [SRC] STACK for C++
« Respuesta #3 en: 25 Noviembre 2011, 07:24 am »

Código
  1.  
  2. void ClsSTACK::ClsPUSH(int Reg)
  3. {
  4. ClsESP++;
  5. *ClsESP = Reg;
  6. }
  7.  
  8.  

El incremento al parecer debe ir despues de la asignacion...

Código
  1.  
  2. void ClsSTACK::ClsPUSH(int Reg)
  3. {
  4. *ClsESP = Reg;
  5. ClsESP++;
  6. }
  7.  
  8.  

tambien puedes hacerlo asi:

Código
  1.  
  2. void ClsSTACK::ClsPUSH(int Reg)
  3. {
  4. *(ClsESP++) = Reg;
  5. }
  6.  
  7.  
En línea

The Dark Shadow is my passion.
x64core


Desconectado Desconectado

Mensajes: 1.908


Ver Perfil
Re: [SRC] STACK for C++
« Respuesta #4 en: 25 Noviembre 2011, 07:41 am »

 sabia lo de parentesis :3 guardado , gracias BlackZeroX
« Última modificación: 25 Noviembre 2011, 07:48 am por RHL » En línea

BlackZeroX
Wiki

Desconectado Desconectado

Mensajes: 3.158


I'Love...!¡.


Ver Perfil WWW
Re: [SRC] STACK for C++
« Respuesta #5 en: 25 Noviembre 2011, 08:37 am »

@RHL
yo lo haria algo asi... aun ya existe una libreria en c++ para esto...

Código:

#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;
}


Código
  1.  
  2. #include <iostream>
  3. #include "include/aa.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. CStack oStack(12);
  10.  
  11. oStack.push(45);
  12. oStack.push(46);
  13. oStack.push(47);
  14. oStack.push(48);
  15. cout << oStack.pop() << endl;
  16. oStack.push(49);
  17. cout << oStack.pop() << endl;
  18. cout << oStack.isFull() << endl;
  19. cout << oStack.ESP() << endl;
  20. oStack.ESP(10);
  21. cout << oStack.isFull() << endl;
  22. cout << oStack.ESP() << endl;
  23. while (oStack.ESP())
  24.        cout << oStack.pop() << endl;
  25. return 0;
  26. }
  27.  
  28.  

Dulces Lunas!¡.
En línea

The Dark Shadow is my passion.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Ayuda con la clase Stack T_T
Java
.Maku. 4 5,104 Último mensaje 11 Mayo 2009, 02:01 am
por AlbertoBSD
Ayuda con la Pila (Stack)
ASM
isidora 6 5,813 Último mensaje 3 Diciembre 2011, 03:33 am
por Иōҳ
Implementar Stack Trace (Walk through stack)
ASM
kub0x 5 3,723 Último mensaje 16 Marzo 2014, 19:21 pm
por Arkangel_0x7C5
Programador Full-Stack
Dudas Generales
behindfa21 1 1,260 Último mensaje 30 Enero 2024, 21:27 pm
por Jonwarner
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines