Page 2 of 2

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Nov 06, 2013 10:23 am
by Psychophanta
Since this "bug" in native PB does not allow to perform this, here it is a definitive solution valid for Intel x68 and Intel x86 64bit uP ina any language that allows assembler:

Code: Select all

Macro mySwap(a,b); 32bit & 64bit x86
  !fld qword[a_#a#]
  !fld qword[a_#b#]
  !fstp qword[a_#a#]
  !fstp qword[a_#b#]
EndMacro
Source example for PB:

Code: Select all

Macro mySwap(a,b); 32bit & 64bit x86
  !fld qword[a_#a#]
  !fld qword[a_#b#]
  !fstp qword[a_#a#]
  !fstp qword[a_#b#]
EndMacro
Dim tableau1(5):Dim tableau2(5)
For i=0 To 5
  tableau1(i)=i
  tableau2(i)=i+10
Next
For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next
Debug "Swap it:"
mySwap(tableau1,tableau2)
For i=0 To 5
  Debug tableau1(i)
  Debug tableau2(i)
Next

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Nov 06, 2013 11:26 am
by Fred
using the FPU to swap addresses is really a bad bad idea.

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Nov 06, 2013 12:44 pm
by Psychophanta
Fred wrote:using the FPU to swap addresses is really a bad bad idea.
Can you explain, in few words, why?
EDIT: arguments like: "The FPU is not intended for that kind of tasks" are not valid.

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Nov 06, 2013 1:03 pm
by Fred
integer numbers above 2^32 will be rounded, so on 64-bit it can badly fail with high range addresses.

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Nov 06, 2013 5:52 pm
by Psychophanta
Sorry, but I have to say that all my tests work like a charm.
If you write here a short tip showing the bad idea of using FPU for this, it would be a good help for "Coding questions" readers.

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Nov 06, 2013 10:08 pm
by heartbone
I'm thinking that Doctor Whom would ask everyone to consider any ramifications into the fifth dimension.
Luckily there are effective alternatives to gain. :D

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Nov 06, 2013 10:52 pm
by Psychophanta
@heartbone, you are a human, so you also sometimes make mistakes.
Fred had a erratum while writting. He just wanted to say:
using the FPU to swap addresses is really a good good idea.
That's all! :)

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Wed Dec 11, 2013 6:57 pm
by Harry0
I apologize ahead of time as I know this thread is old but I have two items, one that might help and the second item are some questions:

Here is my suggestion to do this using 64 bit registers and not the FPU:

Code: Select all

Macro I_Swap(a,b) ; New Macro
  !mov r8 ,qword[a_#a]
  !mov r9 ,qword[a_#b]
  !mov [a_#a], r9
  !mov [a_#b], r8  
EndMacro

Macro mySwap(a,b); 32bit & 64bit x86
  !fld qword[a_#a#]
  !fld qword[a_#b#]
  !fstp qword[a_#a#]
  !fstp qword[a_#b#]
EndMacro

A_Size.i = 200  ; Be able to consistently change the size of the array

Dim tableau1(A_Size):Dim tableau2(A_Size)

For i=0 To A_Size
  tableau1(i)= 10000000 + 10 + i
  tableau2(i)= 10000000 + 100 + i
Next
For i=0 To A_Size
  Debug tableau1(i)
  Debug tableau2(i)
Next
Debug "Swap it:"
;mySwap(tableau1,tableau2)
I_Swap(tableau1,tableau2) ; New Macro

For i=0 To A_Size
  Debug tableau1(i)
  Debug tableau2(i)
Next
I hope this provides a better solution by not using the FPU .

Now the questions:

In working with the above code, I now find that you can work with arrays in assembler (have not been able to find any documentation about it anywhere).

It also seems that the code in the macro will walk through the whole array in one pass/command/macro.

Is there any more documentation about this, any place?
Is there any more documentation about how to access other types, like structures, in ASM?

Thanks!

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Thu Oct 06, 2016 6:21 pm
by Lunasole
Hah, I guess using FPU for pointers can make some sense, at least as one of small things making your code harder to understand on disassembling ^^
Thanks for that stuff Psychophanta, going to use it when there will be a case for it.

UPD: unfortunatelly macro won't work inside procedure, which greatly decreases usability of this stuff.

Code: Select all

EnableExplicit

Structure DAT 
	SomeYourData1$
	SomeYourData2$
	SortField$		; and this is for sorting
EndStructure

; create random array
Dim Test.DAT (10)
Dim Test2.DAT (4)

Macro mySwap(a,b); 32bit & 64bit x86
  !fld qword[a_#a#]
  !fld qword[a_#b#]
  !fstp qword[a_#a#]
  !fstp qword[a_#b#]
EndMacro

; swap arrays
mySwap(Test, Test2)

Procedure XTest(Array A1.DAT(1), Array A2.DAT(1))
	mySwap(A1, A2)
EndProcedure

; swap them once more inside procedure
; macro doesn't work :/
XTest(Test(), Test2())

; show the difference
Debug ArraySize(Test())
Debug ArraySize(Test2())

Re: PB5.20 bug: 'Swap' does not swap pointers

Posted: Mon Oct 10, 2016 10:27 am
by Kwai chang caine
Hello LUNASOLE :D
Just for information :wink:
I have this error on your code W7 X86 v5.40
Debugger wrote:PureBasic.asm [172]:
fld qword[a_A1]
error: undefined symbol 'a_A1'.