home *** CD-ROM | disk | FTP | other *** search
/ Amiga Computing 59 / ac059.adf / SupportFiles / afiles.a / afiles.a
Text File  |  1993-02-05  |  6KB  |  203 lines

  1. ;File commands
  2. ;assembly source code
  3.  
  4.       INCLUDE "EXEC/TYPES.I"
  5.       INCLUDE "LIBRARIES/DOS.I"
  6.       INCLUDE "EXEC/MEMORY.I"
  7.  
  8.  ;External references
  9.  ;the amiga linker will link these to the program
  10.  
  11.  
  12.  XREF  _AbsExecBase        ;exec library address
  13.                            ;always stored in the same location
  14.  XREF  _LVOOpenLibrary     ;exec library routines
  15.  XREF  _LVOCloseLibrary
  16.  XREF  _LVOAllocMem
  17.  XREF  _LVOFreeMem
  18.  
  19.  
  20.  XREF  _LVODelay           ;dos library routines
  21.  XREF  _LVOOutput
  22.  XREF  _LVOWrite
  23.  XREF  _LVORead
  24.  XREF  _LVOOpen
  25.  XREF  _LVOClose
  26.  
  27.      CSECT text,code       ;this directive is used by the Lattice Assembler
  28.  
  29. main:
  30.  jsr      openlibraries    ;open the libraries,if possible
  31.  tst.l    d0
  32.  beq      exit2             ;exit if there is an error
  33.  jsr      memorybuffer      ;allocate memory 
  34.  tst.l    d0                ;exit if there is an error
  35.  beq      exit
  36.  move.l   dosbase,a6
  37.  jsr      readfile          ;read the file,if possible
  38.  tst.l    d0
  39.  beq      exit
  40.  jsr      writefile         ;write the file 
  41.  move.l   buffer,d2         ;display the memory buffer contents
  42.  move.l   #152,d3
  43.  jsr      message
  44.  move.l   #100,d1
  45.  jsr      _LVODelay(a6)     ;delay for a few seconds
  46.  move.l   #filesok,d2       ;success message
  47.  move.l   #15,d3
  48. exit:
  49.  jsr      cleanup           ;tidy up loose ends
  50. exit2:
  51.  rts
  52.  
  53. openlibraries:
  54.  move.l   _AbsExecBase,a6  ;exec library address
  55.  lea      dosname,a1       ;pointer to string containing dos library name
  56.  clr.l    d0               ;any version 
  57.  jsr      _LVOOpenLibrary(a6) ;open the dos library
  58.  movem.l  d0,dosbase       ;and store the returned pointer
  59.  tst.l    d0               ;check for success
  60.  bne      ok1              ;branch if successful
  61.  move.l   #nolib,d2
  62.  move.l   #17,d3
  63.  rts
  64. ok1:
  65.  move.l   dosbase,a6            ;dos library address
  66.  jsr      _LVOOutput(a6)        ;obtain a filehandle for output to the console
  67.  movem.l  d0,consolefilehandle  ;output file handle
  68.  rts
  69.  
  70. memorybuffer:                  ;allocate a block of memory
  71.  move.l   _AbsExecBase,a6       
  72.  move.l   #152,d0              ;152 bytes
  73.  clr.l    d1
  74.  bset     #MEMF_PUBLIC,d1            ;memory requirements
  75.  bset     #MEMF_CLEAR,d1
  76.  jsr      _LVOAllocMem(a6)
  77.  movem.l  d0,buffer            ;store the pointer to the memory block 
  78.  bne      ok2
  79.  move.l   #nomem,d2            ;failure message
  80.  move.l   #17,d3
  81. ok2:
  82.  rts
  83.  
  84. cleanup:                       ;tidy up
  85.  jsr      message              ;display message on screen
  86.  move.l   inputfilehandle,d1   ;close files, if open
  87.  beq      noinfile
  88.  jsr      _LVOClose(a6)
  89. noinfile:
  90.  move.l   outputfilehandle,d1
  91.  beq      nooutfile
  92.  jsr      _LVOClose(a6)
  93. nooutfile:
  94.  move.l   _AbsExecBase,a6      ;free memory, if allocated
  95.  move.l   buffer,a1
  96.  beq      nomem2
  97.  move.l   #152,d0
  98.  jsr      _LVOFreeMem(a6)
  99. nomem2:
  100.  move.l   dosbase,a1           ;dos library pointer
  101.  jsr      _LVOCloseLibrary(a6) ;close the library
  102. nodoslib:
  103.  rts
  104.  
  105.  
  106. message:
  107.  move.l  dosbase,a6            ;dos library address
  108.  move.l  consolefilehandle,d1  ;output file handle
  109.  jsr     _LVOWrite(a6)         ;write to the console
  110.  rts
  111.  
  112. readfile:                      ;read a file
  113.  move.l  dosbase,a6
  114.  move.l  #filename,d1          ;open the file if possible
  115.  move.l  #MODE_OLDFILE,d2      ;must be an existing file
  116.  jsr     _LVOOpen(a6)          
  117.  movem.l d0,inputfilehandle    ;store a pointer to the filehandle
  118.  move.l  d0,d1
  119.  bne     handleok
  120.  clr.l   d0
  121.  move.l  #nofile,d2            ;failure message
  122.  move.l  #28,d3
  123.  rts
  124. handleok:                      ;read file, if file is open
  125.  move.l  buffer,d2             ;memory buffer for file contents
  126.  move.l  #152,d3               ;number of bytes
  127.  jsr     _LVORead(a6)
  128.  tst.l   d0
  129.  bne     bytesok               
  130.  move.l  #nobytes,d2           ;failure message
  131.  move.l  #28,d3
  132. bytesok:
  133.  rts
  134.  
  135. writefile:                     ;write a file
  136.  move.l  #filename2,d1         ;name of the file
  137.  move.l  #MODE_NEWFILE,d2      ;overwrite any existing file of that name
  138.  jsr     _LVOOpen(a6)          ;open the file
  139.  movem.l d0,outputfilehandle   ;store pointer to the file handle
  140.  move.l  d0,d1
  141.  bne     handleok2             ;failure message 
  142.  move.l  #nofile2,d2
  143.  move.l  #29,d3
  144.  rts
  145. handleok2:
  146.  move.l  #newcontents,d2       ;pointer to data to be written
  147.  move.l  #20,d3                ;20 bytes
  148.  jsr     _LVOWrite(a6)         ;write to the file
  149.  move.l  outputfilehandle,d1
  150.  move.l  buffer,d2             ;pointer to data to be written
  151.  move.l  #152,d3               ;152 bytes
  152.  jsr     _LVOWrite(a6)         ;write to the file
  153.  rts
  154.  
  155.  
  156.        CSECT data              ;directive 'SECTION DATA,data' is used by 
  157.                                ;Metacomco Assembler
  158.  
  159. dosname:
  160.  dc.b    'dos.library',0       ;name of dos library
  161.  ds.w    0                     ;word alignment
  162. dosbase:                       ;pointer to Dos Library base
  163.  dc.l    0
  164. consolefilehandle:             ;file handle for console output 
  165.   dc.l    0
  166. buffer:                        ;memory buffer
  167.   dc.l    0                    
  168. inputfilehandle:               ;file handle for input file
  169.   dc.l    0
  170. outputfilehandle:              ;file handle for output file
  171.   dc.l    0
  172. bytes:                         ;number of bytes read 
  173.    dc.l   0
  174. filename:                      ;file names of text files
  175.    dc.b   'limerick',0
  176.    ds.w   0
  177. filename2:
  178.    dc.b   'limerick2',0
  179.    ds.w   0
  180. newcontents:                    ;messages
  181.    dc.b   'This is a new file',13,0
  182.    ds.w    0
  183. nolib:
  184.   dc.b    'No dos library',10,13,0
  185.   ds.w     0
  186. nomem:    
  187.   dc.b    'No free memory',10,13,0
  188.   ds.w     0
  189. filesok:
  190.   dc.b     'Files are ok ',10,13,0  
  191.   ds.w     0
  192. nofile:
  193.   dc.b     'Unable to open input file',10,13,0
  194.   ds.w     0
  195. nobytes:
  196.   dc.b     'Unable to read input file',10,13,0
  197.   ds.w      0
  198. nofile2:
  199.   dc.b     'Unable to open output file',10,13,0
  200.   ds.w      0
  201.  END 
  202.    
  203.