home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / masm / masm6 / show / showutil.asm < prev   
Encoding:
Assembly Source File  |  1991-01-14  |  7.4 KB  |  238 lines

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