Libraries: Difference between revisions

From KolibriOS wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 11: Line 11:
You will need to include them in your program, just place the includes in the same folder as your .asm file is, and put this inside your code (this should be right after your header, before the START label):
You will need to include them in your program, just place the includes in the same folder as your .asm file is, and put this inside your code (this should be right after your header, before the START label):


<asm>
<syntaxhighlight lang="asm">
include 'proc32.inc'
include 'proc32.inc'
include 'macros.inc'
include 'macros.inc'
include 'libcommon.inc'
include 'libcommon.inc'
include 'dll.inc'  
include 'dll.inc'  
</asm>
</syntaxhighlight>
After that, you'll need to think what library's you want to use, we can give each libary a name wich we'll need to use in the rest of our code:
After that, you'll need to think what library's you want to use, we can give each libary a name wich we'll need to use in the rest of our code:


<asm>
<syntaxhighlight lang="asm">
align 16
align 16
@IMPORT:
@IMPORT:
Line 27: Line 27:
         libio ,'libio.obj',\
         libio ,'libio.obj',\
         libini ,'libini.obj'  
         libini ,'libini.obj'  
</asm>
</syntaxhighlight>


Then, you'll need to read the specific library's documentation and think what function of what library you'll need..
Then, you'll need to read the specific library's documentation and think what function of what library you'll need..
We're going to import them into our program just beneath the above code, like this:
We're going to import them into our program just beneath the above code, like this:


<asm>
<syntaxhighlight lang="asm">
import  library_name_i_can_choose, \
import  library_name_i_can_choose, \
         function_name_i_can_choose_1,'real_function_name_1',\
         function_name_i_can_choose_1,'real_function_name_1',\
Line 48: Line 48:
         file.truncate,'file.truncate',\
         file.truncate,'file.truncate',\
         file.close,'file.close'
         file.close,'file.close'
</asm>
</syntaxhighlight>


Apparently, you need to initialize the heap when your program is initialised.
Apparently, you need to initialize the heap when your program is initialised.
Line 55: Line 55:
like this:
like this:


<asm>
<syntaxhighlight lang="asm">
START:
START:


mcall  68,11   
mcall  68,11   
</asm>
</syntaxhighlight>


The libraries will also need to be able to access the following procedures to work with ram.
The libraries will also need to be able to access the following procedures to work with ram.
Place them somewhere in your code ;)
Place them somewhere in your code ;)


<asm>
<syntaxhighlight lang="asm">
;-----------------------------------------------------------------------------
proc mem.Alloc size
proc mem.Alloc size ;/////////////////////////////////////////////////////////
push    ebx ecx
;-----------------------------------------------------------------------------
mcall  68, 12, [size]
        push    ebx ecx
pop    ecx ebx
        mov    eax,[size]
ret
        lea    ecx,[eax+4+4095]
        and    ecx,not 4095
        mcall  68,12
        add    ecx,-4
        mov    [eax],ecx
        add    eax,4
        pop    ecx ebx
        ret
endp
endp


;-----------------------------------------------------------------------------
proc mem.ReAlloc mptr, size
proc mem.ReAlloc mptr,size;///////////////////////////////////////////////////
push    ebx ecx edx
;-----------------------------------------------------------------------------
         mcall  68, 20, [size], [mptr]
        push    ebx ecx esi edi eax
         pop    edx ecx ebx
         mov    eax,[mptr]
ret
        mov    ebx,[size]
        or      eax,eax
        jz      @f
        lea    ecx,[ebx+4+4095]
        and    ecx,not 4095
        add    ecx,-4
        cmp    ecx,[eax-4]
        je      .exit
    @@: mov    eax,ebx
        call    mem.Alloc
        xchg    eax,[esp]
        or      eax,eax
        jz      .exit
        mov    esi,eax
        xchg    eax,[esp]
        mov    edi,eax
        mov    ecx,[esi-4]
        cmp    ecx,[edi-4]
        jbe    @f
        mov    ecx,[edi-4]
    @@: add    ecx,3
        shr    ecx,2
        cld
        rep    movsd
        xchg    eax,[esp]
        call    mem.Free
  .exit:
         pop    eax edi esi ecx ebx
        ret
endp
endp


;-----------------------------------------------------------------------------
proc mem.Free mptr
proc mem.Free mptr ;//////////////////////////////////////////////////////////
;-----------------------------------------------------------------------------
        mov    eax,[mptr]
        or      eax,eax
        jz      @f
         push    ebx ecx
         push    ebx ecx
        lea    ecx,[eax-4]
         mcall  68, 13, [mptr]
         mcall  68,13
         pop    ecx ebx
         pop    ecx ebx
    @@: ret
        ret
endp  
endp  
</asm>
</syntaxhighlight>


Now we would like our program to load the libraries
Now we would like our program to load the libraries
Line 135: Line 92:
(Note how the @IMPORT refers to the above code)
(Note how the @IMPORT refers to the above code)


