Re: ESP (stack) question (using HLA)???



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

.



Relevant Pages

  • Re: Which assembler can handle the BIG stuff ?
    ... btw, that this was done by a *macro*, not extending HLA's code via some ... // push(eax); ... // on the top of the 80x86 stack. ...
    (alt.lang.asm)
  • Re: Standard NASM Macros
    ... This is where the macro implementation of HLL-like constructs starts to ... // The statement above computes true or false in EAX and the ... // on the top of the 80x86 stack. ... mul((type dword [esp])); ...
    (alt.lang.asm)
  • Re: uncertain: x87 or SSE2
    ... FPU control word, and clear the stack on return, unless you're ... needed almost as often as 'eax' and 'edx'). ... push ebp; mov ebp, esp ... mov eax, ...
    (alt.lang.asm)
  • Re: [RosAsm example]
    ... mox eax 0 ... | of "good Programming Practices". ... L0: push 00333333h; bg color ... In fact, the Stack memory is ...
    (alt.lang.asm)
  • C calling conventions
    ... i.e although it may use those registers, ... the contents all other G.P registers may be altered at will(in linux ... A procedures return value is returned in EAX. ... bar pushed onto the stack second.foo pushed onto the stack last. ...
    (comp.lang.c)