TweetFollow Us on Twitter

Eject the Disk
Volume Number:1
Issue Number:12
Column Tag:Basic School

Building A Library Routine To Eject The Disk

By Dave Kelly, Hybrids Engineer, MacTutor Editorial Board

This months BASIC column features some of the ins and outs of installing your own 68000 code to your MS BASIC programs. The potential power that this provides is mind boggling. With just a few lines of code you can access some of the other ROM routines that are not provided with MS BASIC 2.0.

First off, if you are not already familiar with the Apple Macintosh 68000 Development System then I suggest that you read through the manual to become familiar with the system. In MacTutor Vol. 1 No. 1 the Assembly Language Lab column provides an overview of the MDS system that will be useful for our discussion in this column. It is not necessary that you know 68000 assembly to enter and compile the BASIC Library we will be installing. I will try to lead you through step by step from the point of view of someone that is not very familiar with the MDS system.

Now before we get into the thick of things we will discuss the BASIC LIBRARY statement. The LIBRARY command is a BASIC reserved word but there is no description of it in the BASIC manual other than a brief mention of it on page 103. This page also refers to some special documentation entitled "Microsoft BASIC for the Macintosh -- Building Machine Language Libraries" (hereafter abbreviated as BMLL) which is available by contacting the Microsoft Consumer Response Department. The BMLL document has all the necessary information required to set up your own machine language library and includes 3 sample libraries: CopyFile, PrintArgs, and AddStrings.

A BASIC Library is a Macintosh resource with one or more named CODE segments. Refer to the Inside Macintosh Programmer's Guide section for the definition of the file structure of a resource file. Other sections of Inside Macintosh will be useful in setting up your own routines.

To open a library file you use the statement LIBRARY <filename string expression>. Up to 5 library files may be opened at the same time. When the Library file is opened BASIC calls the routine LIBinit which must exist in each file. The purpose of LIBinit is to allow a way for you to inform BASIC of the compatability of your library routine with the version of BASIC being used. If the library routine is incompatible or the LIBinit routine does not exist or the handshake with that routine fails, then the library is closed and an "Illegal function call" error is generated. This statement can result in other errors which are described in BMLL.

Once the library has been opened the routines within the library may be called by BASIC with the BASIC statements: <routine name> [argument-list] or by CALL <routine name> [(argument-list)]. More on the CALL statement may be found in your BASIC manual starting on page 101.

Constructing a library resource for BASIC

A library is detatched from BASIC whenever NEW, SYSTEM, RUN, or LIBRARY CLOSE is executed. To close the library, BASIC looks for the routine called LIBterm if present and executes it. The routine LIBterm should release any memory which was allocated from the heap, and close any files which were opened by routines in the library. Unless you need to allocate or deallocate memory you probably won't need to use LIBterm.

A sample source file for LIBinit is given with BMLL, but you'll have to work out your own LIBterm if needed depending on the routine you are writing. The sample LIBinit file demonstrates how the routine can check the BASIC version and see if it is compatible with the routines in the library. This sample checks to see that the binary version of BASIC is being used and tells BASIC if it is not compatible. Also included is an equate file named BASIC.D which defines entry points in BASIC which you should use to communicate with BASIC. For my sample library I have included all applicable equates in my source listing so the equate file will not be needed. You will want to use these routines to allocate memory and read the variable argument lists in the BASIC CALL statement. It is advisable that you obtain BMLL before writing your own routines as there are alot of useful routines that can be used that are part of BASIC. BMLL explains what each routine does and what registers are effected.

Alright, now we'll get right down to business. First we need to create several files with the MDS editor which are to be used by the assembler, linker and resource compiler. The two code segments that we want to create are listed below and should be saved with the file names shown. The LIBinit routine will be installed as the first segment and our routine called Eject will be the second segment. Eject will allow you to eject either the internal or external disk from within your program (under BASIC control). Type in the two routines then save them to disk.

Save this routine as "LIBinit.asm".

