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

Just starting out? Need help? Post your questions and find answers here.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

using the FPU to swap addresses is really a bad bad idea.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

integer numbers above 2^32 will be rounded, so on 64-bit it can badly fail with high range addresses.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

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

Post 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
Keep it BASIC.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

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

Post 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! :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Harry0
User
User
Posts: 13
Joined: Sun Mar 02, 2008 4:28 pm
Location: Palatine

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

Post 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!
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

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

Post 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())
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

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

Post 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'.
ImageThe happiness is a road...
Not a destination
Post Reply