It is currently Thu Dec 05, 2019 10:46 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Confusion stack
PostPosted: Wed May 20, 2015 5:43 pm 
Offline
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1056
Location: Russia
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)

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: Confusion stack
PostPosted: Wed May 20, 2015 7:07 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2942
Location: Planet Earth
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Confusion stack
PostPosted: Fri May 05, 2017 10:15 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Nov 26, 2015 6:52 pm
Posts: 154
Location: Italy
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.

_________________
The PureBASIC Archives:
FOSS Resources:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye