home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / books / 68k_book / arp_src / create.s < prev    next >
Text File  |  1985-11-20  |  4KB  |  96 lines

  1.  ; Program Name: CREATE.S
  2.  ;      Version: 1.001
  3.  
  4.  ; Assembly Instructions:
  5.  
  6.  ;    Assemble in PC-relative mode and save with a TOS suffix.
  7.  
  8.  ; Execution Instructions:
  9.  
  10.  ;    This program is spawned by PRG_8BR.ACC.  Both programs must reside in
  11.  ; the same directory.
  12.  
  13.  ; Function:
  14.  
  15.  ;    This program creates disk file E:\PRG_8\PRG_8BR.DAT and stores therein
  16.  ; the data contained in PRG_8BR.ACC's buffer.  The purpose of this exercise
  17.  ; is to illustrate an elegant method of creating a disk file for a desk
  18.  ; accessory, writing data to the file and invoking proper file closure.
  19.  
  20. fetch_command_line_address:
  21.  lea        -$82(pc), a3
  22.  
  23.  ; The data in the command line is a string of eight ASCII hexadecimal digits
  24.  ; passed from PRG_8CR.ACC when that program spawned this one.  There are no
  25.  ; characters before that string and none following.  The string is the address
  26.  ; of PRG_8CR.ACC's buffer, in which data that is to be written to a disk file
  27.  ; has been stored.  The address must be converted from an ASCII hexadecimal
  28.  ; string to a binary longword.
  29.  
  30. process_command_line:            ; Convert ASCII hexadecimal string to binary.
  31.  moveq.l    #0, d0               ; Accumulator.
  32.  moveq.l    #7, d2               ; Loop counter.
  33. convert_string:
  34.  move.b     (a3)+, d1            ; D1 is used for each character conversion.
  35.  cmpi.b     #$39, d1             ; Character greater than 9 test.
  36.  bgt        not_decimal
  37.  cmpi.b     #$30, d1             ; Character less than 0 test.
  38.  blt        not_valid
  39.  subi.b     #$30, d1             ; Convert ASCII hex character to binary.
  40.  bra.s      accumulate
  41. not_decimal:
  42.  cmpi.b     #$41, d1             ; Character less than A test.
  43.  blt        not_valid
  44.  cmpi.b     #$46, d1             ; Character greater than F test.
  45.  bgt        not_valid
  46.  subi.b     #$37, d1             ; Convert ASCII hex character to binary.
  47. accumulate:
  48.  lsl.l      #4, d0               ; Shift one nibble = one hex character.
  49.  add.b      d1, d0
  50.  dbra       d2, convert_string
  51.  move.l     d0, a3               ; Address of buffer is in D0.
  52.  movea.l    a3, a0               ; Copy for buffer byte count.
  53.  
  54. count_bytes_in_buffer:
  55.  moveq.l    #0, d0               ; NULL byte test register.
  56.  moveq.l    #0, d3               ; Byte count accumulator.
  57. count:
  58.  move.b     (a0)+, d0            ; Look at each character in buffer and
  59.  beq        create_file          ; accumulate count until NULL is found at
  60.  addq.l     #1, d3               ; end of buffer.
  61.  bra.s      count
  62.  
  63. create_file:                     ; COMPUTE! TOS book page 270.
  64.  move.w     #0, -(sp)            ; File attribute = read/write.
  65.  pea        filename(pc)
  66.  move.w     #$3C, -(sp)          ; Function = f_create = GEMDOS $3C.
  67.  trap       #1                   ; File handle is returned in D0.
  68.  addq.l     #8, sp
  69.  move.w     d0, d4               ; Save file handle in D4.
  70.  
  71. write_buffer_to_file:            ; Function = f_write.  COMPUTE! TOS p.274.
  72.  pea        (a3)                 ; Push buffer's address.
  73.  move.l     d3, -(sp)            ; Push byte count length.
  74.  move.w     d4, -(sp)            ; COMPUTE!'s TOS book incorrectly specifies
  75.  move.w     #$40, -(sp)          ; a longword operation here; see page 274.
  76.  trap       #1
  77.  lea        $C(sp), sp
  78.  
  79. close_output_file:               ; COMPUTE! TOS book page 272.
  80.  move.w     d4, -(sp)            ; Push file handle. 
  81.  move.w     #$3E, -(sp)          ; Function = GEMDOS $3E = f_close.
  82.  trap       #1
  83.  addq.l     #4, sp
  84.  
  85. not_valid:
  86. terminate:
  87.  move.w     #0, -(sp)
  88.  trap       #1
  89.  
  90.  data
  91. filename:     dc.b  'E:\PRG_8\PRG_8BR.DAT',0
  92.  align
  93.  bss
  94. program_end:  ds.l  0
  95.  end
  96.