Author Topic: Swapping LONG Variables with and without REGISTER  (Read 5291 times)

0 Members and 1 Guest are viewing this topic.

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 929
  • User-Rate: +30/-4
    • it-berater
Swapping LONG Variables with and without REGISTER
« on: January 02, 2010, 04:50:54 PM »
Today I had a reason to get my DisASM running again.
Using this Powerbasic code:

Code: [Select]
REGISTER R01 AS LONG, R02 AS LONG
SWAP R01,R02

will result in this ASM Statement (as expected):

Code: [Select]
4024ED  87F7                   XCHG ESI, EDI
while beeing lazy and leaving the REGISTER away, will cost you some cycles:

Code: [Select]
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.
« Last Edit: January 02, 2010, 04:55:08 PM by Theo Gottwald »

Offline Steve Hutchesson

  • Jr. Member
  • **
  • Posts: 83
  • User-Rate: +6/-5
    • The MASM Forum
Re: Swapping LONG Variables with and without REGISTER
« Reply #1 on: January 03, 2010, 09:42:27 AM »
Theo,

How about a simpler way again.

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

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 929
  • User-Rate: +30/-4
    • it-berater
Re: Swapping LONG Variables with and without REGISTER
« Reply #2 on: February 05, 2010, 09:41:27 AM »
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.