680x0:Opening a library
This is a example on how to open a library:
(with the Library Vector Offsets added so you won't need to use any includes yet)
OldOpenLibrary = -408 ; OldOpenLibrary(libName)(A1)
OpenLibrary = -552 ; OpenLibrary(libName,version)(A1,D0)
CloseLibrary = -414 ; CloseLibrary(library)(A1)
move.l $4.w,a6 ; Get the value at position $4 in memory ; This value is the base of exec.library
lea LibName(pc),a1 ; libName must be in A1 moveq #0,d0 ; version must be in D0 jsr OpenLibrary(a6) ; Call the OpenLibrary function
; OpenLibrary looks if the library is in memory (or ROM) ; and loads it if nessesary. ; Then it returns the base of the requested library in d0
move.l d0,libbase ; Store the base
; You can access function in a library with this base, ; like we called OpenLibrary() with exec.library's base.
- move.l d0,a6 ; move to a6, since d0 can't be used for addresses
- jsr SomeFunction(a6)
; Now close the library we opened move.l $4.w,a6 ; Get exec.library's base again move.l libbase,a1 ; Get the base of the library we want to close jsr CloseLibrary(a6)
libname: dc.b 'dos.library',0 libbase: dc.l $00000000
- ...scratch registers
Notes on optimizing:
- use 'move.l $4.w,a6' instead of 'move.l $4,a6'
- use 'moveq' if the version you require is lower than 128
- if you want to save on bytes you can reuse libname to store your libbase
- There's also a function OldOpenLibrary doesn't have the library version check. If you are desperate you could use OldOpenLibrary and save a few bytes by not needing a moveq/move.l #version,d0. Commodore didn't want anyone to use this, it would be removed, but it never happened and removing it would probably break too many programs.