ASM translation from PB to FB

General FreeBASIC programming questions.
AWPStar
Posts: 38
Joined: May 03, 2009 21:47

ASM translation from PB to FB

Postby AWPStar » Aug 04, 2015 9:51

Can someone help me to translate asm code from Powerbasic to FreeBasic?
SRC and DEST - pointers to 32bit picture data

Code: Select all

SUB ALPHA_C(BYREF SRC AS LONG, BYREF DEST AS LONG, _
          BYVAL W AS LONG, BYVAL H AS LONG, _
          BYVAL WS AS LONG, BYVAL WD AS LONG) EXPORT

#REGISTER NONE
DIM ff AS LONG
DIM x AS LONG

  ! mov ff, 255
  ! mov edi, dest
  ! mov esi, src
  ! sub esi, edi

  ! mov eax, w
  ! sub wd, eax
  ! shl wd, 2
  ! sub ws, eax
  ! shl ws, 2
  ! mov eax, wd
  ! sub ws, eax

  ! pxor mm2, mm2

lab0:
  ! mov ecx, w

lab1:
  ! mov bl, [esi+edi+3]
  ! cmp bl, 0
  ! je lab2
  ! mov dh, [edi+3]

  ! mov al, 255
  ! sub al, bl
  ! mul dh
  ! div byte ff

  ! mov bh, bl
  ! shl ebx, 8
  ! mov bl, bh

  ! mov ah, al
  ! shl eax, 8
  ! mov al, ah

  ! movd mm4, eax
  ! punpcklbw mm4, mm2

  ! movd mm5, ebx
  ! punpcklbw mm5, mm2

  ! movd mm0, [esi+edi]
  ! punpcklbw mm0, mm2

  ! movd mm1, [edi]
  ! punpcklbw mm1, mm2

  ! pmullw mm1, mm4
  ! psrlw mm1, 8

  ! pmullw mm0, mm5
  ! psrlw mm0, 8

  ! paddusw mm0, mm1
  ! packuswb mm0, mm0
  ! movd x, mm0

  ! mov al, 255
  ! sub al, dh
  ! mul bl
  ! div byte ff
  ! add dh, al

  ! mov ah, x[0]
  ! div dh
  ! mov [edi], al

  ! mov ah, x[1]
  ! div dh
  ! mov [edi+1], al

  ! mov ah, x[2]
  ! div dh
  ! mov [edi+2], al

  ! mov [edi+3], dh

lab2:
  ! add edi, 4
  ! dec ecx
  ! jne lab1

  ! add esi, ws
  ! add edi, wd
  ! dec h
  ! jne lab0
  ! emms

END SUB


i tried replace (!) with asm and compile with gcc. but it doen't work.
MrSwiss
Posts: 3364
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ASM translation from PB to FB

Postby MrSwiss » Aug 04, 2015 10:44

AWPStar wrote:SRC and DEST - pointers to 32bit picture data

This type of request is difficult, because: no idea what the code piece is used for.
There is no way to test under the given circumstances ...

The pointers must be specified in FreeBASIC Sub's/Function's !!! (see below in code)
AFAIK the ByRef is not necessary on pointers, ByVal should work also ...
EXPORT directive is only needed in a library/dll (fixed/dynamic lib).

ATTENTION: this is only for 32bit FBC ... NOT for the x64 Version!! 32bit ASM ...

Just a quick try:

Code: Select all

