News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Show ESP macro.

Started by hutch--, March 11, 2015, 01:31:09 PM

Previous topic - Next topic

hutch--

I like simple things, this is one of them. Its useful if you are writing procedures with no stack frame to make sure you have not botched it.


    ShowESP MACRO
      IFNDEF espreg
        .data
          espreg dd (0)
        .code
      ENDIF
      mov espreg, esp
      print ustr$(espreg)," = ESP stack address",13,10
    ENDM


In code that you may need to test.


    ShowESP

    YourProc args etc ....

    ShowESP


If they are the same before and after, the stack balances, if not you need to correct the stack.

hutch--

This version does the same thing and allows you to save 4 bytes in the .DATA section.  :biggrin:

    ShowESP MACRO
      IFNDEF esp_reg
        .data?
          esp_reg dd ?
        .code
      ENDIF
      mov esp_reg, esp
      print ustr$(esp_reg)," = ESP stack address",13,10
    ENDM

jj2007

This is the version for exceptionally lazy coders:
UseCheckEsp=1 ; 0=don't generate code
CheckEsp MACRO mode
if UseCheckEsp
  ife mode
.if 1
ifndef EspGlobal
.data?
EspGlobal dd ?
.code
endif
mov EspGlobal, esp
  else
sub EspGlobal, esp
.if !Zero?
pushad
invoke MessageBox, 0, str$(EspGlobal), chr$("Stack is off by some bytes:"), MB_OK
popad
.endif
.endif
  endif
endif
ENDM


...

  CheckEsp 0
  push 1
  CheckEsp 1

;)

rrr314159

Not being lazy yourself, jj2007, u don't understand lazy programmers! *This* is the version for us:

.data
    lazyMsg db "Stack was off but don't worry, I took care of it. Go back to sleep.",0
.code

UseCheckEsp=1 ; 0=don't generate code
CheckEsp MACRO mode
if UseCheckEsp
  ife mode
ifndef EspGlobal
.data?
EspGlobal dd ?
.code
endif
mov EspGlobal, esp
  else
sub EspGlobal, esp
.if !Zero?
            add esp, EspGlobal
            pushad
            invoke MessageBox, 0, str$(EspGlobal), addr lazyMsg, MB_OK
            popad
.endif
  endif
endif
ENDM


Even better, comment out the MessageBox invocation - lazy proggers hate to be forced to click "OK".

Of course if you have to write the CheckEsp macro yourself, this is the preferred version:

;No Code at all

Then when your stack is off, a helpful message will automatically pop up (assuming you're too lazy to have a debugger):

"lazy.exe has stopped working"

Simply click on this choice:

"Check online for a solution and close the program"

and all your data will be sent to Microsoft. Let them figure out what's wrong  8)
I am NaN ;)

jj2007

Quote from: rrr314159 on March 12, 2015, 03:15:58 AM"Check online for a solution and close the program"

IIRC the URL they are using for the check is http://blackhole.microsoft.com :biggrin:

dedndave

i'm sorry, but i can't believe you guys need a macro for that   :lol:

jj2007

Quote from: dedndave on March 12, 2015, 05:27:27 AM
i'm sorry, but i can't believe you guys need a macro for that   :lol:

Honestly, if you have a fat source, then UseCheckEsp=1  ; 0=don't generate code can be handy. One reason being that with a stack frame, you won't notice that your stack is unbalanced, but your xxx proc uses esi edi ebx doesn't work as expected - and that bug is really difficult to chase.

Antariy

We used something like that in the thread with prologue/epologue macroses which do the stack probing, the vars zeroing, the stack balance checking - all is selectable if is required.