Weird output when printing the result of a comparison

General FreeBASIC programming questions.
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Weird output when printing the result of a comparison

Post by Roland Chastain »

Hello gentlemen!

I have this program (a test program for a library).

Code: Select all

dim as any ptr library = dylibload("mater")

if library = 0 then
  print "Failed to load the dynamic library, aborting program..."
  end 1
end if

dim SolveMate as function ( _
  byval as const zstring ptr, _
  byval as const integer, _
  byval as const integer _
) as zstring ptr

SolveMate = dylibsymbol(library, "SolveMate")

if SolveMate = 0 then
  print "Could not retrieve the function's address, aborting program..."
  end 1
end if

const SEARCH_ALL_MOVES = 1
const SEARCH_CHECK_ONLY = 0

print("b7a8n" = *SolveMate("b7/PP6/8/8/7K/6B1/6N1/4R1bk w KQkq -", 3, SEARCH_ALL_MOVES))
print("h6c1" = *SolveMate("8/8/1p5B/4p3/1p2k1P1/1P3n2/P4PB1/K2R4 w KQkq -", 3, SEARCH_ALL_MOVES))
print("d6c7" = *SolveMate("2N5/8/k2K4/8/p1PB4/P7/8/8 w KQkq -", 4, SEARCH_ALL_MOVES))
print("g5g2" = *SolveMate("rnbK2R1/p6p/p1kNpN1r/P3B1Q1/3P1p1p/5p2/5p1b/8 w KQkq -", 4, SEARCH_ALL_MOVES))
print("h7g7" = *SolveMate("8/1n2P2K/3p2p1/2p3pk/6pr/4ppr1/6p1/1b6 w KQkq -", 3, SEARCH_ALL_MOVES))
print("b7b8r" = *SolveMate("4K1R1/PP2P3/2k5/3pP3/3B4/6P1/8/8 w KQkq -", 3, SEARCH_ALL_MOVES))
print("e7e8b" = *SolveMate("8/2P1P1P1/3PkP2/8/4K3/8/8/8 w Qkq -", 3, SEARCH_ALL_MOVES))
print("h3g5" = *SolveMate("3nn3/2p2p1k/1p1pp1p1/p2B3p/r2B2N1/7N/8/7K w KQkq -", 12, SEARCH_CHECK_ONLY))

dylibfree(library)
Here is the output of the program under Linux:

Code: Select all

-1
-1
-1
-1
-1
-1
-1
-1
And here is the output under Windows:

Code: Select all

-1
              -1
              -1
-1
-1
              -1
              -1
              -1
Hex dump:

00000000 2D 31 0D 0A 20 20 20 20 20 20 20 20 20 20 20 20 -1..............
00000010 20 20 2D 31 0D 0A 20 20 20 20 20 20 20 20 20 20 ..-1............
00000020 20 20 20 20 2D 31 0D 0A 2D 31 0D 0A 2D 31 0D 0A ....-1..-1..-1..
00000030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2D 31 ..............-1
00000040 0D 0A 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ................
00000050 2D 31 0D 0A 20 20 20 20 20 20 20 20 20 20 20 20 -1..............
00000060 20 20 2D 31 0D 0A ..-1..


Any idea of what happens? Why those extra spaces?

I tried with FBC 1.08 and FBC 1.09 (both for 32-bit): same result.

FYI, the program comes from this project: https://gitlab.com/rchastain/mater-library
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Weird output when printing the result of a comparison

Post by SARG »

Check if in SolveMate or in a nested function there would be a print.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Weird output when printing the result of a comparison

Post by srvaldez »

Hello Roland Chastain :)
can't build the library in windows, what version of freePascal are you using ?
or is it Delphi ?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Weird output when printing the result of a comparison

Post by dodicat »

Hello roland.
Win 10

Code: Select all

-1
-1
-1
-1
-1
-1
-1
-1
Press any key to continue . . . 
srvaldez
you need
{$mode delphi}
in mater.pas, materutils.pas, matercore.pas.
matertypes.pas is OK without it.
(I use Geany ide for freepascal, so I need first principles and not any preset Lazarus flags).
( {$mode delphi} only applies in the unit it is used in. )

SORRY Roland, I just noticed 32 bits, I tested 64 bits.
I will test 32 bits later.
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Weird output when printing the result of a comparison

Post by Roland Chastain »