SUB ALPHA_C(BYREF SRC AS LONG PTR, BYREF DEST AS LONG PTR, _
         BYVAL W AS LONG, BYVAL H AS LONG, _
         BYVAL WS AS LONG, BYVAL WD AS LONG) EXPORT
   DIM ff AS LONG
   DIM x AS LONG

   Asm
      mov ff, 255
      mov edi, dest
      mov esi, src
      sub esi, edi   
   
      mov eax, w
      sub wd, eax   
      shl wd, 2   
      sub ws, eax
      shl ws, 2
      mov eax, wd
      sub ws, eax   
   
      pxor mm2, mm2
   
   lab0:
      mov ecx, w
   
   lab1:
      mov bl, [esi+edi+3]
      cmp bl, 0
      je lab2
      mov dh, [edi+3]
      
      mov al, 255
      sub al, bl   
      mul dh   
      div byte ff
   
      mov bh, bl
      shl ebx, 8
      mov bl, bh
      
      mov ah, al
      shl eax, 8
      mov al, ah
      
      movd mm4, eax
      punpcklbw mm4, mm2
      
      movd mm5, ebx
      punpcklbw mm5, mm2
      
      movd mm0, [esi+edi]
      punpcklbw mm0, mm2
      
      movd mm1, [edi]
      punpcklbw mm1, mm2
      
      pmullw mm1, mm4
      psrlw mm1, 8
      
      pmullw mm0, mm5
      psrlw mm0, 8
      
      paddusw mm0, mm1
      packuswb mm0, mm0
      movd x, mm0
      
      mov al, 255
      sub al, dh   
      mul bl   
      div byte ff
      add dh, al
   
      mov ah, x[0]
      div dh
      mov [edi], al
      
      mov ah, x[1]
      div dh
      mov [edi+1], al
      
      mov ah, x[2]
      div dh
      mov [edi+2], al
      
      mov [edi+3], dh
   
   lab2:
      add edi, 4
      dec ecx
      jne lab1
      
      add esi, ws
      add edi, wd
      dec h
      jne lab0
      emms
   End Asm

END SUB
AWPStar
Posts: 38
Joined: May 03, 2009 21:47

Re: ASM translation from PB to FB

Postby AWPStar » Aug 04, 2015 11:02

The pointers must be specified in FreeBASIC Sub's/Function's !!!
Ye, you are right.
EXPORT directive is only needed in a library/dll (fixed/dynamic lib).
i use -dll flag.

i ried to do this but it doesn' work.
with gcc

Code: Select all

compiling C failed: 'D:\FreeBASIC-1.02.0-win32\bin\win32\gcc.exe' terminated with exit code 1

with fbc

Code: Select all

assembling failed: 'D:\FreeBASIC-1.02.0-win32\bin\win32\as.exe' terminated with exit code 1


I think there's some syntax problem.
MrSwiss
Posts: 3364
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ASM translation from PB to FB

Postby MrSwiss » Aug 04, 2015 11:10

AWPStar wrote:I think there's some syntax problem.

First thing: try THE POINTER's ByVal (instead of ByRef) ...
Second: supply more information (as stated in my first post), ALL error messages when compiling ...
What is this THING supposed to DO, anyway ???
AWPStar
Posts: 38
Joined: May 03, 2009 21:47

Re: ASM translation from PB to FB

Postby AWPStar » Aug 04, 2015 11:24

as.exe cmd errors
Image

fbc cmd line
-dll -fpu sse -fpmode fast

FreeBASIC Compiler - Version 1.02.0 (04-05-2015), built for win32 (32bit)
Copyright (C) 2004-2015 The FreeBASIC development team.
standalone
target: win32, 486, 32bit
compiling: E:\VBProjects\MEDIA\BoneIt\sr2dex.bas -o E:\VBProjects\MEDIA\BoneIt\sr2dex.asm (main module)
assembling: D:\FreeBASIC-1.02.0-win32\bin\win32\as.exe --32 --strip-local-absolute "E:\VBProjects\MEDIA\BoneIt\sr2dex.asm" -o "E:\VBProjects\MEDIA\BoneIt\sr2dex.o"
assembling failed: 'D:\FreeBASIC-1.02.0-win32\bin\win32\as.exe' terminated with exit code 1


vb6 side:

Code: Select all

call ALPHA_C(srcdata(0),dstdata(0),....)
or
call ALPHA_C(varptr(srcdata(0)),varptr(dstdata(0)),....)

the only difference in declaration
MrSwiss
Posts: 3364
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ASM translation from PB to FB

Postby MrSwiss » Aug 04, 2015 11:32

Sorry mate, your title is simply misleading, if you are in reality coding in VB ...
You'll have to sort this thing out by yourself ...
I've NOT the slightest idea how VB does anything ...
AWPStar
Posts: 38
Joined: May 03, 2009 21:47

Re: ASM translation from PB to FB

Postby AWPStar » Aug 04, 2015 11:38

