home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PPOS2.ZIP / TRYBSRCH.ASM < prev    next >
Assembly Source File  |  1988-12-01  |  9KB  |  228 lines

  1.         title   TRYBSRCH Demo of BSEARCH routine
  2.         page    55,132
  3.         .286
  4.  
  5. ; TRYBSRCH.ASM  Demo of BSEARCH routine (OS/2 version),
  6. ;               searches file previously created by MAKENIF.EXE.        
  7. ; by Ray Duncan, Copyright (C) 1988 Ziff Davis Communications
  8. ;
  9. ; The user is prompted to enter a search key.  The first
  10. ; 8 characters of the key are used to search TESTFILE.DAT,
  11. ; using the binary search algorithm.  The success or failure 
  12. ; of the search is reported along with the record number (if found).
  13. ;
  14. ; Each record in TESTFILE.DAT is RSIZE bytes long.  Bytes 
  15. ; 0 to (KSIZE-1) of the record are the ASCII key for searches
  16. ; by BSEARCH.  Bytes KSIZE to (RSIZE-1) of the record
  17. ; are initialized to zero and are not used.  RSIZE, KSIZE,
  18. ; and the key offset within the record must be kept synchronized
  19. ; with MAKENIF.C.
  20. ;
  21. ; To build TRYBSRCH.EXE you also need TRYBSRCH.DEF, BSEARCH.ASM, 
  22. ; ITOA.ASM, and STRINGS1.ASM.  Enter the following commands:
  23. ;
  24. ;       MASM TRYBSRCH;
  25. ;       MASM BSEARCH;
  26. ;       MASM ITOA;
  27. ;       MASM STRINGS1;
  28. ;       LINK TRYBSRCH+BSEARCH+ITOA+STRINGS1,,,OS2,TRYBSRCH.DEF;
  29.  
  30. stdin   equ     0                       ; standard input handle
  31. stdout  equ     1                       ; standard output handle
  32.  
  33. cr      equ     0dh                     ; ASCII carriage return
  34. lf      equ     0ah                     ; ASCII line feed
  35. blank   equ     20h                     ; ASCII blank
  36.  
  37. rsize   equ     64                      ; TESTFILE.DAT record size
  38. ksize   equ     8                       ; TESTFILE.DAT key size
  39. koffs   equ     0                       ; offset of key within record
  40.  
  41.         extrn   DosChgFilePtr:far       ; references to OS/2 API
  42.         extrn   DosClose:far
  43.         extrn   DosExit:far
  44.         extrn   DosOpen:far
  45.         extrn   DosRead:far
  46.         extrn   DosWrite:far
  47.  
  48. DGROUP  group   _DATA
  49.  
  50. _DATA   segment word public 'DATA'
  51.  
  52. fname   db      'TESTFILE.DAT',0        ; file created by MAKENIF.C
  53. fhandle dw      ?                       ; handle for TESTFILE.DAT
  54. faction dw      ?                       ; receives DosOpen action
  55. frecs   dw      ?                       ; records in file
  56. fbuff   db      rsize dup (0)           ; file record buffer
  57. fsize   dd      ?                       ; filesize in bytes
  58.  
  59. key     dw      ksize                   ; length of key data
  60.         dw      koffs                   ; offset of key within record
  61. kval    db      80 dup (0)              ; actual key data
  62.  
  63. rlen    dw      ?                       ; receives length from DosRead
  64. wlen    dw      ?                       ; receives length from DosWrite
  65.  
  66. msg1    db      cr,lf
  67.         db      'Can''t open TESTFILE.DAT'
  68.         db      cr,lf
  69. msg1_len equ $-msg1
  70.  
  71. msg2    db      cr,lf
  72.         db      'Enter search key: '
  73. msg2_len equ $-msg2
  74.  
  75. msg3    db      cr,lf
  76.         db      'Record number is: '
  77. msg3a   db      6 dup (blank)
  78.         db      cr,lf
  79. msg3_len equ $-msg3
  80.  
  81. msg4    db      cr,lf
  82.         db      'Record not found'
  83.         db      cr,lf
  84. msg4_len equ $-msg4
  85.  
  86. _DATA   ends
  87.  
  88.  
  89. _TEXT   segment word public 'CODE'
  90.  
  91.         assume  cs:_TEXT,ds:_DATA
  92.  
  93.         extrn   itoa:near
  94.         extrn   bsearch:near
  95.  
  96. main    proc    near
  97.  
  98.         push    ds                      ; let ES point to DGROUP too
  99.         pop     es
  100.  
  101.         cld                             ; string ops safety first
  102.  
  103.                                         ; open the file TESTFILE.DAT...
  104.         push    ds                      ; address of filename
  105.         push    offset DGROUP:fname
  106.         push    ds                      ; receives file handle
  107.         push    offset DGROUP:fhandle
  108.         push    ds                      ; receives DosOpen action
  109.         push    offset DGROUP:faction
  110.         push    0                       ; file allocation (N/A)
  111.         push    0
  112.         push    0                       ; file attribute (N/A)
  113.         push    1                       ; open only if already exists
  114.         push    20h                     ; read-only, deny write
  115.         push    0                       ; reserved DWORD 0
  116.         push    0
  117.         call    DosOpen                 ; transfer to OS/2
  118.         or      ax,ax                   ; open successful?
  119.         jz      main1                   ; yes, jump
  120.  
  121.                                         ; open failed, display error
  122.                                         ; message and exit...
  123.         push    stdout                  ; standard output handle
  124.         push    ds                      ; message address
  125.         push    offset DGROUP:msg1
  126.         push    msg1_len                ; message length
  127.         push    ds                      ; receives actual bytes writen
  128.         push    offset DGROUP:wlen
  129.         call    DosWrite                ; transfer to OS/2
  130.         jmp     main4                   ; go perform final exit
  131.  
  132. main1:                                  ; find filesize in bytes...
  133.         push    fhandle                 ; file handle
  134.         push    0                       ; relative offset = 0
  135.         push    0
  136.         push    2                       ; method = rel. to end of file
  137.         push    ds                      ; receives file size
  138.         push    offset DGROUP:fsize
  139.         call    DosChgFilePtr           ; transfer to OS/2
  140.         
  141.         mov     ax,word ptr fsize       ; calculate number of records
  142.         mov     dx,word ptr fsize+2
  143.         mov     bx,rsize                ; filesize / bytes per record
  144.         div     bx                      ; = records in file
  145.         mov     frecs,ax
  146.         
  147.                                         ; display "Enter search key: "
  148. main2:  push    stdout                  ; standard output handle
  149.         push    ds                      ; message address
  150.         push    offset DGROUP:msg2
  151.         push    msg2_len                ; message length
  152.         push    ds                      ; receives actual bytes written
  153.         push    offset DGROUP:wlen
  154.         call    DosWrite                ; transfer to OS/2
  155.  
  156.         mov     cx,ksize                ; zero out previous key
  157.         mov     di,offset kval
  158.         xor     al,al
  159.         rep stosb
  160.  
  161.         mov     cx,6                    ; remove previous record
  162.         mov     di,offset msg3a         ; number from output string
  163.         mov     al,blank
  164.         rep stosb
  165.  
  166.                                         ; get search key from user...
  167.         push    stdin                   ; standard input handle
  168.         push    ds                      ; input buffer address
  169.         push    offset DGROUP:kval
  170.         push    80                      ; maximum input length
  171.         push    ds                      ; receives actual input length
  172.         push    offset DGROUP:rlen
  173.         call    DosRead
  174.  
  175.         cmp     rlen,2                  ; was anything entered?
  176.         je      main4                   ; empty line, exit
  177.  
  178.         mov     bx,rlen                 ; remove CR-LF from input
  179.         mov     word ptr [bx+kval-2],0
  180.  
  181.                                         ; set up for binary search
  182.         mov     bx,fhandle              ; file handle
  183.         mov     cx,rsize                ; record size   
  184.         mov     dx,offset DGROUP:fbuff  ; record buffer
  185.         mov     si,0                    ; first (left) record
  186.         mov     di,frecs                ; last (right) record
  187.         dec     di
  188.         mov     bp,offset DGROUP:key    ; key structure address
  189.         call    bsearch                 ; call search routine
  190.         jnz     main3                   ; jump if record not found
  191.  
  192.         mov     si,offset DGROUP:msg3a  ; convert record number
  193.         mov     cx,10                   ; to ASCII and store in output
  194.         call    itoa
  195.  
  196.                                         ; display 'record number is nnnn'
  197.         push    stdout                  ; standard output handle
  198.         push    ds                      ; message address
  199.         push    offset DGROUP:msg3
  200.         push    msg3_len                ; message length
  201.         push    ds                      ; receives actual bytes written
  202.         push    offset DGROUP:wlen
  203.         call    DosWrite                ; transfer to OS/2
  204.  
  205.         jmp     main2                   ; get another search key
  206.  
  207. main3:                                  ; display 'record not found'
  208.         push    stdout                  ; standard output handle
  209.         push    ds                      ; message address
  210.         push    offset DGROUP:msg4
  211.         push    msg4_len                ; message length
  212.         push    ds                      ; receives actual bytes written
  213.         push    offset DGROUP:wlen
  214.         call    DosWrite                ; transfer to OS/2
  215.  
  216.         jmp     main2                   ; get another search key
  217.  
  218. main4:  push    1                       ; terminate all threads
  219.         push    0                       ; return code
  220.         call    DosExit                 ; transfer to OS/2
  221.  
  222. main    endp
  223.  
  224. _TEXT   ends
  225.  
  226.         end     main
  227.  
  228.