• Welcome to Powerbasic Museum 2020-B.
 

Swapping LONG Variables with and without REGISTER

Started by Theo Gottwald, January 02, 2010, 04:50:54 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Theo Gottwald

Today I had a reason to get my DisASM running again.
Using this Powerbasic code:

REGISTER R01 AS LONG, R02 AS LONG
SWAP R01,R02


will result in this ASM Statement (as expected):

4024ED  87F7                   XCHG ESI, EDI

while beeing lazy and leaving the REGISTER away, will cost you some cycles:

4024EB  8B8564FFFFFF           MOV EAX, DWORD PTR [EBP+FFFFFF64]
4024F1  878568FFFFFF           XCHG EAX, DWORD PTR [EBP+FFFFFF68]
4024F7  898564FFFFFF           MOV DWORD PTR [EBP+FFFFFF64], EAX

                                                         
In this case, we see that Bob wasn't lazy. We get perfectly optimized code using SWAP. And we can speed things up a bit more, using REGISTER Variables.

Steve Hutchesson

Theo,

How about a simpler way again.


    push var1
    push var2
    pop var1
    pop var2


No registers used and passably fast. XCHG is small but you would not use it where speed matters, for example in a sort where you are swapping pointers.

Theo Gottwald

Stack is in Memory. The only advantage seems to me, not to use Registers.
Anyway, this was just something I recognized while using DisASM.

I'd always prefer the normal PB Swap Command before trying own constructs on Variables.
This may work on some datatypes, but maybe on some it may fail.

I have seen that many people often do not use the power of REGISTER Variables.
Thats why i made this post. I use them often.