;
; LIBinit ROUTINE
;Sample version of LIBinit routine
;
;  REGISTER INFORMATION
;
;  A0 = pointer to a version record containing:
;     2 bytes = version of Basic interpreter (ie 2 for 2.01)
;  2 bytes = revision number of Basic interpreter (01)
;  2 bytes = 0 if decimal math, 1 if IEEE binary format
;2 bytes = compatibility variable of this routine to Basic:
;0 = compatible
;-1= incompatible
;8 bytes = reserved array of four INTEGER values
;
; A4 = pointer to a handle (long word) owned by this library
;Use this as a handle to a static data segment.
; A5 = pointer to the base of the application jump table
; D0 = 0 on exit if this routine is purgeable.

LIBVER_Result  EQU 6 ;offset to version record field
 ;for compatibility. (See above)
 
LIBinit:
 CLR.W  LIBVER_Result(A0)       ;assume compatible 
LIBinitExit:
 MOVEQ  #0,D0
 RTS

Here is the Eject Library source code. Save this file as "Eject.asm".

; BASIC Eject Library Source Code
; By Dave Kelly
; MacTutor 1985

;Synopsis:
;CALL Eject ( VolRefNum )
;Output:
;The specified disk is ejected (not dismounted) from
;the selected drive where VolRefNum may be 0 for the
;default drive, 1 for the internal drive, 2 for the
;external drive.

GetNextLibArg  EQU $2A    ; Basic Lib offset from A5
IntegerArgEQU  $32   ; Basic Lib offset from A5
BasicErrorEQU  $42 ; Basic Lib offset from A5

.TRAP _Eject$A000+23 ;toolbox  trap

Ejectdisk:
 BSR  GetIntegerVar;integer arg in [d3:w]
 CMP.W  #0,d3
 BEQ  Doit;branch if on 0 (default drive)
 CMP.W  #1,d3
 BEQ  Doit;branch if on 1 (internal drive)
 CMP.W  #2,d3
 BEQ  Doit;branch if on 2 (external drive)
UnknownVol:
 MOVEQ  #74,d2   ;Unknown Volume error
 JSR  BasicError(a5) 
Doit: 
 LEA  ParamBlock,a0;get pointer
 MOVE.W d3,22(a0);move drive # to ioVRefNum 
 _Eject 
 RTS  
