home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mass61.zip / mass.zip / masm61 / DISK3 / SAMPLES / SHOW / SHOWUTIL.AS$ / SHOWUTIL
Text File  |  1992-11-12  |  8KB  |  236 lines

  1. ;* SHOWUTIL.ASM - Module containing various routines used by SHOW
  2.  
  3.         TITLE   ShowUtil
  4.         .MODEL  small, pascal
  5.  
  6.         INCLUDE show.inc
  7.  
  8.         .CODE
  9.  
  10. ;* GetNamePos - Given a file specification potentially including filename,
  11. ;* directory, and/or drive, return the position of the first character
  12. ;* of the filename.
  13. ;*
  14. ;* Params: pchSpec - address of file spec
  15. ;*
  16. ;* Return: Near pointer to position of name portion of file spec
  17.  
  18. GetNamePos PROC USES di si,
  19.         pchSpec:PTR BYTE
  20.  
  21.         push    ds
  22.         pop     es
  23.         mov     di, pchSpec             ; Load address of filename
  24.         mov     si, di                  ; Save copy
  25.  
  26.         sub     cx, cx                  ; Use CX as count
  27.         sub     dx, dx                  ; Use DX as found flag
  28.         sub     ax, ax                  ; Search for null
  29.  
  30.         .REPEAT
  31.         .IF BYTE PTR es:[di] == '\'     ; For each backslash,
  32.         mov     si, di                  ;   save position and
  33.         inc     dx                      ;   set flag to true
  34.         .ENDIF
  35.         inc     cx                      ; Count it
  36.         scasb                           ; Get next character
  37.         .UNTIL  zero?
  38.  
  39.         .IF     dx != 0                 ; If found backslash,
  40.         mov     ax, si                  ;   return position in AX
  41.         inc     ax
  42.  
  43.         .ELSE                           ; Else search for colon
  44.         mov     di, si                  ; Restore start of name
  45.         mov     ax, ":"                 ; Search for colon
  46.         repne   scasb
  47.  
  48.         .IF     zero?                   ; If colon,
  49.         mov     ax, di                  ;   return position in DX:AX
  50.         .ELSE                           ; Else,
  51.         mov     ax, si                  ;   return original address
  52.         .ENDIF
  53.         .ENDIF
  54.  
  55.         ret
  56.  
  57. GetNamePos ENDP
  58.  
  59.  
  60. ;* GoBack - Purpose   Searches backward through buffer
  61. ;*
  62. ;* Params:   CX has number of lines
  63. ;*           ES:DI has buffer position
  64. ;*           AL has 10 (line feed character)
  65. ;*
  66. ;* Return:   None
  67. ;*
  68. ;* Modifies: Updates yCur and offBuf
  69.  
  70. GoBack  PROC
  71.  
  72.         neg     cx                      ; Make count positive
  73.         mov     dx, cx                  ; Save a copy
  74.         inc     cx                      ; One extra to go up one
  75.         .IF     di == 0                 ; If start of file, done
  76.         ret
  77.         .ENDIF
  78.  
  79.         .REPEAT
  80.         push    cx                      ; Save count
  81.         mov     cx, 0FFh                ; Load maximum character count
  82.         .IF     cx >= SWORD PTR di      ; If near start of buffer,
  83.         mov     cx, di                  ;   search only to start
  84.         .ENDIF
  85.         std                             ; Go backward
  86.         repne   scasb                   ; Find last previous LF
  87.         cld                             ; Go forward
  88.         jcxz    atstart                 ; If not found, must be at start
  89.         pop     cx
  90.         .UNTILCXZ
  91.  
  92.         .IF     yCur == 0FFFFh          ; If end of file flag,
  93.         add     di, 2                   ;   adjust for cr/lf,
  94.         mov     offBuf, di              ;   save position,
  95.         call    EndCount                ;   count back to get line number,
  96.         mov     yCur, ax                ;   store line count
  97.         ret
  98.         .ENDIF
  99.  
  100.         sub     yCur, dx                ; Calculate line number
  101.         jg      positive
  102.         mov     yCur, 1                 ; Set to 1 if negative
  103. positive:
  104.         add     di, 2                   ; Adjust for cr/lf
  105.         mov     offBuf, di              ; Save position
  106.         ret
  107. atstart:
  108.         pop     cx
  109.         sub     di, di                  ; Load start of file
  110.         mov     yCur, 1                 ; Line 1
  111.         mov     offBuf, di              ; Save position
  112.         ret
  113.  
  114. GoBack  ENDP
  115.  
  116.  
  117. ;* GoForeward - Skips forward through a buffer of text a specified
  118. ;* number of lines.
  119. ;*
  120. ;* Params:  CX - number of text lines to skip
  121. ;*          ES:DI - starting buffer position
  122. ;*          AL has 10 (line-feed character)
  123. ;*
  124. ;* Return:  None
  125. ;*
  126. ;* Modifes: yCur, offBuf, bx, cx, di
  127.  
  128. GoForeward PROC
  129.  
  130.         cld                             ; Go forward
  131.         mov     dx, cx                  ; Copy count
  132.  
  133.         .REPEAT
  134.         push    cx                      ; Save count
  135.         mov     cx, 0FFh                ; Load maximum character count
  136.         mov     bx, cbBuf               ; Get end of file
  137.  
  138.         sub     bx, di                  ; Characters to end of file
  139.         .IF     cx >= bx                ; If less than maximum per line,
  140.         mov     cx, bx                  ;   adjust
  141.         .ENDIF
  142.  
  143.         repne   scasb                   ; Find next LF
  144.         pop     cx
  145.  
  146.  
  147.         .IF     !zero? || (di >= cbBuf) ; If LF not found or beyond end,
  148.         mov     di, offBuf              ;   restore original position
  149.         ret                             ;   and quit
  150.         .ENDIF
  151.         .UNTILCXZ
  152.  
  153.         add     yCur, dx                ; Calculate line number
  154.         mov     offBuf, di              ; Save position
  155.         ret
  156.  
  157. GoForeward ENDP
  158.  
  159.  
  160. ;* EndCount - Skips backward through a buffer of text, counting each
  161. ;* text line.
  162. ;*
  163. ;* Params: ES:DI - buffer position (end of file)
  164. ;*
  165. ;* Return: Number of lines counted
  166.  
  167. EndCount PROC USES di dx cx
  168.  
  169.         std                             ; Backward
  170.         mov     al, 13                  ; Search for CR
  171.         mov     dx, -1                  ; Initialize (first will inc to 0)
  172.  
  173.         .REPEAT
  174.         inc     dx                      ; Adjust count
  175.         mov     cx, 0FFh                ; Load maximum character count
  176.  
  177.         .IF     SWORD PTR cx >= di      ; If near start of buffer,
  178.         mov     cx, di                  ;   search only to start
  179.         .ENDIF
  180.  
  181.         repne   scasb                   ; Find last previous CR
  182.         .UNTIL  !zero?                  ; If not found, must be at start
  183.  
  184.         mov     ax, dx                  ; Return count
  185.         cld                             ; Forward
  186.         ret
  187.  
  188. EndCount ENDP
  189.  
  190.  
  191. ;* BinToStr - Converts an unsigned integer to a string. User is
  192. ;* responsible for providing a large enough buffer. The string is
  193. ;* not null terminated.
  194. ;*
  195. ;* Params: i - Integer to be converted
  196. ;*         pch - Pointer to character buffer to receive string
  197. ;*
  198. ;* Return: Number of character in string.
  199.  
  200. BinToStr PROC,
  201.         i:WORD,
  202.         pch:PTR BYTE
  203.  
  204.         mov     ax, i
  205.         mov     di, pch
  206.  
  207.         sub     cx, cx                  ; Clear counter
  208.         mov     bx, 10                  ; Divide by 10
  209.  
  210. ; Convert and save on stack backwards
  211.  
  212.         .REPEAT
  213.         sub     dx, dx                  ; Clear top
  214.         div     bx                      ; Divide to get last digit as remainder
  215.         add     dl, "0"                 ; Convert to ASCII
  216.         push    dx                      ; Save on stack
  217.         .UNTILCXZ ax == 0               ;   until quotient is 0
  218.  
  219. ; Take off the stack and store forward
  220.  
  221.         neg     cx                      ; Negate and save count
  222.         mov     dx, cx
  223.  
  224.         .REPEAT
  225.         pop     ax                      ; Get character
  226.         stosb                           ; Store it
  227.         .UNTILCXZ
  228.         mov     ax, dx                  ; Return digit count
  229.  
  230.         ret
  231.  
  232. BinToStr ENDP
  233.  
  234.  
  235.         END
  236.