Docs for ASM command

Forum for discussion about the documentation project.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Docs for ASM command

Post by fzabkar »

https://www.freebasic.net/wiki/KeyPgAsm

Code: Select all

' Assuming "n" is a FB global or local ULONG variable
mov  eax, [n]        ' OK: size is apparent from eax
inc  [n]             ' Not OK: size is not given
inc  dword [n]       ' Not OK: size given, but still not accepted by GAS
inc  dword Ptr [n]   ' OK: "ptr" is needed by GAS here
I had a case where the following line produced a compiler error:

Code: Select all

mov [Function], 0

Code: Select all

"Error: ambiguous operand size for `mov' "
I needed the following code to resolve this error:

Code: Select all

movb [Function], 0
Could similar examples be added to the docs? I spent a lot of time off-site searching for this solution.
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Docs for ASM command

Post by Vortex »

Hello,

Assuming that the return value of your function is DWORD sized, this one works :

Code: Select all

mov  DWORD PTR [Function],0
In your case, movb means :

Code: Select all

mov     byte ptr [.....], 0  
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Docs for ASM command

Post by MrSwiss »

Vortex wrote:Assuming that the return value of your function is DWORD sized, this one works :
I think it's vice versa: stating the size of the 0 value:
Asm mov [function], DWord Ptr[0]
Since the size of [function] is known (typically EAX or RAX, aka: register size).
Depends on bitness of FBC used (32 - DWord, 64 - QWord).
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Docs for ASM command

Post by jj2007 »

MrSwiss wrote:I think it's vice versa: stating the size of the 0 value:
Asm mov [function], DWord Ptr[0]
No, it's asm mov [function], DWord Ptr 0 (you should test your code before posting wrong information)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Docs for ASM command

Post by MrSwiss »

Its not wrong "what I've written", as you've just confirmed. Thanks.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Docs for ASM command

Post by jj2007 »

MrSwiss wrote:Its not wrong "what I've written", as you've just confirmed. Thanks.
Nothing you ever write is ever "wrong", dear Swiss Oberlehrer (apart, from, the, commas of, course), but what you wrote above throws errors.

These are tested:

Code: Select all

  Dim myR8 as longint
  asm
	mov [function], eax
	mov [function], ax
	mov [function], al
	mov [function], byte ptr 0
	mov [function], word ptr 0
	mov [function], dword ptr 0
	' bug? movlps [myR8], qword ptr 0	' Error: operand type mismatch for `movlps'
  end asm
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Docs for ASM command

Post by MrSwiss »

Seems you don't want to understand the difference between a Assembler (TASM/FASM ...) and FBC's inline ASM ...
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Docs for ASM command

Post by jj2007 »

MrSwiss wrote:Seems you don't want to understand the difference between a Assembler (TASM/FASM ...) and FBC's inline ASM ...
Dear Swiss friend, I tested it with GAS before posting it. You didn't.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Docs for ASM command

Post by fxm »

And you, have you also tested the syntax proposed by MrSwiss?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Docs for ASM command

Post by jj2007 »

fxm wrote:And you, have you also tested the syntax proposed by MrSwiss?
Yesss. It chokes.
MrSwiss wrote:I think it's vice versa: stating the size of the 0 value:
Asm mov [function], DWord Ptr[0]
Error: too many memory references for `mov' (and that's correct, there are very few mem to mem instructions in x86)

P.S. ' bug? movlps [myR8], qword ptr 0 ' Error: operand type mismatch for `movlps' is not a bug, it must be (for example)
movlps [myR8], xmm0 - my fault.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Docs for ASM command

Post by fzabkar »

Here is my working code:

Code: Select all

Function paritychk ( wordvar As UShort ) As Byte

    ASM
        movb [Function], 0
        mov eax, [wordvar]
        xor al, ah
        jp L1
        movb [Function], 1
L1:
    End ASM
   
End Function
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Docs for ASM command

Post by jj2007 »

Good choice, actually!

Code: Select all

Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
 1745         milliseconds for paritychk FZ, sum= 9680484  <<<<<< slow alternatives
 116          milliseconds for paritychkFZA, sum= 9680484  <<<<<< your code
 95           milliseconds for MB  PopCount, sum= 9680484  <<<<<< 20% faster (but it rarely matters)
 60           milliseconds for paritychk J1, sum= 9680484 (native popcnt, not available on older CPUs)
Here is an alternative that is a tick faster:

Code: Select all

Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
 1770         milliseconds for paritychk FZ, sum= 9680484
 107          milliseconds for paritychkFZA, sum= 9680484 <<<<<<<<<<<<<<<<<<<<<<<<<<<
 102          milliseconds for MB  PopCount, sum= 9680484
 63           milliseconds for paritychk J1, sum= 9680484 (native popcnt instruction)
It avoids the jp:

Code: Select all

Function paritychkA ( wordvar As UShort ) As Byte
    ASM
        mov eax, [wordvar]
        xor al, ah
        setnp al
        movsx eax, al
        mov [Function], eax
    End ASM  
End Function
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Docs for ASM command

Post by fzabkar »

How about this?

Code: Select all

Function paritychkA ( wordvar As UShort ) As Byte
    ASM
        mov eax, [wordvar]
        xor al, ah
        setnp al
'        movsx eax, al
'        mov [Function], eax
        mov [Function], al
    End ASM 
End Function
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Docs for ASM command

Post by jj2007 »

Exactly the same timings. However, I forgot something: Naked code has no overhead...!

Code: Select all

Function paritychkA naked ( wordvar As UShort ) As Byte
  ASM
	mov eax, [esp+4]	' wordvar
	xor al, ah
	setnp al
	ret 4
  End ASM  
End Function
And that one beats the native popcnt function!

Code: Select all

Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, AVX)

 1765         milliseconds for paritychk FZ, sum= 9680484
 59           milliseconds for paritychkFZA, sum= 9680484
 60           milliseconds for paritychkFZA, sum= 9680484
 59           milliseconds for paritychkFZA, sum= 9680484
 197          milliseconds for paritychk BI, sum= 9680484

 349          milliseconds for paritychk J0, sum= 9680484 (cmp ecx, 16)
 350          milliseconds for paritychk J0, sum= 9680484 (dec ecx)
 350          milliseconds for paritychk J0, sum= 9680484 (cmp ecx, 16)
 354          milliseconds for paritychk J0, sum= 9680484 (dec ecx)
 356          milliseconds for paritychk J0, sum= 9680484 (cmp ecx, 16)
 351          milliseconds for paritychk J0, sum= 9680484 (dec ecx)

 96           milliseconds for MB  PopCount, sum= 9680484
 61           milliseconds for paritychk J1, sum= 9680484 (native popcnt instruction)
However, PopCount and popcnt do more than just return parity: they count the bits set.
Post Reply