Guide to create static lib for PB in masm, fasm

Share your advanced PureBasic knowledge/code with the community.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Guide to create static lib for PB in masm, fasm

Post by Keya »

"Why port when you can import?" - Some lazy (or efficient?) programmer i happen to agree with :)

See also:
* Guide to create static C lib for PB in Linux/OSX using gcc
* Guide to create static C lib for PB in Windows/Visual Studio
I think it is really awesome Purebasic allows us to use .lib libraries so im trying to get comfortable with the basics of that :)
___

If there's a setpath(s).bat in your masm/fasm directory (or \bin\) run that first! ...

1. Create mylib.asm:
MASM:

Code: Select all

.486
.model flat, stdcall
.code

start:

addnums proc num1:dword, num2:dword
 mov eax, num1
 mov ecx, num2
 add eax, ecx
 ret ;eax
addnums endp

end start
FASM:

Code: Select all

format MS COFF
public  addnums as '_addnums@8'
include 'win32a.inc'

section '.text' code readable executable

proc addnums num1,num2
 mov eax, [num1]
 mov ecx, [num2]
 add eax, ecx
 ret ;eax
endp
2. Compile .asm to .obj:

Code: Select all

MASM: ml /c /coff mylib.asm
FASM: fasm mylib.asm
3. Compile .obj to .lib:
fasm seemingly cant create libs so i use masm32's lib.exe for both:

Code: Select all

lib /out:mylib.lib mylib.obj
4. Call mylib.lib from Purebasic:

Code: Select all

Import "mylib.lib"  ;Import, not ImportC
  addnums.l (num1.l, num2.l) As "_addnums@8"  ;8 as there are 2x4-byte params
EndImport

Define num1.l, num2.l, result.l
num1 = 2:  num2 = 5
result = addnums(num1, num2)  ;should be 7
Debug "Result=" + Str(result)
Last edited by Keya on Tue Jun 07, 2016 5:48 am, edited 5 times in total.
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Guide to create static lib for PB in masm

Post by Lunasole »

Thanks, looks pretty simple. If only I can write libs in ASM :D

PS. Did you even tried to use GCC on Windows? I've seen many C projects ready to build both by VC and GCC, but failed to build any of them with GCC ported to windows. I was trying with DevC++ IDE which has GCC binaries for windows.
The reason is that now many VC projects using version 2013 or newer - extremely bloated and useless IDEs unlike VC 2010 (damn, when I installed VC 2013 it increased size of windows 7 registry files by ~100mb [several times larger and trashed that it was before] and that didn't cleared up after uninstall), and compiling them using VC 2010 becomes hard and requires lot of fixes in some cases.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Guide to create static lib for PB in masm

Post by DontTalkToMe »

If you are interested: I use GCC on windows, or to be more precise TDM-GCC

http://tdm-gcc.tdragon.net/

I use CodeBlocks and I often compiled open source libraries, most of which are using CMake and create a makefile for CodeBlocks.
it increased size of windows 7 registry files by ~100mb [several times larger and trashed that it was before] and that didn't cleared up after uninstall)
That's typical. Large programs when "uninstalling" leave behind tons of files in different folders and garbage in the registry. I would make them pay a fine for every file left on my system. Idiots.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: Guide to create static lib for PB in masm

Post by Keya »

yes ive been using TDM-GCC too quite a bit where C is required :) it seems one of the friendlier C compilers, if there is such a thing. I also try compiling C with PellesC, Visual Studio, and lcc, but usually TDM first. But for all of C's portability it has a funny knack of usually only liking any one of the four :D - and having said that I haven't looked into Codeblocks yet but keep seeing it pop up so i'll have to check that out next :)
(disclaimer: im no good with C and some would argue i shouldnt have even graduated to basic yet, but yes i am proud of my masm32 example that adds two numbers together)
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: Guide to create static lib for PB in masm, fasm

Post by DontTalkToMe »

@keya
(disclaimer: im no good with C and some would argue i shouldnt have even graduated to basic yet, but yes i am proud of my masm32 example that adds two numbers together)
You look pretty snappy to me, there is a good programmer in the making in you, and you know it. Just wait and see !

If you pardon my impudence :lol:
Post Reply