home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 7509 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  6.4 KB

  1. Path: aargh.incubus.sub.org!marc
  2. From: marc@aargh.incubus.sub.org (Marc 'Nepomuk' Heuler)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Writing shared libraries in assembler
  5. Message-ID: <JDiqy*Oza@aargh.incubus.sub.org>
  6. Date: Wed, 17 Apr 1996 21:55:57 CET
  7. Reply-To: marc@aargh.incubus.sub.org
  8. References: <9604162155.AA0035i@etchq.demon.co.uk>
  9. Organization: Data Design
  10. X-Newsreader: Arn V1.03a
  11.  
  12. In article <9604162155.AA0035i@etchq.demon.co.uk>, MushyPea writes:
  13.  
  14. > I am happy with the idea of writing 100% re-entrant, relocatable code - what
  15. > I would like is some assistance on the format of a shared library, including
  16. > the library source, and the creation of an _LVO offset file.
  17.  
  18.          STRUCTURE    mylib,LIB_SIZE            ; library base
  19.         UBYTE    mylib_Flags
  20.         UBYTE    mylib_Pad01
  21.         APTR    mylib_SegList
  22.         APTR    mylib_SysBase
  23.         APTR    mylib_DosBase
  24.         APTR    mylib_ExpansionBase
  25.         APTR    mylib_UtilityBase
  26.         APTR    mylib_GraphicsBase
  27.         APTR    mylib_CiaBase
  28.         APTR    mylib_Status
  29.         ULONG    mylib_RandSeed
  30.         LABEL    mylib_Size
  31.  
  32.         SECTION    Library,code
  33.  
  34. Dummy        moveq    #0,d0
  35.         rts
  36.  
  37. RomTag        dc.w    RTC_MATCHWORD
  38.         dc.l    RomTag
  39.         dc.l    .EndCode
  40.         dc.b    RTF_AUTOINIT
  41.         dc.b    _VERSION
  42.         dc.b    NT_LIBRARY
  43.         dc.b    PRIORITY
  44.         dc.l    .Name
  45.         dc.l    .Info
  46.         dc.l    .Init
  47.  
  48. .EndCode
  49. .Name        NAME
  50.         dc.b    ".library",0
  51.         even
  52.         dc.b    "$VER: "
  53. .Info        NAME
  54.         dc.b    ".library "
  55.         VERSION
  56.         dc.b    " ("
  57.         DATE
  58.         dc.b    ")"
  59. [copyright info follows]
  60.         dc.b    $d,$a,0
  61.         even
  62.  
  63. .Init        dc.l    mylib_Size
  64.         dc.l    .Func
  65.         dc.l    .Data
  66.         dc.l    Init
  67.  
  68. .Func        dc.l    Open        ;FD: ##base _mylibBase
  69.         dc.l    Close        ;FD: ##bias 30
  70.         dc.l    Expunge        ;FD: ##public
  71.         dc.l    Null
  72.         dc.l    mylibRexx    ;FD: mylibRexx(rexxmsg)(a0)
  73.         dc.l    mylibRandom    ;FD: mylibRandom()()
  74.         dc.l    mylibError    ;FD: mylibError(error)(d0)
  75. [other function definitions follow]
  76.         dc.l    -1        ;FD: ##end
  77.  
  78. .Data        INITBYTE LN_TYPE,NT_LIBRARY
  79.         INITLONG LN_NAME,.Name
  80.         INITBYTE LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
  81.         INITWORD LIB_VERSION,_VERSION
  82.         INITWORD LIB_REVISION,_REVISION
  83.         INITLONG LIB_IDSTRING,.Info
  84.         dc.l     0
  85.  
  86.         ***************************************************
  87.         ***            Init Library        ***
  88.         ***************************************************
  89.         ***    d0:library a0:seglist            ***
  90.         ***************************************************
  91.  
  92. Init        move.l    a5,-(sp)
  93.         move.l    d0,a5                ; a5:library
  94.  
  95.         move.l    a6,mylib_SysBase(a5)
  96.         move.l    a0,mylib_SegList(a5)
  97.  
  98.         lea    .UtilityName(pc),a1        ; open utility.library
  99.         moveq    #37,d0
  100.         CALL    OpenLibrary
  101.         move.l    d0,mylib_UtilityBase(a5)
  102.         beq    .Error
  103.  
  104.         lea    .DosName(pc),a1            ; open dos.library
  105.         moveq    #37,d0
  106.         CALL    OpenLibrary
  107.         move.l    d0,mylib_DosBase(a5)
  108.         beq    .Error
  109.  
  110.         lea    .ExpansionName(pc),a1
  111.         moveq    #37,d0
  112.         CALL    OpenLibrary
  113.         move.l    d0,mylib_ExpansionBase(a5)
  114.         beq    .Error
  115.  
  116.         lea    .GraphicsName(pc),a1        ; open graphics.library
  117.         moveq    #37,d0
  118.         CALL    OpenLibrary
  119.         move.l    d0,mylib_GraphicsBase(a5)
  120.         beq    .Error
  121.  
  122.         lea    .CiaName(pc),a1            ; open ciab.resource
  123.         CALL    OpenResource
  124.         move.l    d0,mylib_CiaBase(a5)
  125.         beq    .Error
  126.  
  127. [other stuff follows that gets executed whenever the library is
  128.  fresh loaded into memory]
  129.  
  130.         ***************************************************
  131.         ***              Exit            ***
  132.         ***************************************************
  133.  
  134. .Okay        move.l    a5,d0
  135. .Exit        move.l    (sp)+,a5
  136.         rts
  137.  
  138. .Error        bsr    Cleanup
  139.         moveq    #0,d0
  140.         bra.s    .Exit
  141.  
  142. .UtilityName    dc.b    "utility.library",0
  143. .DosName    dc.b    "dos.library",0
  144. .ExpansionName    dc.b    "expansion.library",0
  145. .GraphicsName    dc.b    "graphics.library",0
  146. .CiaName    dc.b    "ciab.resource",0
  147.         even
  148.  
  149.         ***************************************************
  150.         ***        Function: Open()        ***
  151.         ***************************************************
  152.         ***    a6:library d0:version            ***
  153.         ***************************************************
  154.  
  155. Open        addq.w    #1,LIB_OPENCNT(a6)
  156.         bclr    #LIBB_DELEXP,LIB_FLAGS(a6)
  157.         move.l    a6,d0
  158.         rts
  159.  
  160.         ***************************************************
  161.         ***              Close()            ***
  162.         ***************************************************
  163.         ***    a6:library                ***
  164.         ***************************************************
  165.  
  166. Close        moveq    #0,d0
  167.         subq.w    #1,LIB_OPENCNT(a6)
  168.         bne.s    .Exit
  169.         btst    #LIBB_DELEXP,LIB_FLAGS(a6)
  170.         beq.s    .Exit
  171.         bsr    Expunge
  172. .Exit        rts
  173.  
  174.         ***************************************************
  175.         ***            Expunge()            ***
  176.         ***************************************************
  177.         ***    a6:library                ***
  178.         ***************************************************
  179.  
  180. Expunge        movem.l    d2/a5-a6,-(sp)
  181.         move.l    a6,a5
  182.         tst.w    LIB_OPENCNT(a5)
  183.         bne.s    .Delayed
  184.  
  185.         bsr    Cleanup
  186.  
  187.         move.l    mylib_SegList(a5),d2
  188.         move.l    a5,a1                ; remove from library list
  189.         REMOVE
  190.         moveq    #0,d0                ; free library base
  191.         move.w    LIB_NEGSIZE(a5),d0
  192.         move.l    a5,a1
  193.         sub.l    d0,a1
  194.         add.w    LIB_POSSIZE(a5),d0
  195.         CALLEXE    FreeMem
  196.         move.l    d2,d0
  197. .Exit        movem.l    (sp)+,d2/a5-a6
  198.         rts
  199.  
  200. .Delayed    bset    #LIBB_DELEXP,LIB_FLAGS(a5)    ; delayed expunge
  201.         moveq    #0,d0
  202.         bra.s    .Exit
  203.  
  204.         ***************************************************
  205.         ***              Cleanup            ***
  206.         ***************************************************
  207.  
  208. Cleanup        move.l    mylib_ExpansionBase(a5),d0
  209.         beq.s    .NoExpansion
  210.         move.l    d0,a1
  211.         CALLEXE    CloseLibrary
  212. .NoExpansion    move.l    mylib_UtilityBase(a5),d0        ; close utility.library
  213.         beq.s    .NoUtility
  214.         move.l    d0,a1
  215.         CALLEXE    CloseLibrary
  216. .NoUtility    move.l    mylib_DosBase(a5),d0        ; close dos.library
  217.         beq.s    .NoDos
  218.         move.l    d0,a1
  219.         CALLEXE    CloseLibrary
  220. .NoDos        move.l    mylib_GraphicsBase(a5),d0        ; close graphics.library
  221.         beq.s    .NoGraphics
  222.         move.l    d0,a1
  223.         CALLEXE    CloseLibrary
  224. .NoGraphics    rts
  225.  
  226.         ***************************************************
  227.         ***            Null()            ***
  228.         ***************************************************
  229.  
  230. Null        moveq    #0,d0
  231.         rts
  232.  
  233. I extract the FD info (see func-table above) by a selfmade ARexx script,
  234. and use C='s LVO program to create an _LVO.i file that can be included into
  235. Assembly programs that want to use the library functions:
  236.  
  237. rx <"apipe:ssearch src:mylib/mylib.s *";FD: *" cs nonum" >src:mylib/mylib_lib.fd "do while ~eof(stdin) ; line = readln(stdin) ; if line ~= '' then say delstr(line,1,pos(';FD: ',line)+4) ; end"
  238. copy src:mylib/mylib_lib.fd fd:
  239. rx <"apipe:lvo mylib" >src:mylib/mylib_lvo.i "do while ~eof(stdin) ; line = readln(stdin) ; if line = '' then iterate ; parse var line with ' $' dummy ' ' offset '  ' name '()' ; say '_LVO'||name '=' offset ; end
  240.  
  241. > Does anybody have any suggestions or advice on writing libraries?  I don't
  242. > particularly want to have to spend large quantities of money on the RKMs at
  243. > the moment, although I will eventually invest in them.
  244.  
  245. You should buy them now!  Programming a library is not the only thing that
  246. can be done wrong.
  247.