Re: ESP (stack) question (using HLA)???
- From: "kain" <spamtrap@xxxxxxxxxx>
- Date: Thu, 27 Oct 2005 23:46:11 +0000 (UTC)
2 things:
1. HLA is 32 bit only. Change your variables to 32 bits.
2. push *subtracts* esp, so load ESP with address *after* the end of
the buffer.
// sample
program minstack;
#include ("stdlib.hhf")
static
Wdeg: uns32 := 0;
rstack: uns32[2];
tesp: dword;
endstatic;
begin minstack;
mov (esp, tesp);
lea (esp, rstack [2*4]);
pushd (10);
pop (Wdeg);
mov (tesp, esp);
stdout.put ("Wdeg = ", Wdeg,nl);
stdout.put ("contents of stack = :",
rstack[0*4], " ",
rstack[1*4],nl);
end minstack;
I also wrote an HLA OOP stack class recently if you are interested.
Just keep in mind that all elements are dword-size.
The code and description for that follows:
Procedures and Methods
tStack.create ( size:dword );
Creates the stack with a maximum of 'size' elements.
tStack.destroy();
Call to free up memory associated with this object
tStack.spush ( obj:dword );
Push obj onto the stack. Returns 0 on overflow
tStack.spop();
Pops the last object pushed onto the stack and returns
in EAX. Returns 0 on stack underflow.
tStack.getsize();
Returns the size with which the object was created.
tStack.getnumobjects();
Returns the number of elements currently on the stack.
//=========================================================
program twerp;
#include ("stdlib.hhf")
type
tStack: class
var
_private__size :uns32;
_private__bos :dword; /* bottom of stack */
_private__stp :dword; /* stack pointer */
_private__pMem :dword; /* memory of dword
objects */
endvar;
// public methods/procs
procedure create ( size:dword); @returns("ESI");
method destroy;
method spush (obj:dword); @returns ("EAX");
method spop; @returns ("EAX");
method getsize; @returns ("EAX");
method getnumobjects; @returns ("EAX");
endclass;
endtype;
readonly
tStackVMT :VMT(tStack);
endreadonly;
procedure tStack.create (size: dword);
begin create;
push (eax); push (ecx);
if ( ! ESI) then
mem.alloc (@size(tStack));
mov (eax, esi);
endif;
mov (&tStack._VMT_, this._pVMT_);
mov (size, eax);
mov (eax, this._private__size);
shl (2, eax);
mov (eax, ecx);
mov ( mem.alloc (eax), this._private__pMem);
add (ecx, eax);
mov (eax, this._private__bos);
mov (eax, this._private__stp);
pop (ecx); pop (eax);
end create;
method tStack.destroy; @nodisplay; @noframe;
begin destroy;
push (eax);
mem.free (this._private__pMem);
if (mem.isInHeap(esi)) then
mem.free (esi);
endif;
pop (eax);
ret();
end destroy;
method tStack.getsize; @nodisplay; @noframe;
begin getsize;
mov (this._private__size, eax);
ret();
end getsize;
method tStack.getnumobjects; @nodisplay; @noframe;
begin getnumobjects;
push (edx);
mov (this._private__stp, edx);
sub (this._private__pMem, edx);
mov (this._private__bos, eax);
sub (this._private__pMem, eax);
sub (edx, eax);
pop (edx);
shr (2, eax);
ret();
end getnumobjects;
method tStack.spush (obj:dword);
// returns 0 on failure
// returns possitive on success
begin spush;
mov (this._private__stp, eax);
if ( eax = this._private__pMem) then
// error stack overflow
xor (eax, eax);
else
sub (4, eax);
mov (eax, this._private__stp);
mov (obj, [eax]);
endif;
end spush;
method tStack.spop;
// returns 0 on failure
// returns object on success
begin spop;
mov (this._private__stp, eax);
if (eax = this._private__bos) then
// error stack underflow
xor (eax, eax);
else
mov ([eax], eax);
add (4, this._private__stp);
endif;
end spop;
var
stk :tStack;
endvar;
begin twerp;
stk.create( 10 );
stk.spush (100);
stk.spush (200);
stk.spush (300);
stk.getsize();
stdout.put ( "Max number of objects in this stack : ", (type
uns32
eax), nl );
stk.getnumobjects();
stdout.put ( "Number of objects currently on stack: ", (type
uns32
eax), nl nl);
stk.spop();
stdout.put ("pop : ", (type uns32 eax),nl);
stk.spop();
stdout.put ("pop : ", (type uns32 eax),nl);
stk.spop();
stdout.put ("pop : ", (type uns32 eax),nl);
stk.spop();
stdout.put ("pop : ", (type uns32 eax),nl);
stk.destroy();
end twerp;
// end
-sevag.K.
www.geocities.com/kahlinor
.
- Follow-Ups:
- Re: ESP (stack) question (using HLA)???
- From: kain
- Re: ESP (stack) question (using HLA)???
- References:
- ESP (stack) question (using HLA)???
- From: spamtrap
- ESP (stack) question (using HLA)???
- Prev by Date: Re: Combining two MMX registers into one SSE register?
- Next by Date: Re: ESP (stack) question (using HLA)???
- Previous by thread: ESP (stack) question (using HLA)???
- Next by thread: Re: ESP (stack) question (using HLA)???
- Index(es):
Relevant Pages
|
|