<asm>
<syntaxhighlight lang="asm">
         stdcall dll.Load,@IMPORT
         stdcall dll.Load, @IMPORT
         or      eax,eax             ; This code will check if the program was able to
         or      eax, eax           ; This code will check if the program was able to
                                     ; load the libraries successfull
                                     ; load the libraries successfull
         jnz    EXIT                ; If not, it will jump to the label EXIT
         jnz    EXIT                ; If not, it will jump to the label EXIT
</asm>
</syntaxhighlight>


After that, and when you have putted your libraries in the /sys/lib/ folder  
After that, and when you have putted your libraries in the /sys/lib/ folder  
Line 148: Line 105:
Like this:
Like this:


<asm>
<syntaxhighlight lang="asm">
         invoke function_name_i_can_choose_1 ascii_string1, 0xAFB123, 1237
         invoke function_name_i_can_choose_1 ascii_string1, 0xAFB123, 1237


ascii_string1 db 'blablabla',0
ascii_string1 db 'blablabla',0
</asm>
</syntaxhighlight>


This will call the function 'real_function_name_1' from 'library_realname.obj' and pass ascii_string1, 0xAFB123 and 1237 to it.
This will call the function 'real_function_name_1' from 'library_realname.obj' and pass ascii_string1, 0xAFB123 and 1237 to it.

Latest revision as of 19:28, 31 July 2012

KolibriOS libraries explained

This article assume's you know the basics of programming applications for KolibriOS in fasm, if not, start by reading this article Writing applications for KolibriOS.

First, when you want to start writing programs using libraries, you should get ahold of these four includes (they can be found with the libraries or on the SVN server):

  • macros.inc
  • proc32.inc
  • dll.inc
  • libcommon.inc

You will need to include them in your program, just place the includes in the same folder as your .asm file is, and put this inside your code (this should be right after your header, before the START label):

<syntaxhighlight lang="asm"> include 'proc32.inc' include 'macros.inc' include 'libcommon.inc' include 'dll.inc' </syntaxhighlight> After that, you'll need to think what library's you want to use, we can give each libary a name wich we'll need to use in the rest of our code:

<syntaxhighlight lang="asm"> align 16 @IMPORT:

library \

       library_name_i_can_choose ,'library_realname.obj',\
       libio ,'libio.obj',\
       libini ,'libini.obj' 

</syntaxhighlight>

Then, you'll need to read the specific library's documentation and think what function of what library you'll need.. We're going to import them into our program just beneath the above code, like this:

<syntaxhighlight lang="asm"> import library_name_i_can_choose, \

       function_name_i_can_choose_1,'real_function_name_1',\
       function_name_i_can_choose_2,'real_function_name_2',\
       function_name_i_can_choose_3,'real_function_name_3'

import libio, \

       file.aux.match_wildcard,'file.aux.match_wildcard',\
       file.find_first,'file.find_first',\
       file.find_next,'file.find_next',\
       file.find_close,'file.find_close',\
       file.open,'file.open',\
       file.seek,'file.seek',\
       file.write,'file.write',\
       file.truncate,'file.truncate',\
       file.close,'file.close'

</syntaxhighlight>

Apparently, you need to initialize the heap when your program is initialised. To do this, you may use system function 68 subfunction 11

like this:

<syntaxhighlight lang="asm"> START:

mcall 68,11 </syntaxhighlight>

The libraries will also need to be able to access the following procedures to work with ram. Place them somewhere in your code ;)

<syntaxhighlight lang="asm"> proc mem.Alloc size push ebx ecx mcall 68, 12, [size] pop ecx ebx ret endp

proc mem.ReAlloc mptr, size push ebx ecx edx

       mcall   68, 20, [size], [mptr]
       pop     edx ecx ebx

ret endp

proc mem.Free mptr

       push    ebx ecx
       mcall   68, 13, [mptr]
       pop     ecx ebx
       ret

endp </syntaxhighlight>

Now we would like our program to load the libraries We're going to use a function from dll.inc for this. Just put this at the beginning of your code: (Note how the @IMPORT refers to the above code)

<syntaxhighlight lang="asm">

       stdcall dll.Load, @IMPORT
       or      eax, eax            ; This code will check if the program was able to
                                   ; load the libraries successfull
       jnz     EXIT                ; If not, it will jump to the label EXIT

</syntaxhighlight>

After that, and when you have putted your libraries in the /sys/lib/ folder (if they werent already there :), it's time to start the real work..

You can use the invoke function (from proc32.inc) to use any of the functions you included Like this:

<syntaxhighlight lang="asm">

       invoke function_name_i_can_choose_1 ascii_string1, 0xAFB123, 1237

ascii_string1 db 'blablabla',0 </syntaxhighlight>

This will call the function 'real_function_name_1' from 'library_realname.obj' and pass ascii_string1, 0xAFB123 and 1237 to it. To know what you need to pass to each function, you need to read the library's documentation...

Good luck!