home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol8n05.zip / TYPEFILE.ASM < prev   
Assembly Source File  |  1988-12-12  |  6KB  |  158 lines

  1. ;  TYPEFILE.ASM -- Simple demo of RFILE subroutine (MS-DOS version).  
  2. ;                  Displays text file on standard output device.
  3. ;  Copyright (c) 1989 Ziff Communications Co.
  4. ;  PC Magazine * Ray Duncan
  5. ;  Requires: RFILE.ASM, STRINGS1.ASM, ARGV.ASM, ARGC.ASM.
  6. ;
  7. ;  Build:       MASM TYPEFILE;
  8. ;               MASM RFILE;
  9. ;               MASM STRINGS1;
  10. ;               MASM ARGC;
  11. ;               MASM ARGV;
  12. ;               LINK TYPEFILE+RFILE+STRINGS1+ARGC+ARGV;
  13. ;
  14. ;  Usage:       TYPEFILE filename.ext
  15.  
  16. cr      equ     0dh             ; ASCII carriage return
  17. lf      equ     0ah             ; ASCII line feed
  18. blank   equ     20h             ; ASCII space code
  19.  
  20. blksize equ     256             ; size of input file buffer     
  21.  
  22. cmd     equ     80h             ; buffer for command tail
  23.  
  24. stdin   equ     0               ; standard input handle
  25. stdout  equ     1               ; standard output handle
  26. stderr  equ     2               ; standard error handle
  27.  
  28. DGROUP  group   _DATA,STACK
  29.  
  30. _TEXT   segment word public 'CODE'
  31.  
  32.         extrn   argc:near       ; external subroutines
  33.         extrn   argv:near
  34.         extrn   rline:near
  35.  
  36.         assume  cs:_TEXT,ds:DGROUP,es:NOTHING,ss:STACK
  37.  
  38. main    proc    far             ; entry point from MS-DOS
  39.  
  40.         mov     ax,DGROUP       ; make our data segment
  41.         mov     ds,ax           ; addressable via DS
  42.                                 ; (leave ES = PSP segment)
  43.  
  44.                                 ; check if filename present
  45.         mov     bx,offset cmd   ; ES:BX = command tail
  46.         call    argc            ; count command arguments
  47.         cmp     ax,2            ; are there 2 arguments?
  48.         je      main1           ; yes, proceed
  49.  
  50.                                 ; missing filename, display
  51.                                 ; error message and exit
  52.         mov     dx,offset msg2  ; DS:DX = message address
  53.         mov     cx,msg2_len     ; CX = message length
  54.         jmp     main5           ; go display it
  55.  
  56. main1:                          ; get address of filename
  57.         mov     ax,1            ; AX = argument number
  58.                                 ; ES:BX still = command tail
  59.         call    argv            ; returns ES:BX = address,
  60.                                 ; and AX = length
  61.  
  62.         mov     di,offset fname ; copy filename to buffer
  63.         mov     cx,ax           ; CX = length
  64.  
  65. main2:  mov     al,es:[bx]      ; copy one byte
  66.         mov     [di],al
  67.         inc     bx              ; bump string pointers
  68.         inc     di
  69.         loop    main2           ; loop until string done
  70.         mov     byte ptr [di],0 ; add terminal null byte
  71.  
  72.         push    ds              ; set ES = DGROUP too
  73.         pop     es
  74.         assume  es:DGROUP
  75.  
  76.                                 ; now open the file...
  77.         mov     ax,3d00h        ; Fxn 3DH = open file
  78.                                 ; mode 0 = read only
  79.         mov     dx,offset fname ; DS:DX = filename
  80.         int     21h             ; transfer to MS-DOS
  81.         mov     fhandle,ax      ; save file handle
  82.         jnc     main3           ; jump, open successful
  83.  
  84.                                 ; file doesn't exist
  85.         mov     dx,offset msg1  ; DS:DX = error message
  86.         mov     cx,msg1_len     ; CX = message length
  87.         jmp     main5           ; go display it
  88.  
  89. main3:                          ; read line from file
  90.         mov     bx,fhandle      ; BX = file handle
  91.         mov     cx,blksize      ; CX = buffer length
  92.         mov     dx,offset fbuff ; DS:DX = buffer
  93.         call    rline
  94.         or      ax,ax           ; reached end of file?
  95.         jz      main4           ; yes, exit
  96.  
  97.                                 ; otherwise display line
  98.         mov     cx,ax           ; line length
  99.         mov     bx,stdout       ; standard output handle
  100.         mov     ah,40h          ; Function 40H = write
  101.         int     21h             ; transfer to MS-DOS
  102.  
  103.         jmp     main3           ; get another line
  104.  
  105. main4:                          ; success exit point
  106.         mov     bx,fhandle      ; BX = file handle
  107.         mov     ah,3eh          ; Fxn 3EH = close
  108.         int     21h             ; transfer to MS-DOS
  109.  
  110.         mov     ax,4c00h        ; Fxn 4CH = terminate,
  111.                                 ; return code = 0
  112.         int     21h             ; transfer to MS-DOS
  113.  
  114. main5:                          ; common error exit
  115.                                 ; DS:DX = message address
  116.                                 ; CX = message length
  117.         mov     bx,stderr       ; standard error handle
  118.         mov     ah,40h          ; Fxn 40H = write
  119.         int     21h             ; transfer to MS-DOS
  120.  
  121.         mov     ax,4c01h        ; Fxn 4CH = terminate,
  122.                                 ; return code = 1
  123.         int     21h             ; transfer to MS-DOS
  124.         
  125. main    endp
  126.  
  127.  
  128. _TEXT    ends
  129.  
  130.  
  131. _DATA   segment word public 'DATA'
  132.  
  133. fname   db      64 dup (0)      ; buffer for input filespec
  134.  
  135. fhandle dw      0               ; input file handle
  136.  
  137. fbuff   db      blksize dup (?) ; data from input file
  138.  
  139. msg1    db      cr,lf
  140.         db      'typefile: file not found'
  141.         db      cr,lf
  142. msg1_len equ    $-msg1
  143.  
  144. msg2    db      cr,lf
  145.         db      'typefile: missing filename'
  146.         db      cr,lf
  147. msg2_len equ    $-msg2
  148.  
  149. _DATA   ends    
  150.  
  151.  
  152. STACK   segment para stack 'STACK'
  153.  
  154.         db      64 dup (?)
  155.  
  156. STACK   ends
  157.         end     main
  158.