tutorial to write libs in asm or C requested

For everything that's not in any way related to PureBasic. General chat etc...
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

tutorial to write libs in asm or C requested

Post by jack »

i am fairly familiar in asm, and dabbled in C, so please, freedimension or
El Choni, please give me (and the world) a brief tutorial in writing
good asm or C libs for PB. :P
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

You're very lucky it already exists XD

It was available from Mr. Skunk's site (who had written it long ago, for ASM), but the site is down now and we haven't heard of Mr. Skunk for a long time. If nobody posts a working link to it, I'll put it in my ftp site tomorrow (I don't have it here). It's not completely up to date, but enough (uses NASM syntax, which is very similar to FASM's).

You should also have a look at the docs and samples in the PureBasic/Library SDK/ folder, and download some libs with included source which will help you a lot to learn about this.

Regards,
El_Choni
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

thanks, El Choni, I admire your work and that of Mr. Skunk.
i have saved Mr. Skunks Tutorials on My HD, perhaps you could update
them for us starving souls? :)
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

But, these tutorials are just fine. You only need to change the file headers and segment declarations. To do it, copy any header from an ASM lib and use it. Change "Extern" for "Extrn", "GLOBAL" for "public" and you're done.

Apart from this, there are some useful macros in FASM that make coding and maintaining code easier.

One of them is the 'proc' macro, included with the FASM distribution (not with PB). With that macro, function code can look like this:

Code: Select all

proc PB_MyFunction,arg1,arg2,arg3
  MyLocalStruc STRUCTURE 
  MyLocalVar dd ?
  MyLocalVar2 dd ?
  MyLocal3Var dd ?
  enter
  push ebx ecx
  ...
  ...
  pop ecx ebx
  return ; <-- this is another macro
In the function code, you can refer to args and local vars by its defined name without having to use neither globals nor [esp+8] or whatever. However, if you use this macro, the function must be declared as StdCall in the .Desc file. But since in most cases you must save the first argument (eax when you don't use StdCall), which would require 'push' and 'pop' anyway, my advice would be to use always StdCall, unless you don't need to keep the value of eax in your code.

I've also adapted a 'procs' macro for functions that return a string, to make PB string pointer available also by using a name and not [esp+args*4+4].

Another useful macro is 'assume', very useful with structures:

Code: Select all

Extrn _GetProcessHeap@0
Extrn _HeapAlloc@12

GetProcessHeap equ _GetProcessHeap@0
HeapAlloc equ _HeapAlloc@12

...
  push ebx
...
  call GetProcessHeap
  stdcall HeapAlloc,eax,0,1024 ; <-- stdcall macro, you'll need it ;)
  mov ebx, eax
  assume ebx,POINT ; <-- ebx is widely used to 'cast' structures in ASM
  mov [ebx.x], 4
...
  pop ebx
...
There are lots of other macros for FASM. For further information, i recommend you the FASM forum:

http://board.flatassembler.net

Feel free to ask any questions about this. Regards,
El_Choni
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

thanks El Choni for the tip :)
Post Reply