PureBasic Forum
https://www.purebasic.fr/english/

Confusion stack
https://www.purebasic.fr/english/viewtopic.php?f=35&t=62279
Page 1 of 1

Author:  User_Russian [ Wed May 20, 2015 5:43 pm ]
Post subject:  Confusion stack

Why is the procedure returns 2, instead of 1234?
Code:
EnableASM

ss:

Procedure Tst(x)
  PUSH dword 2
  MOV eax,l_ss
  PUSH eax
  PUSH x
  POP eax
  ADD esp,8
  ProcedureReturn
EndProcedure

Debug Tst(1234)

Author:  Danilo [ Wed May 20, 2015 7:07 pm ]
Post subject:  Re: Confusion stack

You manipulate the stack with your PUSHes, so PB gets confused. "PUSH x" becomes "PUSH dword [esp+PS0+0]".
That means, PB expects 'x' at a certain position on the stack. It does not recognize your stack manipulations, so
after the PUSHes it loads the wrong value for 'x'.
Code:
EnableASM

ss:
DisableDebugger
    Procedure Tst(x)
      PUSH dword 2
      MOV eax,l_ss
      PUSH eax
     
      ;PUSH x ; becomes PUSH dword [esp+4]
      PUSH dword [esp+12]
     
      POP eax
      ADD esp,8
      ProcedureReturn
    EndProcedure
EnableDebugger

x = Tst(1234)
Debug x

Author:  Tristano [ Fri May 05, 2017 10:15 pm ]
Post subject:  Re: Confusion stack

This issue of PB variables and the stack pointer seems to popup a lot --- other posts dealing with this issue:

Thanks for your added comments to @User_Russian's code, @Danilo! they make understanding the issue easier.

The other posts offer some examples of workarounds to the problem.

If I've undestood correctly, EBP can't be used to store ESP (as customary) because it's being used by PureBASIC internally (or the debugger)?

It would be nice to have some reference documentation explaining in detail (or in summary) this topic --- more generally, what are the limitations in using ASM inside PB code.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/