home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / masm / masm5 / pagerp.asm < prev    next >
Encoding:
Assembly Source File  |  1988-08-11  |  11.2 KB  |  305 lines

  1.           TITLE   Pager
  2.           .MODEL  small, pascal
  3.  
  4. INCL_VIO  EQU 1
  5.  
  6.           INCLUDE os2.inc
  7.           .DATA
  8.           EXTRN   stAtrib:BYTE, scAtrib:BYTE, Cell:WORD, stLine:BYTE
  9.           EXTRN   sBuffer:WORD, oBuffer:WORD, Buffer:DWORD, lBuffer:WORD
  10.           EXTRN   nLines:WORD, curLine:WORD 
  11.  
  12.           .CODE
  13.           PUBLIC  Pager
  14.  
  15. ; Procedure Pager
  16. ; Purpose   Displays status and text lines
  17. ; Input     Stack variable: lines to scroll (negative up, positive down)
  18. ;           Global variables: "sbuffer", "oBuffer", "curLine"
  19. ; Output    To screen
  20.  
  21. Pager     PROC    count
  22.  
  23.           mov     es, sBuffer           ; Initialize buffer position
  24.           mov     di, oBuffer
  25.  
  26.           mov     cx, count             ; Get count argument
  27.           mov     ax, 10                ; Search for linefeed
  28.  
  29.           or      cx, cx                ; Argument 0?
  30.           jl      skip1                 ; If below, backward
  31.           jg      skip2                 ; If above, forward
  32.           jmp     SHORT skip3           ; If equal, done
  33.  
  34. skip1:    call    GoBack                ; Adjust backward
  35.           jmp     SHORT skip3           ; Show screen
  36. skip2:    call    GoForwd               ; Adjust forward
  37.  
  38. ; Write line number to status line
  39.  
  40. skip3:    cld                           ; Go forward
  41.           push    di                    ; Save
  42.           push    ds                    ; ES = DS
  43.           pop     es
  44.  
  45. ; BinToStr (curLine, OFFSET stLine[6])
  46.  
  47.           push    curLine               ; Arg 1
  48.           @Pushc  <OFFSET stLine[6]>    ; Arg 2
  49.           call    BinToStr              ; Convert to string
  50.  
  51. ; Fill in status line
  52.  
  53.           mov     cx, 6                 ; Six spaces to fill
  54.           sub     cx, ax                ; Subtract those already done
  55.           mov     al, " "               ; Fill with space
  56.           rep     stosb
  57.  
  58.           @VioWrtCharStrAtt stLine, 80, 0, 0, stAtrib, 0 ; Write to screen
  59.  
  60.           pop     di                    ; Update position
  61.           mov     si, di
  62.           mov     cx, nLines            ; Lines per screen
  63.  
  64. loop1:    mov     bx, nLines            ; Lines of text
  65.           inc     bx                    ; Adjust for 0
  66.           sub     bx, cx                ; Calculate current row
  67.           push    cx                    ; Save line number
  68.  
  69. ; ShowLine (position, line, maxlength, &scAtrib)
  70.  
  71.           push    sBuffer               ; Arg 1
  72.           push    si
  73.           push    bx                    ; Arg 2
  74.           push    lBuffer               ; Arg 3
  75.           push    ds                    ; Arg r4
  76.           @Pushc  <OFFSET scAtrib>
  77.           call    ShowLine              ; Write line
  78.  
  79.           pop     cx                    ; Restore line number
  80.           mov     si, ax                ; Get returned position
  81.  
  82.           cmp     ax, lBuffer           ; If beyond end of file,
  83.           jae     skip4                 ;   fill screen with spaces
  84.           loop    loop1                 ; else next line
  85.           jmp     SHORT exit            ; Exit if done
  86.  
  87. ; Fill the rest with spaces
  88.  
  89. skip4:    dec     cx
  90.           jcxz    exit
  91.           mov     ax, 80                ; Columns times remaining lines
  92.           mul     cl
  93.           mov     dx, ax                ; Macros use AX, so use DX
  94.           sub     cx, nLines            ; Calculate starting line
  95.           neg     cx
  96.           inc     cx
  97.  
  98.           @VioWrtNCell Cell, dx, cx, 0, 0 ; Write space cells
  99.  
  100. exit:     ret
  101. Pager     ENDP
  102.  
  103. ; Procedure ShowLine (inLine, sRow, &pAtrib)
  104. ; Purpose   Writes a line to screen
  105. ; Input     Stack variables: 1 - FAR PTR to input line
  106. ;                            2 - line number
  107. ;                            3 - maximum number of characters (file length)
  108. ;                            4 - FAR PTR to attribute
  109. ; Output    Line to screen
  110.  
  111. ShowLine  PROC    USES si di, inLine:FAR PTR BYTE, srow, max, pAtrib:FAR PTR BYTE
  112.           LOCAL   outLine[80]:BYTE
  113.  
  114.           push    ds                    ; Save
  115.           push    ss                    ; ES = SS
  116.           pop     es
  117.           lea     di, outLine           ; Destination line
  118.           lds     si, inLine            ; Source line
  119.           mov     cx, 80                ; Cells per row
  120.           mov     bx, di                ; Save copy of start for tab calc
  121. loop1:    lodsb                         ; Get character
  122.           cmp     al, 9                 ; Tab?
  123.           je      skip1                 ; Space out tab
  124.           cmp     al, 13                ; CR?
  125.           je      skip3                 ; Fill rest of line with spaces
  126.           stosb                         ; Copy out
  127.           cmp     si, max               ; Check for end of file
  128.           ja      skip3
  129.           loop    loop1
  130.  
  131. loop2:    lodsb                         ; Throw away rest of line to truncate
  132.           cmp     si, max               ; Check for end of file
  133.           ja      exit
  134.           cmp     al, 13                ; Check for end of line
  135.           jne     loop2
  136.           inc     si                    ; Throw away line feed
  137.  
  138.           jmp     SHORT exit            ; Done
  139.  
  140. skip1:    push    bx                    ; Fill tab with spaces
  141.           push    cx
  142.  
  143.           sub     bx, di                ; Get current position in line
  144.           neg     bx
  145.  
  146.           mov     cx, 8                 ; Default count 8
  147.           and     bx, 7                 ; Get modulus
  148.           sub     cx, bx                ; Subtract
  149.           mov     bx, cx                ; Save modulus
  150.  
  151.           mov     al, " "               ; Write spaces
  152.           rep     stosb
  153.  
  154.           pop     cx
  155.           sub     cx, bx                ; Adjust count
  156.           jns     skip2                 ; Make negative count 0
  157.           sub     cx, cx
  158. skip2:    pop     bx
  159.           jcxz    loop2                 ; If beyond limit done
  160.           jmp     SHORT loop1
  161.  
  162. skip3:    inc     si                    ; After CR, throw away LF
  163.           mov     al, ' '               ; Fill rest of line
  164.           rep     stosb 
  165.  
  166. exit:     pop     ds
  167.           @VioWrtCharStrAtt outLine, 80, [srow], 0, [pAtrib], 0
  168.           
  169.           mov     ax, si                ; Return position
  170.           ret
  171. ShowLine  ENDP
  172.  
  173. ; Procedure GoBack
  174. ; Purpose   Searches backward through buffer
  175. ; Input     CX has number of lines; ES:DI has buffer position
  176. ; Output    Updates "curLine" and "oBuffer"
  177.  
  178. GoBack    PROC
  179.           std                           ; Go backward
  180.           neg     cx                    ; Make count positive
  181.           mov     dx, cx                ; Save a copy
  182.           inc     cx                    ; One extra to go up one
  183.           or      di, di                ; Start of file?
  184.           je      exit                  ; If so, ignore
  185. loop1:    push    cx                    ;   else save count
  186.           mov     cx, 0FFh              ; Load maximum character count
  187.           cmp     cx, di                ; Near start of buffer?
  188.           jl      skip1                 ; No? Continue
  189.           mov     cx, di                ;   else search only to start
  190. skip1:    repne   scasb                 ; Find last previous LF
  191.           jcxz    skip4                 ; If not found, must be at start
  192.           pop     cx
  193.           loop    loop1
  194.           cmp     curLine, -1           ; End of file flag?
  195.           jne     skip2                 ; No? Continue
  196.           add     di, 2                 ; Adjust for cr/lf
  197.           mov     oBuffer, di           ; Save position
  198.           call    EndCount              ; Count back to get line number
  199.           ret
  200.  
  201. skip2:    sub     curLine, dx           ; Calculate line number
  202.           jg      skip3
  203.           mov     curLine, 1            ; Set to 1 if negative
  204. skip3:    add     di, 2                 ; Adjust for cr/lf
  205.           mov     oBuffer, di           ; Save position
  206.           ret
  207.  
  208. skip4:    pop     cx
  209.           sub     di, di                ; Load start of file
  210.           mov     curLine, 1            ; Line 1
  211.           mov     oBuffer, di           ; Save position
  212. exit:     ret
  213. GoBack    ENDP
  214.  
  215. ; Procedure GoForwd
  216. ; Purpose   Searches forward through a buffer
  217. ; Input     CX has number of lines; ES:DI has buffer position
  218. ; Output    Updates "curLine" and "oBuffer"
  219.  
  220. GoForwd   PROC
  221.           cld                           ; Go forward
  222.           mov     dx, cx                ; Copy count
  223. loop1:    push    cx                    ; Save count
  224.           mov     cx, 0FFh              ; Load maximum character count
  225.           mov     bx, lBuffer           ; Get end of file
  226.  
  227.           sub     bx, di                ; Characters to end of file
  228.           cmp     cx, bx                ; Less than maximum per line?
  229.           jb      skip1
  230.           mov     cx, bx
  231. skip1:    repne   scasb                 ; Find next LF
  232.           jcxz    exit                  ; If not found, must be at end
  233.           cmp     di, lBuffer           ; Beyond end?
  234.           jae     exit
  235.           pop     cx
  236.           loop    loop1
  237.           add     curLine, dx           ; Calulate line number
  238.           mov     oBuffer, di           ; Save position
  239.           ret
  240.  
  241. exit:     pop     cx
  242.           mov     di, oBuffer           ; Restore position
  243.           ret
  244. GoForwd   ENDP
  245.  
  246. ; Procedure EndCount
  247. ; Purpose   Counts backward to count lines in file
  248. ; Input     ES:DI has buffer position
  249. ; Output    Modifies "curLine"
  250.  
  251. EndCount  PROC
  252.           push    di
  253.  
  254.           mov     al, 13                ; Search for CR
  255.           mov     curLine, 0            ; Initialize
  256.  
  257. loop1:    inc     curLine               ; Adjust count
  258.           mov     cx, 0FFh              ; Load maximum character count
  259.           cmp     cx, di                ; Near start of buffer?
  260.           jl      skip1                 ; No? Continue
  261.           mov     cx, di                ;   else search only to start
  262. skip1:    repne   scasb                 ; Find last previous cr
  263.           jcxz    exit                  ; If not found, must be at start
  264.           jmp     SHORT loop1
  265.  
  266. exit:     pop     di
  267.           ret
  268. EndCount  ENDP
  269.  
  270. ; Procedure BinToStr (number, string)
  271. ; Purpose   Converts integer to string
  272. ; Input     Stack arguments: 1 - Number to convert; 2 - Near address for write
  273. ; Output    AX has characters written
  274.  
  275. BinToStr  PROC    number, string:PTR BYTE
  276.  
  277.           mov     ax,number
  278.           mov     di,string
  279.  
  280.           sub     cx, cx                ; Clear counter
  281.           mov     bx, 10                ; Divide by 10
  282.  
  283. ; Convert and save on stack backwards
  284.  
  285. loop1:    sub     dx, dx                ; Clear top
  286.           div     bx                    ; Divide to get last digit as remainder
  287.           add     dl, "0"               ; Convert to ASCII
  288.           push    dx                    ; Save on stack
  289.           or      ax, ax                ; Quotient 0?
  290.           loopnz  loop1                 ; No? Get another
  291.  
  292. ; Take off the stack and store forward
  293.  
  294.           neg     cx                    ; Negate and save count
  295.           mov     dx, cx
  296. loop2:    pop     ax                    ; Get character
  297.           stosb                         ; Store it
  298.           loop    loop2   
  299.           mov     ax, dx                ; Return digit count
  300.  
  301.           ret     
  302. BinToStr  ENDP
  303.  
  304.           END
  305.