But it's all about difference in asm syntax in pb and fb.
MrSwiss
Posts: 3364
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: ASM translation from PB to FB

Postby MrSwiss » Aug 04, 2015 11:51

AWPStar wrote:But it's all about difference in asm syntax in pb and fb.

It is NOT, the very moment you are using a different language to call a procedure,
(in a lib) this languages syntax (you might call that interface) differs from FB, there-
fore the procedure has to return something 'legible' to the caller!

BTW: the differences in ASM in FB is in my first code ... (if the ASM is written with
faults, I can't help it ...) lack of information!

FYI: The FBC version 1.02.0 was incomplete, use either 1.02.1 or the current ver. 1.03.0.
caseih
Posts: 1424
Joined: Feb 26, 2007 5:32

Re: ASM translation from PB to FB

Postby caseih » Aug 05, 2015 1:04

I think what the OP means is he has a PB program that compiles to a dll that is then used from VB. So he's got several problems going on at the same time if I understand correctly.

- He's unable to compile his PB dll with fbc owing to differences in assembler syntax, calling conventions, etc.
- He's unable to actually get fbc to generate a dll of any sort.
- Use the resulting dll in a similar manner to how he used it in VB.

Did I get that right?

Rather than try to translate the asm directly at this point, why not explain what the function does. What are the inputs and outputs, and what is expected when a caller calls it. Then I'm sure the assembler can be tweaked to work inside of fbc, though perhaps a pure-FB algorithm can be found that is just as fast. Assembler is simply not portable (as you are finding out, even on the same platform).

As for the dll stuff, try making a simple dummy dll first. Just one function. Try the fbc invocation with -dll:

Code: Select all

fbc -dll mylibrary.bas

Then try using that dll. Once you get that working, attempt your actual code again. See http://www.freebasic.net/wiki/wikka.php ... dLibraries

I note that on Linux I don't seem to need the export keyword when making functions. They seem to still link and resolve symbols okay. Not sure if Linux is just different from Windows in this or what.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: ASM translation from PB to FB

Postby MichaelW » Aug 05, 2015 6:48

I downloaded and installed Version 1.02.0 (04-05-2015), built for win32 (32bit), and corrected the errors in the first 12 lines of assembly code so the code will compile (and assemble) without errors.

Code: Select all

SUB ALPHA_C(BYREF SRC AS LONG PTR, BYREF DEST AS LONG PTR, _
            BYVAL W AS LONG, BYVAL H AS LONG, _
            BYVAL WS AS LONG, BYVAL WD AS LONG) EXPORT
    DIM ff AS LONG
    DIM x AS LONG

    Asm
        mov DWORD PTR [ff], 255
        mov edi, [dest]
        mov esi, [src]
        sub esi, edi   
   
        mov eax, [w]
        sub [wd], eax
        shl DWORD PTR [wd], 2   
        sub [ws], eax
        shl DWORD PTR [ws], 2
        mov eax, [wd]
        sub [ws], eax
   
        pxor mm2, mm2
    lab0:
   
    end asm
   
end sub   

For the indirect memory operands (normally local variables or parameters on the stack), the names must be placed in square brackets because the compiler will replace the names with EBP references that the assembler will not recognize as indirect memory operands, if the brackets are not present. And for instructions that contain an indirect memory operand, and where the other operand is not a register, you must specify the operand size (DWORD PTR in this case, but there are other possibilities).
AWPStar
Posts: 38
Joined: May 03, 2009 21:47

Re: ASM translation from PB to FB

Postby AWPStar » Aug 05, 2015 9:15

@MichaelW
Thank you for the explanation!

Got it! But still cannot compile with gcc. Is there some specific asm rules in gcc?

Image

What should i do with these lines?

Code: Select all

div Byte ff
mov ah, [x + 1]

Will it work?

Code: Select all

div BYTE PTR [ff]
mov ah, BYTE PTR x[1]
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: ASM translation from PB to FB

Postby MichaelW » Aug 05, 2015 11:28

I'm not sure what you mean by "compile with gcc". I can compile my test source, as posted, with -gen gcc using this command line:

Code: Select all

"\program files (x86)\freebasic32\fbc" -gen gcc -Wc -O2 -v -C -R -RR -s console testpbconversion.bas

And get this output:

Code: Select all


C:\Users\User\FreeBASIC32 My>"\program files (x86)\freebasic32\fbc" -gen gcc -Wc
 -O2 -v -C -R -RR -s console testpbconversion.bas
FreeBASIC Compiler - Version 1.02.0 (04-05-2015), built for win32 (32bit)
Copyright (C) 2004-2015 The FreeBASIC development team.
standalone
target:       win32, 486, 32bit
compiling:    testpbconversion.bas -o testpbconversion.c (main module)
compiling C:  C:\program files (x86)\freebasic32\bin\win32\gcc.exe -m32 -march=i
486 -S -nostdlib -nostdinc -Wall -Wno-unused-label -Wno-unused-function -Wno-unu
sed-variable -Wno-unused-but-set-variable -Wno-main -Werror-implicit-function-de
claration -O0 -fno-strict-aliasing -frounding-math -fno-math-errno -fno-exceptio
ns -fno-unwind-tables -fno-asynchronous-unwind-tables -masm=intel "testpbconvers
ion.c" -o "testpbconversion.asm" -O2
assembling:   C:\program files (x86)\freebasic32\bin\win32\as.exe --32 --strip-l
ocal-absolute "testpbconversion.asm" -o "testpbconversion.o"
linking:      C:\program files (x86)\freebasic32\bin\win32\ld.exe -m i386pe -o "
testpbconversion.exe" -subsystem console "C:\program files (x86)\freebasic32\lib
\win32\fbextra.x" --stack 1048576,1048576 -s -L "C:\program files (x86)\freebasi
c32\lib\win32" -L "." "C:\program files (x86)\freebasic32\lib\win32\crt2.o" "C:\
program files (x86)\freebasic32\lib\win32\crtbegin.o" "C:\program files (x86)\fr
eebasic32\lib\win32\fbrt0.o" "testpbconversion.o" "-(" -lfb -lgcc -lmsvcrt -lker
nel32 -luser32 -lmingw32 -lmingwex -lmoldname -lgcc_eh "-)" "C:\program files (x
86)\freebasic32\lib\win32\crtend.o"

C:\Users\User\FreeBASIC32 My>pause
Press any key to continue . . .

But for a source where the speed-critical parts are coded in assembly, compiling with gcc provides no real advantages because the compiler is limited to optimizing C code.
AWPStar
Posts: 38
Joined: May 03, 2009 21:47

Re: ASM translation from PB to FB

Postby AWPStar » Aug 05, 2015 11:56

MichaelW wrote:I'm not sure what you mean by "compile with gcc". I can compile my test source, as posted, with -gen gcc using this command line:...

I found out that the gcc doesn't want to compile it with these lines:

Code: Select all

div Byte PTR [ff]
'mov ah, [x + 0]
'works with mov ah, Byte PTR [x + 0]
div dh

compiling C failed

But fbc does.
Don't know why.
dkl
Site Admin
Posts: 3210
Joined: Jul 28, 2005 14:45
Location: Germany

Re: ASM translation from PB to FB

Postby dkl » Aug 05, 2015 12:45

Hi,

you might be running into an issue where fbc -gen gcc replaces instruction names in inline asm by references to previously declared variables/procedures. For example:

Code: Select all

declare sub div alias "div"

dim divisor as long = 1
asm
   div dword ptr [divisor]
end asm

FreeBASIC-1.03.0-linux-x86/bin/fbc test.bas -gen gcc
2.c: In function ‘main’:
2.c:21:56: error: ‘div’ undeclared (first use in this function)
...


That was fixed recently in fbc's Git repository (see also http://sourceforge.net/p/fbc/bugs/782/).

The work-around in the above example is to avoid using the name "div" as symbol name, i.e. the sub div must be renamed, then it compiles. Of course that's not ideal, because (for example) the CRT binding declares a function named "div" too (in crt/stdlib.bi).
caseih
Posts: 1424
Joined: Feb 26, 2007 5:32

Re: ASM translation from PB to FB

Postby caseih » Aug 05, 2015 14:13

I am a bit confused... AWPStar, what does gcc have to do with things? I thought this was originally a PB file. Now you're using a C file?

Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests