Compiling errors #3

New to FreeBASIC? Post your questions here.
Post Reply
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Compiling errors #3

Post by Juergen Kuehlwein »

Hi all,

this code compiles and works in 32 bit,

Code: Select all

common shared x as uinteger


function test as long
'***********************************************************************************************
'
'***********************************************************************************************

  asm push &H1
  asm pop [x]


  function = x


end function


test
print str(x)
sleep

but in 64 bit i get this:

D:\Users\Administrator\Desktop\Freebasic\Test.c:26:2: error: 'asm' operand has impossible constraints

__asm__ __volatile__( "POP [%0]" : "+m" (X$) : : "cc", "memory", "rax", "rbx", "rcx", "rdx", "rsp", "rdi", "rsi", "r8", "r9", "r10", "r11", "r12", "r13", "r14",
"r15" );
^
compiling C failed: 'D:\FreeBasic\FB64\bin\win64\gcc.exe' terminated with exit code 1

I want to fill the global variable x (uinteger) with a value (uinteger) currently on the stack, 64 bit allows for 64 bit pushes and pops - so what is wrong?


Thanks,


JK
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Compiling errors #3

Post by MrSwiss »

Maybe, variable (64 bit) to register; i.e. RAX then push/pop (reg) ...
The C compiler (g++) is more 'finnicki' than the GAS backend, when it comes to ASM!

Code: Select all

ASM
	xor rax, rax
	mov rax, byte ptr [&h01]
	push rax
	pop rax
	mov qword ptr [x], rax
End ASM
And, if dealing with 64 bit exclusively, why not ULongInt (fixed size 64 bit, FB type).
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling errors #3

Post by Juergen Kuehlwein »

Thanks for your reply!

The problem seems to be related to "Common", if replaced by "Dim" (Dim Shared) or if i make it a local variable, it works, but in this case it must be a global variable shared between modules and it must be in assembler, "Extern" doesn´t help either.


JK
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Compiling errors #3

Post by srvaldez »

hello Juergen Kuehlwein
add -O 1 to your compile command
strangely with optimization level 1 thru 3 it works.
Juergen Kuehlwein
Posts: 284
Joined: Mar 07, 2018 13:59
Location: Germany

Re: Compiling errors #3

Post by Juergen Kuehlwein »

@srvaldez


amazing - this works, thanks a lot!

[ironic]
... and where in the documentation should i have read that ?
[/ironic]

Seriously, are there some hidden treasures of knowledge i missed ?


JK
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Compiling errors #3

Post by srvaldez »

@Juergen Kuehlwein
FB 64-bit inline asm is a bit tricky because the assembly code is translated into gcc inline asm and gcc compiles that code.
one thing to consider is to use local labels and relative jumps, it allows gcc more freedom to optimize and also spares you from strange errors such as this label is already defined.
another approach is to use gcc extended inline asm, but it takes some time to study and at first it looks very alien.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Compiling errors #3

Post by TeeEmCee »

I would guess it's a bug in old versions of GCC when dealing with __attribute__(common) variables. The code you posted works fine for me in both 32 and 64 bit on GNU/Linux with fbc 1.06 and gcc 7.3.0.
Post Reply