Author Topic: Show ESP macro.  (Read 3996 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8755
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Show ESP macro.
« on: March 11, 2015, 01:31:09 PM »
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 at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8755
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Show ESP macro.
« Reply #1 on: March 11, 2015, 11:08:01 PM »
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
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 11783
  • Assembler is fun ;-)
    • MasmBasic
Re: Show ESP macro.
« Reply #2 on: March 12, 2015, 02:19:43 AM »
This is the version for exceptionally lazy coders:
Code: [Select]
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

...

Code: [Select]
  CheckEsp 0
  push 1
  CheckEsp 1
;)

rrr314159

  • Member
  • *****
  • Posts: 1382
Re: Show ESP macro.
« Reply #3 on: March 12, 2015, 03:15:58 AM »
Not being lazy yourself, jj2007, u don't understand lazy programmers! *This* is the version for us:

Code: [Select]
.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:

Code: [Select]
;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

  • Member
  • *****
  • Posts: 11783
  • Assembler is fun ;-)
    • MasmBasic
Re: Show ESP macro.
« Reply #4 on: March 12, 2015, 03:40:30 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

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Show ESP macro.
« Reply #5 on: March 12, 2015, 05:27:27 AM »
i'm sorry, but i can't believe you guys need a macro for that   :lol:

jj2007

  • Member
  • *****
  • Posts: 11783
  • Assembler is fun ;-)
    • MasmBasic
Re: Show ESP macro.
« Reply #6 on: March 12, 2015, 12:33:46 PM »
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

  • Member
  • ****
  • Posts: 557
Re: Show ESP macro.
« Reply #7 on: March 13, 2015, 07:49:00 AM »
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.