It is currently Thu Jul 02, 2020 7:33 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Preserving EBX register
PostPosted: Sun Apr 26, 2015 5:15 pm 
Offline
PureBasic Fanatic
PureBasic Fanatic

Joined: Fri Dec 09, 2005 12:15 pm
Posts: 2236
Location: Elbonia
I've got this procedure where I need an extra register besides eax, ecx and edx. ebx seems to be good for the task, but I can't seem to be able to preserve it. For example consider the following code:

Code:
Procedure myproc(bbb.l)

    CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
        !push rbx
    CompilerElse
        !push ebx
    CompilerEndIf

    ;bbb + 1
    !mov ebx, dword [p.v_bbb]
    !inc ebx
    !mov dword [p.v_bbb], ebx

    CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
        !pop rbx
    CompilerElse
        !pop ebx
    CompilerEndIf 

    ProcedureReturn bbb

EndProcedure

n = myproc(5) ;should return 6
Debug n


If the push and pop parts are commented, the result comes out ok. What is the safe way to use ebx?

_________________
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)


Top
 Profile  
Reply with quote  
 Post subject: Re: Preserving EBX register
PostPosted: Sun Apr 26, 2015 5:31 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3614
Location: Netherlands
When you alter the stack with pushing and popping registers, that messes up references to local variables like [p.v_bbb]
What I'm currently using is an integer variable to store the register. This works fine.
The same way you can preserve rsi/esi and rdi/edi if you need even more registers.
Using rbp/ebp as an extra register causes problems with the debugger.
Code:
Procedure myproc(bbb.l)
 
  Protected.i reg_bx
 
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov [p.v_reg_bx], rbx
  CompilerElse
    !mov [p.v_reg_bx], ebx
  CompilerEndIf
 
  ;bbb + 1
  !mov ebx, dword [p.v_bbb]
  !inc ebx
  !mov dword [p.v_bbb], ebx
 
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov rbx, [p.v_reg_bx]
  CompilerElse
    !mov ebx, [p.v_reg_bx]
  CompilerEndIf
 
  ProcedureReturn bbb
 
EndProcedure

n = myproc(5) ;should return 6
Debug n

_________________
macOS 10.15 Catalina, Windows 10


Top
 Profile  
Reply with quote  
 Post subject: Re: Preserving EBX register
PostPosted: Sun Apr 26, 2015 5:35 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Nov 09, 2010 10:15 pm
Posts: 1669
Pb uses the stack for local variables. Since it doesn't use a frame pointer, any stack changes you make will kill the reference point.

This was a design decision the developer made early on, to allow more registers for optimizing, but at the cost of capabilities like this (and many other standard practices).


Top
 Profile  
Reply with quote  
 Post subject: Re: Preserving EBX register
PostPosted: Sun Apr 26, 2015 6:33 pm 
Offline
PureBasic Fanatic
PureBasic Fanatic

Joined: Fri Dec 09, 2005 12:15 pm
Posts: 2236
Location: Elbonia
Thank you both for the explanation, it's perfectly clear now. I'll save the register in a variable as advised. Still got many things to learn :mrgreen:.

_________________
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 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