home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / SRCR215.ZIP / DUMP.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-03-30  |  13.4 KB  |  352 lines

  1.  name dump
  2.  page 78,132
  3.  title DUMP--Display File Contents
  4.  
  5.  ;DUMP--a utility to display the contents
  6.  ;of a file in hex and ASCII format. MS-DOS 2.00
  7.  
  8.  ;Used in the form:
  9.  ;A>dump path\filename.ext[>device]
  10.  ;(item in square bracket is optional)
  11.  
  12.  ;version 1.0 March 25, 1984
  13.  ;copyright (c) 1984 by Ray Duncan
  14.  ;may be freely reproduced for noncommercial use
  15.  
  16.                  cr      equ 0dh ;ASCII carriage return
  17.                  lf      equ 0ah ;ASCII linefeed
  18.                  blank   equ 20h ;ASCII space
  19.                  adr     equ 65535
  20.                  command equ 80h ;buffer for command tail
  21.                  blksize equ 512 ;size of inputfile
  22.                  output_handle equ 1 ;handle of standard output
  23.                                      ;device(redirectable)
  24.                  error_handle equ 2  ;handle of standard error device (not
  25.                                      ;redirectable)
  26.  
  27.  
  28.  cseg  segment para public 'CODE'
  29.  
  30.  assume cs:cseg,ds:data,es:data,ss:stack
  31.  
  32.  include dump.pub
  33.  
  34.  dump            proc    far     ;entry point from DOS
  35.                  push    ds      ;save DS:0000 for final
  36.                  xor     ax,ax   ;return to MS-DOS
  37.                  push    ax
  38.                  mov     ax,data ;make our data segment
  39.                  mov     es,ax   ;addressable via ES
  40.                  mov     ah,30h  ;check version DOS
  41.                  int     21h
  42.                  cmp     al,2
  43.                  jae     dump1   ;proceed if DOS 2 or greater
  44.                  mov     dx,offset msg3 ;DOS 1.x-print error msg
  45.                  mov     ax,es   ;we must use old DOS
  46.                  mov     ds,ax   ;string output function
  47.                  mov     ah,9    ;since handles not
  48.                  int     21h     ;avail this vers DOS
  49.                  ret
  50.  
  51.  
  52. dump1:           call    get_filename   ;get path and filespec for input
  53.                                  ;file from command line tail
  54.                  mov     ax,es   ;Set DS=ES for remainder
  55.                  mov     ds,ax   ;of program
  56.                  jnc     dump2   ;jump got acceptable name
  57.                  mov     dx,offset msg2 ;missing or illegal
  58.                  mov     cx,msg2_length ;filespec
  59.                  jmp     dump9   ;print error and exit
  60.  
  61.  dump2:          call    open_input     ;now try to open input
  62.                  jnc     dump3   ;file-jump opened ok
  63.                  mov     dx,offset msg1 ;open input file failed
  64.                  mov     cx,msg1_length
  65.                  jmp     dump9   ;print error and exit
  66.  
  67.  dump3:          call    read_block     ;initialize input buffer
  68.                  jnc     dump4   ;jump, got a block
  69.                  mov     dx,offset msg4 ;empty file,print error
  70.                  mov     cx,msg4_length ;message and exit
  71.                  jmp     dump9
  72.  
  73.  dump4:          call    get_char       ;file successfully opened
  74.                  jc      dump8          ;now convert and display it.
  75.                  inc     input_addr     ;read 1 char from input
  76.                                         ;jump, if end of file
  77.                                         ;update file position
  78.                  or      bx,bx          ;is this 1st char blk
  79.                  jnz     dump5          ;no
  80.                  call    print_heading  ;yes
  81.  
  82.  dump5:          and     bx,0fh  ;is this 1st byte of 16?
  83.                  jnz     dump6   ;no,jump
  84.                  push    ax      ;save the byte
  85.                  mov     di,offset output ;convert relative file addr
  86.                  mov     ax,input_addr  ;for output string
  87.                  call    conv_word
  88.                  pop     ax
  89.  
  90.  dump6:                          ;store ascii vers of
  91.                                  ;char if it is alphanumeric
  92.                  mov     di,offset outputb
  93.                  add     di,bx   ;calculate output string
  94.                  mov     byte ptr [di],'.' ;addr if it is control
  95.                  cmp     al,blank ;char,just print dot
  96.                  jb      dump7   ;jump, not alphanumeric
  97.                  cmp     al,7eh
  98.                  ja      dump7   ;jump,not alphanumeric
  99.                  mov     [di],al ;store ascii char
  100.  
  101.  dump7:                          ;now convert binary byte
  102.                                  ;to hex ascii equivalent
  103.                  push    bx      ;save offset 0-15 this
  104.                                  ;byte calculates its posn in
  105.                                  ;output string
  106.                  mov     di,offset outputa
  107.                  add     di,bx   ;base addr + offset*3
  108.                  add     di,bx
  109.                  add     di,bx
  110.                  call    conv_byte ;conv data byte to hex
  111.                  pop     bx      ;restore byte offset
  112.                  cmp     bx,0fh  ;16 bytes converted yet
  113.                  jne     dump4   ;no,get another byte
  114.                  mov     dx,offset output
  115.                  mov     cx,output_length
  116.                  call    write_std      ;yes,print line
  117.                  jmp     dump4   ;get next char from
  118.                                  ;input file
  119.  
  120.  dump8:          call    close_input    ;end of file detected
  121.                  ret                    ;close input file
  122.  
  123.  dump9:          call    write_error    ;come here to print msg
  124.                  ret                    ;on std error device
  125.                                         ;and return cntrl to DOS
  126.  
  127.                  dump    endp
  128.  
  129.  get_filename    proc    near    ;process name of input file
  130.                                  ;return carry=0 if successful
  131.                                  ;return carry=1 if unsuccessful
  132.                                  ;if no filename DS:SI<-addr command line
  133.                  mov     si,offset command
  134.                                  ;ES:DI<-addr filespec buffer
  135.                  mov     di,offset input_name
  136.                  cld
  137.                  lodsb           ;any command line present?
  138.                  or      al,al   ;return error status if not
  139.                  jz      get_filename4
  140.  
  141.  get_filename1:                  ;scan over leading blank
  142.                  lodsb           ;to filename
  143.                  cmp     al,cr   ;if we hit return
  144.                  je      get_filename4 ;yes,exit with code
  145.                  cmp     al,20h  ;is this a blank
  146.          je    get_filename1    ;if not keep moving
  147.  
  148.  get_filename2:                  ;found 1st char of name
  149.                  stosb           ;move last char
  150.                  lodsb           ;check next char, found
  151.                  cmp     al,cr   ;cr yet?
  152.                  je      get_filename3 ;yes,exit with success
  153.                  cmp     al,20h  ;code. is this a blank?
  154.                  jne     get_filename2 ;if not keep moving char
  155.  
  156.  get_filename3:  clc             ;exit with carry=0
  157.                  ret             ;for success flag
  158.  
  159.  
  160.  get_filename4:  stc             ;exit with carry=1
  161.                  ret             ;for error flag
  162.  
  163.  
  164.  get_filename    endp
  165.  
  166.  open_input      proc    near    ;open input file
  167.                                  ;DS:DX=addr filename
  168.                  mov     dx,offset input_name
  169.                  mov     al,0    ;AL=0 for read only
  170.                  mov     ah,3dh  ;function 3dh = open
  171.                  int     21h     ;handle returned in AX
  172.                  mov     input_handle,ax ;save it for later
  173.                  ret             ;CY is set if error
  174.  
  175.  open_input      endp
  176.  
  177.  close_input     proc    near    ;close input file
  178.                  mov     bx,input_handle ;BX=handle
  179.                  mov     ah,3eh
  180.                  int     21h
  181.                  ret
  182.  close_input     endp
  183.  
  184.  get_char        proc    near    ;get one char from
  185.                                  ;input buffer
  186.                  mov     bx,input_ptr   ;return AL=char BX=buffer
  187.                  cmp     bx,blksize     ;offset. CY=1 if eof
  188.                  jne     get_char1      ;is ptr at end of buffer
  189.                  mov     input_ptr,0
  190.                  call    read_block     ;new block read from disk
  191.                  jnc     get_char       ;got blk start routine
  192.                  ret                    ;over
  193.  
  194.  get_char1:                      ;get data byte to AL
  195.                  mov     al,[input_buffer+bx]
  196.                  inc     input_ptr      ;bump input buffer ptr
  197.                  clc             ;return CY flag=0 since
  198.                  ret             ;not end of file
  199.  get_char        endp
  200.  
  201.  read_block      proc    near    ;read block of data from
  202.                  mov     bx,input_handle ;input file CY=0 if ok
  203.                  mov     cx,blksize      ;CY=1 if end of file
  204.                  mov     dx,offset input_buffer ;request read
  205.                  mov     ah,3fh         ;from D
  206.                  int     21h
  207.                  inc     input_block    ;initialize pointers
  208.                  mov     input_ptr,0
  209.                  or      ax,ax   ;was anything read in?
  210.                                  ;or turns off CY flag
  211.  
  212.                  jnz     read_block1
  213.  
  214.                  stc             ;no, eof return CY=true
  215.  
  216.  read_block1:
  217.          ret
  218.  
  219.  read_block      endp
  220.  
  221.  write_std       proc    near    ;write string to std
  222.                                  ;output call DX=addr
  223.                                  ;of out string CX=lgth
  224.                  mov     bx,output_handle ;of string
  225.                  mov     ah,40h  ;function 40=write to file
  226.                  int     21h     ;or device function call.
  227.                  ret
  228.  write_std       endp
  229.  
  230.  write_error     proc    near    ;write str to std error
  231.                                  ;device  call DX=addr
  232.                                  ;of output string
  233.                                  ;CX=length of string
  234.                  mov     bx,error_handle ;BX=handle for std error
  235.                  mov     ah,40h  ;device funct 40h=write
  236.                  int     21h     ;to device
  237.                  ret
  238.  write_error     endp
  239.  
  240.  print_heading   proc    near
  241.                  push    ax      ;print record nr and
  242.                  push    bx      ;heading for block
  243.                  mov     di,offset headinga ;of data 1st save reg
  244.                  mov     ax,input_block
  245.                  call    conv_word      ;convert record nr to
  246.                  mov     dx,offset heading ;ascii
  247.                  mov     cx,heading_length
  248.                  call    write_std      ;now print heading
  249.                  pop     bx
  250.                  pop     ax      ;restore registers
  251.                  ret             ;and exit
  252.  print_heading   endp
  253.  
  254.  conv_word       proc    near    ;conv 16 bit word to hex
  255.                                  ;call with ax=binary val
  256.                                  ;DI=addr to store string
  257.                                  ;AX,DI,CX destroyed
  258.                  push    ax
  259.                  mov     al,ah
  260.                  call    conv_byte ;convert upper byte
  261.                  pop     ax
  262.                  call    conv_byte ;convert lower byte
  263.                  ret
  264.  conv_word       endp
  265.  
  266.  conv_byte       proc    near    ;convert binary byte
  267.                                  ;to hex ascii
  268.                                  ;call with al=binary
  269.                                  ;DI=addr to store str
  270.                  sub     ah,ah   ;clear upper byte
  271.                  mov     cl,16
  272.                  div     cl      ;div binary by 16
  273.                  call    ascii   ;remainder becomes
  274.                  stosb           ;second ascii char
  275.                  mov     al,ah
  276.                  call    ascii
  277.                  stosb
  278.                  ret
  279.  conv_byte       endp
  280.  
  281.  ascii           proc    near    ;conv last 4 bits inAL
  282.                  add     al,'0'
  283.                  cmp     al,'9'
  284.                  jle     ascii2  ;jump if range 0-9
  285.                  add     al,'A'-'9'-1 ;offset to range A-F
  286.  ascii2:         ret             ;return ascii char AL
  287.  ascii           endp
  288.  
  289.  cseg            ends
  290.  
  291.  data  segment para public 'DATA'
  292.  
  293.  input_name      db      64 dup (0)     ;buffer for input file
  294.  
  295.  input_handle    dw      0              ;token for dos input
  296.  
  297.  input_ptr       dw      0       ;ptr to input deblocking
  298.  
  299.  input_addr      dw      adr     ;relative addr in file
  300.  
  301.  input_block     dw      0       ;current in block nr.
  302.  
  303.  output          db      'nnnn',blank,blank
  304.  
  305.  outputa         db      16 dup ('00',blank)
  306.                  db      blank
  307.  
  308.  outputb         db      '0123456789ABCDEF',cr,lf
  309.  
  310.  output_length   equ     $-output
  311.  
  312.  heading         db      cr,lf,'RECORD',blank
  313.  
  314.  headinga        db      'nnnn',blank,blank,cr,lf
  315.                  db      7 dup(blank)
  316.                  db      '0  1  2  3  4  5  6  7  '
  317.                  db      '8  9  A  B  C  D  E  F',cr,lf
  318.  
  319.  heading_length  equ     $-heading
  320.  
  321.  input_buffer    db      blksize dup(?)
  322.  
  323.  msg1            db      cr,lf
  324.                  db      'Cannot find input file'
  325.                  db      cr,lf
  326.  
  327.  msg1_length     equ     $-msg1
  328.  
  329.  msg2            db      cr,lf
  330.                  db      'Missing file name.'
  331.                  db      cr,lf
  332.  
  333.  msg2_length     equ     $-msg2
  334.  
  335.  msg3            db      cr,lf
  336.                  db      'Requires MS-DOS version 2 or greater'
  337.                  db      cr,lf,'$'
  338.  
  339.  msg4            db      cr,lf,'Empty File.',cr,lf
  340.  
  341.  msg4_length     equ     $-msg4
  342.  
  343.  data            ends
  344.  
  345.  stack  segment para stack 'STACK'
  346.  
  347.                  db      64 dup(?)
  348.  
  349.  stack           ends
  350.  
  351.                  end dump
  352.