home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH15 / FIND.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-03-25  |  3.0 KB  |  160 lines

  1. ; Find.asm
  2. ;
  3. ; This program opens a file specified on the command line and searches for
  4. ; a string (also specified on the command line).
  5. ;
  6. ; Program Usage:
  7. ;
  8. ;    find "string" filename
  9.  
  10.  
  11.         .xlist
  12.         include     stdlib.a
  13.         includelib    stdlib.lib
  14.         .list
  15.  
  16. wp        textequ    <word ptr>
  17.  
  18. dseg        segment    para public 'data'
  19.  
  20. StrPtr        dword    ?
  21. FileName    dword    ?
  22. LineCnt        dword    ?
  23.  
  24. FVar        filevar    {}
  25.  
  26. InputLine    byte    1024 dup (?)
  27. dseg        ends
  28.  
  29.  
  30. cseg        segment    para public 'code'
  31.         assume    cs:cseg, ds:dseg
  32.  
  33.  
  34. ; Readln-    This procedure reads a line of text from the input
  35. ;        file and buffers it up in the "InputLine" array.
  36.  
  37. ReadLn        proc
  38.         push    es
  39.         push    ax
  40.         push    di
  41.         push    bx
  42.  
  43.         lesi    FVar        ;Read from our file.
  44.         mov    bx, 0        ;Index into InputLine.
  45. ReadLp:        fgetc            ;Get next char from file.
  46.         jc    EndRead        ;Quit on EOF
  47.  
  48.         cmp    al, cr        ;Ignore carriage returns.
  49.         je    ReadLp
  50.         cmp    al, lf        ;End of line on line feed.
  51.         je    EndRead
  52.  
  53.         mov    InputLine[bx], al
  54.         inc    bx
  55.         jmp    ReadLp
  56.  
  57. ; If we hit the end of a line or the end of the file,
  58. ; zero-terminate the string.
  59.  
  60. EndRead:    mov    InputLine[bx], 0
  61.         pop    bx
  62.         pop    di
  63.         pop    ax
  64.         pop    es
  65.         ret
  66. ReadLn        endp
  67.  
  68.  
  69. ; The following main program extracts the search string and the
  70. ; filename from the command line, opens the file, and then searches
  71. ; for the string in that file.
  72.  
  73. Main        proc
  74.         mov    ax, dseg
  75.         mov    ds, ax
  76.         mov    es, ax
  77.         meminit
  78.  
  79.         argc
  80.         cmp    cx, 2
  81.         je    GoodArgs
  82.         print
  83.         byte    "Usage: find 'string' filename",cr,lf,0
  84.         jmp    Quit
  85.  
  86. GoodArgs:    mov    ax, 1        ;Get the string to search for
  87.         argv            ; off the command line.
  88.         mov    wp StrPtr, di
  89.         mov    wp StrPtr+2, es
  90.  
  91.         mov    ax, 2        ;Get the filename from the
  92.         argv            ; command line.
  93.         mov    wp Filename, di
  94.         mov    wp Filename+2, es
  95.  
  96. ; Open the input file for reading
  97.  
  98.         mov    ax, 0        ;Open for read.
  99.         mov    si, wp FileName
  100.         mov    dx, wp FileName+2
  101.         lesi    Fvar
  102.         fopen
  103.         jc    BadOpen
  104.  
  105. ; Okay, start searching for the string in the file.
  106.  
  107.         mov    wp LineCnt, 0
  108.         mov    wp LineCnt+2, 0
  109. SearchLp:    call    ReadLn
  110.         jc    AtEOF
  111.  
  112.  
  113. ; Bump the line number up by one.  Note that this is 8086 code
  114. ; so we have to use extended precision arithmetic to do a 32-bit
  115. ; add.  LineCnt is a 32-bit variable because some files have more
  116. ; that 65,536 lines.
  117.  
  118.         add    wp LineCnt, 1
  119.         adc    wp LineCnt+2, 0
  120.  
  121. ; Search for the user-specified string on the current line.
  122.  
  123.         lesi    InputLine
  124.         mov    dx, wp StrPtr+2
  125.         mov    si, wp StrPtr
  126.         strstr
  127.         jc    SearchLp    ;Jump if not found.
  128.  
  129. ; Print an appropriate message if we found the string.
  130.  
  131.         printf
  132.         byte    "Found '%^s' at line %ld\n",0
  133.         dword    StrPtr, LineCnt
  134.         jmp    SearchLp
  135.  
  136. ; Close the file when we're done.
  137.  
  138. AtEOF:        lesi    FVar
  139.         fclose
  140.         jmp    Quit
  141.  
  142. BadOpen:    printf
  143.         byte    "Error attempting to open %^s\n",cr,lf,0
  144.         dword    FileName
  145.  
  146.  
  147. Quit:        ExitPgm            ;DOS macro to quit program.
  148. Main        endp
  149.  
  150. cseg        ends
  151.  
  152. sseg        segment    para stack 'stack'
  153. stk        db    1024 dup ("stack   ")
  154. sseg        ends
  155.  
  156. zzzzzzseg    segment    para public 'zzzzzz'
  157. LastBytes    db    16 dup (?)
  158. zzzzzzseg    ends
  159.         end    Main
  160.