home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / BBS_UTIL / BM0406_A.ZIP / BMASM.ZIP / FINDWORD.ASM < prev    next >
Assembly Source File  |  1992-08-01  |  4KB  |  149 lines

  1. comment @
  2.  
  3.   Changes made by Scott McNay (1:395/11) July 26, 1992, to allow routine to be
  4.   used with code compiled with BC's /Fs option.  (Most) changes marked with
  5.   ;BCFS0726.  May have optimized some code also.  This is a plug-in replacement
  6.   for the original code.
  7.  
  8.   BCFS0801: Standardized code format.  Made sure that DI, SI, BP, and DS are
  9.   saved to meet requirements of BC 7.x.
  10.  
  11.  *  FINDWORD
  12.  *----------------------------------------------------------------------------
  13.  *
  14.  *  Routine to find the next (direction = 1)  or previous (direction = 0)
  15.  *  word position in a QuickBASIC string
  16.  *
  17.  *  Author: Tom Collins
  18.  *        09-20-90
  19.  *
  20.  *  Typical Calling Sequence:
  21.  *
  22.  *     WordPos% = 1
  23.  *     A$ = "This is a test"
  24.  *     CALL FindWord(A$ ,1 ,WordPos%)
  25.  *
  26.  *  Assemble with: TASM upcase.asm [/Zi]   or,
  27.  *           MASM upcase.asm [/Zi]
  28.  *  (Zi = Debug info for CV)
  29.  *
  30.  
  31.     @
  32.  
  33.     extrn    StringAddress:far
  34.  
  35.     .MODEL    MEDIUM, BASIC
  36.  
  37.     .CODE
  38.  
  39.     PUBLIC    FINDWORD
  40.  
  41. FINDWORD proc    USES si di, STRDES:ptr, Direction:ptr, Z:ptr
  42.  
  43. ; The registers are used as follows:
  44. ;
  45. ;    AH    = Direction flag
  46. ;    BL    = Z value
  47. ;    BH    = New return value
  48. ;    CL    = String length
  49. ;    CH    = Current string index in main loop
  50. ;    DS:SI = Current string index + 1 address
  51.  
  52.     push    ds        ; Save DS
  53.  
  54.     push    STRDES                                  ;BCFS0726
  55.     call    StringAddress    ; DX:AX = address, CX = length              ;BCFS0726
  56.     mov    si,ax        ; save offset of string                  ;BCFS0726
  57.  
  58.     mov    di, Direction    ; DS:DI -> Direction indicator
  59.     mov    ah, [di]    ; AH = Direction
  60.  
  61.     mov    di, Z        ; DS:DI -> Z value
  62.     mov    bl, [di]    ; BL = Where we're at now (Z value)
  63.  
  64.     mov    ds,dx
  65.  
  66. ; Test for invalid conditions
  67.  
  68.     mov    bh, bl        ; Assume no change in Z value
  69.     jcxz    FW100        ; Null string?  Leave.                  ;BCFS0726
  70.     or    ch,ch        ; Check string too long                  ;BCFS0726
  71.     jnz    FW100        ; We have a screen line > 255??  Wow!          ;BCFS0726
  72.  
  73.     inc    ch        ; CH = String index counter              ;BCFS0726
  74.  
  75.     cmp    cl, bl        ; See if we're starting past string end
  76.     jge    FW00        ; No
  77.     and    ah, ah        ; Yes. Test the direction.
  78.     jnz    FW100        ; Forward.  Return
  79. ;    mov    bh, cl        ; Backward.  Set return value to str len + 1
  80. ;    inc    bh
  81. ;    jmp    FW100        ; Return
  82.  
  83. ; Initialize the default return value based on the direction
  84.  
  85. FW00:    mov    bh, cl        ; Assume string length + 1
  86.     inc    bh
  87.     and    ah, ah        ; Set the flags
  88.     jnz    FW01        ; Backwards
  89.     mov    bh, 1        ; Backwards.  Assume length of 1
  90.  
  91. ; Main loop.
  92.  
  93. FW01:    lodsb            ; Get a character from string into AL
  94.     cmp    al, ' '        ; Is it a space?
  95.     je    FW02        ; Yes
  96.     cmp    al, 0FAh    ; Is is a soft space?
  97.     jne    FW10        ; No
  98.  
  99. ; Found a space or soft space.  See what the next character is.
  100.  
  101. FW02:    mov    al, [si]    ; Get next character                  ;BCFS0726
  102.     cmp    al, ' '        ; Is it a space?
  103.     je    FW10        ; Yes. Continue loop
  104.     cmp    al, 0FAh    ; Is is a soft space?
  105.     je    FW10        ; Yes
  106.  
  107. ; Found a word position.  See which direction we're going in.
  108.  
  109.     and    ah, ah
  110.     jz    FW04        ; Backwards
  111.  
  112. ; Forward direction.  If index > Z, set return value and exit.  Otherwise,
  113. ; keep looking.
  114.  
  115. FW03:    cmp    ch, bl        ; See if index > Z
  116.     jl    FW10        ; No. Keep looking.
  117.     inc    ch        ; Yes. Save current index + 1 in return loc
  118.     mov    bh, ch
  119.     jmp    short FW100    ; Return
  120.  
  121. ; Reverse direction.  If we're >= Z, return.  Otherwise set new value
  122. ; and look for the next word position.
  123.  
  124. FW04:    push    cx        ; Save CX temporarily
  125.     inc    ch        ; Add one to CH for comparison
  126.     cmp    ch, bl        ; See if index < Z
  127.     pop    cx        ; Restore CX
  128.     jge    FW100        ; Yes.  Return
  129.     mov    bh, ch        ; No. Save current index + 1 in return loc
  130.     inc    bh
  131.     jmp    short FW10    ; Continue loop
  132.  
  133. ; Continue loop while not at the end of the string
  134.  
  135. FW10:    inc    ch        ; Increment string index
  136.     cmp    cl, ch        ; Test for equal to string length
  137.     jge    FW01        ; Not equal.  Continue loop.
  138.  
  139. ; Return to the calling program
  140.  
  141. FW100:    pop    ds        ; Restore BASIC's DS so we can return value   ;BCFS0726
  142.     mov    di, Z        ; DS:DI -> Z
  143.     mov    [di], bh    ; Load return value
  144.     ret            ; Return
  145.  
  146. FINDWORD endp
  147.  
  148.     end
  149.