home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / video / 3 / 3_10.asm next >
Encoding:
Assembly Source File  |  1988-08-11  |  3.2 KB  |  134 lines

  1.         TITLE    'Listing 3-10'
  2.         NAME    DisplayText
  3.         PAGE    55,132
  4.  
  5. ;
  6. ; Name:        DisplayText
  7. ;
  8. ; Function:    Display an alphanumeric string without interference on the CGA
  9. ;
  10. ; Caller:    Microsoft C:
  11. ;
  12. ;            int DisplayText(buf,n,offset);
  13. ;
  14. ;            char *buf;        /* buffer containing text in
  15. ;                           CGA alphanumeric format
  16. ;                           (alternating character codes
  17. ;                           and attribute bytes)
  18. ;                        */
  19. ;
  20. ;            int n;            /* buffer length in bytes */
  21. ;
  22. ;            unsigned int offset;    /* offset into video buffer */
  23. ;
  24.  
  25. ARGbuf        EQU    word ptr [bp+4]
  26. ARGn        EQU    word ptr [bp+6]
  27. ARGoffset    EQU    word ptr [bp+8]
  28. TIMEOUT     EQU    6        ; horizontal timeout loop limit
  29. VBcount     EQU    250        ; number of words to write during
  30.                     ;  vertical blanking interval
  31.  
  32. _TEXT        SEGMENT    byte public 'CODE'
  33.         ASSUME    cs:_TEXT
  34.  
  35.         PUBLIC    _DisplayText
  36. _DisplayText    PROC    near
  37.  
  38.         push    bp        ; usual C prologue to establish
  39.         mov    bp,sp        ;  stack frame and preserve registers
  40.         push    si
  41.         push    di
  42.  
  43.         mov    ax,0B800h
  44.         mov    es,ax
  45.         mov    di,ARGoffset    ; ES:DI -> destination in video buffer
  46.         mov    si,ARGbuf    ; DS:SI -> source buffer
  47.         mov    cx,ARGn
  48.         shr    cx,1        ; CX := buffer length in words
  49.  
  50.         mov    dx,3DAh        ; DX := CGA Status Port
  51.  
  52. ; write during remaining vertical blanking interval
  53.  
  54. L01:        mov    bx,cx        ; preserve buffer length in BX
  55.         mov    cx,TIMEOUT    ; CX := horizontal timeout
  56.         cli            ; disable interrupts during loop
  57.  
  58. L02:        in    al,dx        ; AL := video status
  59.         test    al,1
  60.         loopnz    L02        ; loop while Display Enable inactive
  61.         jz    L03        ; jump if loop did not time out
  62.  
  63.         movsw            ; copy one word
  64.         sti
  65.         mov    cx,bx        ; CX := buffer length
  66.         loop    L01
  67.  
  68.         jmp    short L10    ; exit (entire string copied)
  69.  
  70. ; write during horizontal blanking intervals
  71.  
  72. L03:        sti
  73.         mov    cx,bx        ; restore CX
  74.  
  75. L04:        lodsw            ; AL := character code
  76.                     ; AH := attribute
  77.         mov    bx,ax        ; BX := character and attribute
  78.         push    cx        ; preserve word loop counter
  79.         mov    cx,TIMEOUT    ; CX := timeout loop limit
  80.  
  81.         cli            ; clear interrupts during one scan line
  82. L05:        in    al,dx
  83.         test    al,1
  84.         loopnz    L05        ; loop during horizontal blanking
  85.                     ;  until timeout occurs
  86.         jnz    L07        ; jump if timed out (vertical
  87.                     ;  blanking has started)
  88. L06:        in    al,dx
  89.         test    al,1
  90.         jz    L06        ; loop while Display Enable is active
  91.  
  92.         xchg    ax,bx        ; AX := character & attribute
  93.         stosw            ; copy 2 bytes to display buffer
  94.  
  95.         sti            ; restore interrupts
  96.         pop    cx        ; CX := word loop counter
  97.         loop    L04
  98.  
  99.         jmp    short L10    ; exit (entire string copied)
  100.  
  101. ; write during entire vertical blanking interval
  102.  
  103. L07:        pop    bx        ; BX := word loop counter
  104.         dec    si
  105.         dec    si        ; DS:SI -> word to copy from buffer
  106.  
  107.         mov    cx,VBcount    ; CX := # of words to copy
  108.         cmp    bx,cx
  109.         jnb    L08        ; jump if more than VBcount words remain
  110.                     ;  in buffer
  111.         mov    cx,bx        ; CX := # of remaining words in buffer
  112.         xor    bx,bx        ; BX := 0
  113.         jmp    short L09
  114.  
  115. L08:        sub    bx,cx        ; BX := (# of remaining words) - VBcount
  116.  
  117. L09:        rep    movsw        ; copy to video buffer
  118.  
  119.         mov    cx,bx        ; CX := # of remaining words
  120.         test    cx,cx
  121.         jnz    L01        ; loop until buffer is displayed
  122.  
  123. L10:        pop    di        ; usual C epilogue to restore registers
  124.         pop    si        ;  and discard stack frame
  125.         mov    sp,bp
  126.         pop    bp
  127.         ret
  128.  
  129. _DisplayText    ENDP
  130.  
  131. _TEXT        ENDS
  132.  
  133.         END
  134.