Question about inline assembler and C backend.

Just starting out? Need help? Post your questions and find answers here.
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Question about inline assembler and C backend.

Post by Armoured »

I don't have experience with assembly programming, but after testing various examples, I noticed that they only work if the C backend is not used.
I was wondering if it is currently possible to use inline assembly with the C backend, and if so, how?

Thanks
Fred
Administrator
Administrator
Posts: 18175
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Question about inline assembler and C backend.

Post by Fred »

Sure you can, but you need to use the gcc syntax: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Question about inline assembler and C backend.

Post by Armoured »

With the asm{} keyword? Could you also provide a simple example?
Thanks.
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: Question about inline assembler and C backend.

Post by jack »

gcc extended asm is a very strange animal, it takes a special kind of mindset to understand it, you can find small examples on the web but it's tricky to adapt to PB
here's an ultra-simple and useless example

Code: Select all

Procedure.d get_pi()

    Define.d pi

    !asm (
    !    "fldpi\n"
    !    "fstpl   %[v_pi]\n"
    !    :[v_pi]"=m"(v_pi)::
    !);

    ProcedureReturn pi
  EndProcedure
  
  Define.d n
  n=get_pi()
  Debug n
  
fortunately you can also use intel syntax, not sure who the original author is

Code: Select all

Procedure bswap(v.l) 
  Protected ret.l 
  CompilerIf #PB_Compiler_Backend = #PB_Backend_C 
    !".intel_syntax noprefix";
    !"mov eax, v_v";
    !"bswap eax"; 
    !"mov v_ret, eax";
  CompilerElse 
    !mov eax, [p.v_v]
    !bswap eax 
    !mov [p.v_ret],eax 
  CompilerEndIf 
  ProcedureReturn ret 
EndProcedure 

x.i = $FF000000 
Debug RSet(Hex(x),8,"0")
x = bswap(x) 
Debug RSet(Hex(x),8,"0")
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: Question about inline assembler and C backend.

Post by jack »

a slightly more interesting example using the gcc extended inline syntax

Code: Select all

Procedure.q fib(ub.l, *f)
  If (ub<3) 
    ProcedureReturn 1
  EndIf
  ;  !set_dpfpu();
    !asm (
    !    "mov %[p_f], %%rax;"
    !    "fldz;"
    !    "fistl (%%rax);"
    !    "add $8, %%rax;"
    !    "fld1;"
    !    "fistl (%%rax);"
    !    "add $8, %%rax;"
    !    "mov %[v_ub], %%ecx;"
    !    "sub $2, %%ecx;"
    !    "fib2:"
    !    "fxch %%st(1);"
    !    "fadd %%st(1),%%st(0);"
    !    "fld %%st(0);"
    !    "fistpq (%%rax);"
    !    "add $8, %%rax;"
    !    "dec %%ecx;"
    !    "jg fib2;"
    !    "fstp %%st(0);"
    !    "fstp %%st(0);" 
    !     :[p_f]"=m"(p_f)
    !     :[v_ub]"m"(v_ub)
    !     :"rax","ecx"
    !);
    ProcedureReturn
  EndProcedure
  
  Dim fibonacci.q(20)
  n.l=20
  fib(n+1, @fibonacci(0))
  For i.l=0 To n
   Debug fibonacci(i)
  Next
  
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Question about inline assembler and C backend.

Post by Armoured »

Thanks Jack! Your examples have been really helpful for understanding how to insert inline assembly code, which I couldn’t find in the PureBasic manual. I have one last question: could you point me to some tutorials or books on assembly that support this syntax (AT&T, if I understood correctly)?
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Re: Question about inline assembler and C backend.

Post by jack »

I don't know of any books or tutorials except for bits and pieces on the web, perhaps some other member knows
this could be useful https://github.com/namantam1/x86-assembly
User avatar
mk-soft
Always Here
Always Here
Posts: 6238
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Question about inline assembler and C backend.

Post by mk-soft »

If you use ASM code in C-Backend, you have problems at the latest if you want to run the code on a machine with ARM (Raspberry, Window for ARM) or M1 (macOS).

So I never become ASM in C-backend.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5881
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Question about inline assembler and C backend.

Post by idle »

I thought about this for awhile as it would be nice to still be able to use intel asm from the c backend
and while the demo isn't particularly useful it wouldn't be to hard to turn into a compiler tool so it generates a data section and loads it.
https://www.purebasic.fr/english/viewtopic.php?t=86226
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Question about inline assembler and C backend.

Post by Armoured »

mk-soft wrote: Thu Feb 06, 2025 10:56 pm If you use ASM code in C-Backend, you have problems at the latest if you want to run the code on a machine with ARM (Raspberry, Window for ARM) or M1 (macOS).

So I never become ASM in C-backend.
You are absolutely right, but unfortunately I need to generate an atomic variable to optimize a thread, and portability is not my priority in this case. If you have any alternatives that do not require the use of assembly, I'm interested.
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Question about inline assembler and C backend.

Post by Armoured »

idle wrote: Fri Feb 07, 2025 5:06 am I thought about this for awhile as it would be nice to still be able to use intel asm from the c backend
and while the demo isn't particularly useful it wouldn't be to hard to turn into a compiler tool so it generates a data section and loads it.
https://www.purebasic.fr/english/viewtopic.php?t=86226
That's really interesting—thank you!
User avatar
idle
Always Here
Always Here
Posts: 5881
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Question about inline assembler and C backend.

Post by idle »

You can easily use asm insrinsics in c backend if you need atomics.
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Question about inline assembler and C backend.

Post by Armoured »

idle wrote: Fri Feb 07, 2025 7:10 pm You can easily use asm insrinsics in c backend if you need atomics.
How?
User avatar
idle
Always Here
Always Here
Posts: 5881
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Question about inline assembler and C backend.

Post by idle »

Armoured wrote: Sat Feb 08, 2025 12:37 am
idle wrote: Fri Feb 07, 2025 7:10 pm You can easily use asm insrinsics in c backend if you need atomics.
How?
what functions do you need?
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: Question about inline assembler and C backend.

Post by Armoured »

idle wrote: Sat Feb 08, 2025 1:26 am
Armoured wrote: Sat Feb 08, 2025 12:37 am
idle wrote: Fri Feb 07, 2025 7:10 pm You can easily use asm insrinsics in c backend if you need atomics.
How?
what functions do you need?
I need a function like InterlockedIncrement.
Post Reply