Thank you all for your answers.
srvaldez wrote: Jun 14, 2022 13:12 can't build the library in windows, what version of freePascal are you using ?
I compile with these options:

Code: Select all

fpc -Mobjfpc -Sh mater.pas
You can also use the 'Delphi' mode as dodicat suggested:

Code: Select all

fpc -Mdelphi mater.pas
Otherwise, I use FPC 3.2.0, but I don't think it matters.
dodicat wrote: Jun 14, 2022 15:02 I just noticed 32 bits, I tested 64 bits.
Good to know.
Last edited by Roland Chastain on Jun 14, 2022 15:48, edited 1 time in total.
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Weird output when printing the result of a comparison

Post by Roland Chastain »

SARG wrote: Jun 14, 2022 13:04 Check if in SolveMate or in a nested function there would be a print.
Yes, it could be an explanation, but I don't think there is a print. The problems occurs only with this demo program, not with other.
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Weird output when printing the result of a comparison

Post by SARG »

Another idea :
put the pointed value in a string mystrg=*SolveMate(.....)
then
print "xx";mystrg;"xx"
print ("b7a8n" = mystrg)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Weird output when printing the result of a comparison

Post by srvaldez »

@dodicat, thank you :)
@Roland Chastain, it works ok if using the gcc back-end but fails as you described when using the asm back-end, tested on 32-bit version
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Weird output when printing the result of a comparison

Post by Roland Chastain »

srvaldez wrote: Jun 14, 2022 17:15 @Roland Chastain, it works ok if using the gcc back-end but fails as you described when using the asm back-end, tested on 32-bit version
Thank you for the information. But I don't know how to choose the back-end. Should I reinstall the compiler from another package, or is it a command line option?

P.-S. I used this package. Should I try use this one?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Weird output when printing the result of a comparison

Post by dodicat »

I tested 32 bits now also.
Same problem.
I made LVerbose=True, the -1 then are not spaced.
I set it back to FALSE

puts str(...) also gives weird results in gas
example
...
puts str("e7e8b" = *SolveMate("8/2P1P1P1/3PkP2/8/4K3/8/8/8 w Qkq -", 3, SEARCH_ALL_MOVES))
...

Code: Select all

?‼³
¸'³
ð7³
 8³
P8³
?8³
°8³
à8³ 
puts is OK -gen gcc 32 bits

with fb 1.09
put #cmdline "-gen gcc"
at the top of the code.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Weird output when printing the result of a comparison

Post by srvaldez »

Roland Chastain, that looks like the latest official release
since version 1.09.0 you can put compiler options in the source as dodicat mentioned, put something like this at the top of your source #cmdline "-gen gcc"
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Weird output when printing the result of a comparison

Post by Roland Chastain »

Very interesting informations. Thank you dodicat.
dodicat wrote: Jun 14, 2022 18:29 with fb 1.09
put #cmdline "-gen gcc"
at the top of the code.
I will try tomorrow. (Now, I am at home when there is only Linux.)
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: Weird output when printing the result of a comparison

Post by Roland Chastain »

srvaldez wrote: Jun 14, 2022 18:45 since version 1.09.0 you can put compiler options in the source as dodicat mentioned, put something like this at the top of your source #cmdline "-gen gcc"
OK, I didn't know. Great, thank you!
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Weird output when printing the result of a comparison

Post by SARG »

I tried to reproduce the problem with this code but unsuccessfully :

Code: Select all

function  solve (z as zstring ptr,a as integer,b as integer) as zstring ptr
	static as string zstr
	zstr="b7a8n"
	'print *z
	return strptr(zstr)
End Function

dim SolveMate as function (z as zstring ptr,a as integer,b as integer) as zstring ptr
SolveMate=@solve()

print *SolveMate("aaa",12,15)
print ("b7a8n" = *SolveMate("aaa",12,15))
print ("b7a8n" = *SolveMate("aaa",12,15))
print ("b7a8n" = *SolveMate("aaa",12,15))
print ("b7a8n" = *SolveMate("aaa",12,15))
print ("b7a8n" = *SolveMate("aaa",12,15))
print ("b7a8n" = *SolveMate("aaa",12,15))

sleep
Try on your side.
And if you can please upload the dll and I'll test.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Weird output when printing the result of a comparison

Post by dodicat »

Here is the 32 bit pascal dll
https://www.mediafire.com/file/f3a294yz ... r.zip/file
I am also trying to simulate the error, as yet no luck.
Post Reply