GetIntegerVar:
 JSR  GetNextLibArg(a5) ;Get the next  ;argument
 JSR  IntegerArg(a5) ;[d3:w] = integer ;(error if arg can't be forced 
 ;into an integer
 RTS

;  local data area
 
ParamBlock:
 DC.L 0 ;ioLinkptr.
 DC.W 0 ;ioType
 DC.W 0 ;ioTrap
 DC.L 0 ;ioCmdAddr
 DC.L 0 ;ioCompletionptr.
 DC.W 0 ;ioResult
 DC.L 0 ;ioFileNameptr.
 DC.W 0 ;ioDrvNum (ioVRefNum)

 END

By the way, the Eject routine shows an example of using the file manager parameter block similar to what has been discussed in a few places in the last few MacTutor issues. Refer to those columns for more information. (ie Vol. 1 number 7 & 8)

Next we need to start a new MDS editor file to create a link file for the linker to use. The link shown below should be typed in and saved as "EjectLib.link". Note that if you are not using the default (startup) disk to save these files, the volume name must also be included with the filename.

;Eject Library Link
;MacTutor 1985

/OUTPUT EjectLib
LIBinit.Rel
<
Eject.Rel
$

Now create one last editor file for the Resource compiler. This file creates the resource file that BASIC will call to use the library. Be sure that each line (including the last one) has a carriage return at the end of the line or all the resources may not be compiled. Save this file as "EjectLib.R"

EjectLib.Rsrc
BLIB
TYPE CODE = GNRL
LIBinit,1
.R
EjectLib CODE 1

TYPE CODE = GNRL
Eject,2
.R
EjectLib CODE 2

Ok, we are now ready to assemble our code segments. Assemble LIBinit.asm and Eject.asm by selecting ASM from the MDS system transfer menu or by double clicking on the assembler if you are running the finder desktop. A standard file dialog box will appear and you can select each file and assemble them one at a time. Two new files containing the assembled code are stored on the disk, LIBinit.Rel and Eject.Rel. These two files will be used by the linker to create a linked application file. Select the LINK application from the transfer menu of the MDS system. Note you may have to push the cancel button from the dialog box to get to the transfer menu. The LINKER will create an application to be used by RMAKER. Don't try to run the EjectLib application as it will BOMB!! It only contains resource code segments and is not a regular application program. After linking, select RMAKER from the transfer menu and use the EjectLib.R file to create the resource file to be used by our BASIC library.

Figure 1 shows the files required and the flow of the above procedures. If you have any problems with this then I recommend that you study the MDS system manual and the BMLL supplement.

To try out the Eject Library routine, type in the Eject Demo program. To call the Eject routine, open the Eject Library and then use CALL Eject (VolRefNum). The VolRefNum is a number for the volume which you want to eject. A "0" will eject the disk in the default drive, a "1" will eject the internal drive and "2" will eject the external drive. For any other VolRefNum the file manager returns a unknown volume name error which could be trapped in your BASIC error trap routine if necessary. By the way, using ResEdit you can move the resource segments from the resource files into your BASIC program file (data file where your program is stored). Then the routine can be called by opening up the library with the filename the same as the program. The program would now be completely transportable as one file.

Installing the routines is the easy part. After trying out the demo routines in the BMLL supplement and the Eject routine your mouth may be watering for some more. Well next time we will take a look at some routines which have been prewritten to attach to your BASIC programs allowing access to over a hundred more ROM routines. They are available from a company called Clear Lake Research, 5353 Dora Street #7, Houston, Texas 77005 (1-800-835-2246 X199). If you've felt that BASIC just didn't have enough access to ROM routines then check this one out.

'Eject Library Demo
'By Dave Kelly
'MacTutor ©1985

'Note: EjectLib.Rsrc must be on default disk
'or you must specify vol name in Library statement
LIBRARY  "EjectLib.Rsrc"
'Set up menus
FOR i=1 TO 5
    MENU i,0,0,""
NEXT i
MENU 1,0,1,"File"
MENU 1,1,1,"Quit"
MENU 2,0,1,"Eject Disk"
MENU 2,1,1,"Eject Default Disk"
MENU 2,2,1,"Eject Internal Disk"
MENU 2,3,1,"Eject External Disk"

ON MENU GOSUB Menucheck:MENU ON
pause:GOTO pause

Menucheck:
    menunumber = MENU(0)
    menuitem=MENU(1):MENU
    IF menunumber = 1 THEN filemenu
    IF menunumber <>2 THEN RETURN
    IF menuitem = 1 THEN vol= 0
    IF menuitem = 2 THEN vol = 1
    IF menuitem = 3 THEN vol = 2
    CALL Eject (vol)
    RETURN
    
filemenu:
    IF menuitem <> 1 THEN RETURN
    MENU RESET:LIBRARY CLOSE:END

Thanks go to Robert Millis for sending us an improvement to the Cursor Editor routine in the August '85 issue. Due to editorial deadlines it is not possible to streamline all the code used in each issue for maximum efficiency. We do try to make sure that each and every program works. Keep in mind also that the code for most of the columns must be edited to fit properly. The programs are run before editing and placed on the source disks which you may order from us. We encourage you to modify and improve our programs. Please feel encouraged to send us any improvements that you have made so that our other readers may also benefit. The following code may replace the getpixel routine in the August Cursor Editor for improved speed and usability of the program:

getpixel: 'For Cursor Editor
    pixel%=256
    xp=MOUSE(1):yp=MOUSE(2)
    IF xp<20 OR xp>196 THEN RETURN
    IF yp<20 OR yp>196 THEN RETURN
    row=INT((yp-20)/11):col = INT((xp-20)/11)
    pixel%=row*16+(15-col)
    RETURN

It should be noted that the same routine was used in the September Paint Pattern Editor with some minor differences. Don't mix them up; they are different. The following code may replace the getpixel routine in Paint Pattern Editor:

getpixel: 'For Paint Pattern Editor
    pixel%=64
    xp=MOUSE(1):yp=MOUSE(2)
    IF xp<20 OR xp>108 THEN RETURN
    IF yp<20 OR yp>108 THEN RETURN
    row=INT((yp-20)/11):col = INT((xp-20)/11)
    pixel%=row*8+(7-col)
    RETURN
 
AAPL
$101.58
Apple Inc.
+0.00
MSFT
$46.52
Microsoft Corpora
+0.00
GOOG
$584.77
Google Inc.
+0.00

MacTech Search:
Community Search:

Software Updates via MacUpdate

Delivery Status 6.1.2 - Check delivery s...
Delivery Status displays delivery status of packages for a variety of shipment services. Can't wait for your packages to arrive? Don't waste your time checking the site constantly, just open this all... Read more
Mavericks Cache Cleaner 8.0.9 - Clear ca...
Mavericks Cache Cleaner is an award-winning general purpose tool for OS X. MCC makes system maintenance simple with an easy point-and-click interface to many OS X functions. Novice and expert users... Read more
OneNote 15.2.2 - Free digital notebook f...
OneNote is your very own digital notebook. With OneNote, you can capture that flash of genius, that moment of inspiration, or that list of errands that's too important to forget. Whether you're at... Read more
Apple Configurator 1.6 - Configure and d...
Apple Configurator makes it easy for anyone to mass configure and deploy iPhone, iPad, and iPod touch in a school, business, or institution. Three simple workflows let you prepare new iOS devices... Read more
SpamSieve 2.9.16 - Robust spam filter fo...
SpamSieve is a robust spam filter for major email clients that uses powerful Bayesian spam filtering. SpamSieve understands what your spam looks like in order to block it all, but also learns what... Read more
OS X Server 3.2.1 - For OS X 10.9.5 Mave...
OS X Server is the next generation of Apple's award winning server software. Designed for OS X and iOS devices, OS X Server makes it easy to share files, schedule meetings, synchronize contacts, host... Read more
Apple Security Update 2014-004 - For OS...
Apple Security Update is recommended for all users and improves the security of Mac OS X. For information on the security content of this update, please visit this website: http://support.apple.com/... Read more
OS X Mavericks 10.9.5 - The latest versi...
Apple OS X Mavericks is the latest release of the world's most advanced desktop operating system. Now free! With more than 200 new features, OS X Mavericks brings Maps and iBooks to the Mac,... Read more
iExplorer 3.5.0.0 - View and transfer al...
iExplorer is an iPhone browser for Mac lets you view the files on your iOS device. By using a drag and drop interface, you can quickly copy files and folders between your Mac and your iPhone or... Read more
BusyCal 2.6 - Powerful calendar app with...
BusyCal is an award-winning desktop calendar that combines personal productivity features for individuals with powerful calendar sharing capabilities for families and workgroups. BusyCal's unique... Read more

Latest Forum Discussions

See All

Ninja Raft (Games)
Ninja Raft 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: ** Special Launch Price ** "Ninja Raft is definitely the game to play if you’re into Tower Defense games and want to play something... | Read more »
Paste+ | Clipboard Action Widget (Produ...
Paste+ | Clipboard Action Widget 1.0.0 Device: iOS iPhone Category: Productivity Price: $3.99, Version: 1.0.0 (iTunes) Description: Powerful clipboard widget for iOS 8. Reimagine what you can do with your clipboard! Paste+ is a... | Read more »
Agenda+ | Calendar & Reminder Widget...
Agenda+ | Calendar & Reminder Widget 1.0.0 Device: iOS iPhone Category: Productivity Price: $1.99, Version: 1.0.0 (iTunes) Description: Best Calendar and Reminder Widget Ever for iOS 8 | Read more »
Leg·end·ar·y (Games)
Leg·end·ar·y 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Simple and yet complex. Leg•end•dar•y is a grid-based puzzle game based on numbers. Discover the adventure of Leg•end•dar•y and... | Read more »
KuaiBoard (formerly QuickBoard) (Utilit...
KuaiBoard (formerly QuickBoard) 1.0 Device: iOS Universal Category: Utilities Price: $1.99, Version: 1.0 (iTunes) Description: KuaiBoard is currently 50% off for launch! Billing info. Signatures. Locations. KuaiBoard allows you to... | Read more »
Treasure Fetch - Adventure Time (Games)
Treasure Fetch - Adventure Time 1.0.1 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0.1 (iTunes) Description: Adventure Time Treasure Fetch is a fresh take on the classic Snake game! STRETCHY JAKE Snake your way... | Read more »
Light in the Dark (Games)
Light in the Dark 1.1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.1.0 (iTunes) Description: Be enlightened by this delightful puzzle game that has never before seen the light of day! "A physics and color-blending... | Read more »
Transmit iOS (Utilities)
Transmit iOS 1.0 Device: iOS Universal Category: Utilities Price: $9.99, Version: 1.0 (iTunes) Description: >> LAUNCH SPECIAL: Transmit iOS is ONLY $10 for a LIMITED TIME. << THE #1 FILE TRANSFER CLIENT FOR THE MAC NOW... | Read more »
Minuum - The Little Keyboard for Big Fin...
Minuum - The Little Keyboard for Big Fingers 1.0 Device: iOS iPhone Category: Utilities Price: $1.99, Version: 1.0 (iTunes) Description: Type faster, see more of your screen, and take control of autocorrect with Minuum: the little... | Read more »
TextExpander 3 + custom keyboard (Utili...
TextExpander 3 + custom keyboard 3.0 Device: iOS Universal Category: Utilities Price: $4.99, Version: 3.0 (iTunes) Description: | Read more »

Price Scanner via MacPrices.net

16GB iPad Air on sale for $449, save $50
Walmart has the 16GB iPad Air WiFi on sale for $449 on their online store for a limited time. Choose free home shipping or free local store pickup. Their price represents a $50 savings over standard... Read more
13-inch 256GB MacBook Air on sale for $1099,...
B&H Photo has the 2014 13″ 1.4GHz 256GB MacBook Air on sale for $1099.99. Shipping is free, and B&H charges NY sales tax only. Their price is $100 off MSRP. Read more
Toshiba Introduces TransMemory ID High-Speed...
Toshiba’s Digital Products Division (DPD), a division of Toshiba America Information Systems, Inc., today introduced the TransMemory ID USB 3.0 Flash Drive, a simpler storage solution for people who... Read more
New iPads and OS X Yosemite Release Coming Oc...
The DailyDot’s Micah Singleton reports that Apple is planning to hold its next product announcement event on Oct. 21, at which it will unveil the iPad Air 2 and iPad mini 3 and release a final build... Read more
Logitech Bluetooth Multi-Device Cross-Platfor...
Logitech has an enviable track record of making some of the best computer keyboards and mice. At least in my estimation, the best freestanding keyboards I’ve ever used have been Logitech units,... Read more
Roundup of Apple refurbished iPad Airs and iP...
Apple is offering Certified Refurbished iPad Airs for up to $140 off MSRP. Apple’s one-year warranty is included with each model, and shipping is free. Stock tends to come and go with some of these... Read more
Sprint offers 16GB iPad mini for $199.99 with...
Sprint is offering 1st generation 16GB iPad minis for $199.99 with a 2-year service agreement. Standard MSRP for this iPad is $429. Their price is the lowest available for this model. Read more
2.5GHz Mac mini remains on sale for $549, sav...
B&H Photo has the 2.5GHz Mac mini on sale for $549.99 including free shipping. That’s $50 off MSRP, and B&H will also include a free copy of Parallels Desktop software. NY sales tax only. Read more
Apple refurbished iMacs available for up to $...
The Apple Store has Apple Certified Refurbished iMacs available for up to $300 off the cost of new models. Apple’s one-year warranty is standard, and shipping is free. These are the best prices on... Read more
13″ 2.5GHz MacBook Pro offered for $100 off M...
B&H Photo has the 13″ 2.5GHz MacBook Pro on sale for $999.99 including free shipping plus NY sales tax only. Their price is $100 off MSRP. Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.