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

  1.         TITLE    'Listing 8-4'
  2.         NAME    ScanRight10
  3.         PAGE    55,132
  4.  
  5. ;
  6. ; Name:        ScanRight10
  7. ;
  8. ; Function:    Scan for a pixel of a given value in 16-color EGA/VGA graphics
  9. ;
  10. ; Caller:    Microsoft C:
  11. ;
  12. ;            int ScanRight10(x,y);
  13. ;
  14. ;            int x,y;            /* starting pixel */
  15. ;
  16. ;            extern    int BorderValue;    /* value of border pixel */
  17. ;
  18. ;        Returns the x-coordinate of the rightmost border pixel.
  19. ;
  20.  
  21. ARGx        EQU    word ptr [bp+4]    ; stack frame addressing
  22. ARGy        EQU    word ptr [bp+6]
  23.  
  24. ByteOffsetShift    EQU    3        ; used to convert pixels to byte offset
  25. BytesPerLine    EQU    80        ; 80 for most 16-color graphics modes
  26.                     ;  (40 for 320x200 16-color)
  27.  
  28.  
  29. DGROUP        GROUP    _DATA
  30.  
  31. _TEXT        SEGMENT    byte public 'CODE'
  32.         ASSUME    cs:_TEXT,ds:DGROUP
  33.  
  34.         EXTRN    PixelAddr10:near
  35.  
  36.         PUBLIC    _ScanRight10
  37. _ScanRight10    PROC    near
  38.  
  39.         push    bp        ; preserve caller registers
  40.         mov    bp,sp
  41.         push    si
  42.         push    di
  43.  
  44. ; calculate pixel address of (0,y)
  45.  
  46.         mov    ax,ARGy        ; AX := y
  47.         xor    bx,bx        ; BX := 0
  48.         call    PixelAddr10    ; ES:BX -> buffer
  49.         mov    di,bx        ; ES:DI -> buffer
  50.  
  51. ; calculate offset of x in row
  52.  
  53.         mov    ax,ARGx
  54.         mov    si,ax        ; SI,AX := x
  55.         mov    cl,ByteOffsetShift
  56.         shr    si,cl        ; SI := offset of x in row y
  57.         add    di,si        ; DI := offset of x in buffer
  58.  
  59. ; calculate a bit mask for the first byte to scan
  60.  
  61.         mov    cl,al
  62.         and    cl,7        ; CL := x & 7
  63.         mov    ch,0FFh
  64.         shr    ch,cl        ; CH := bit mask for first scanned byte
  65.  
  66. ; configure the Graphics Controller
  67.  
  68.         mov    dx,3CEh        ; DX := Graphics Controller port addr
  69.  
  70.         mov    ah,_BorderValue    ; AH := pixel value for Color Compare reg
  71.         mov    al,2        ; AL := Color Compare Reg number
  72.         out    dx,ax
  73.  
  74.         mov    ax,805h        ; AH := 00001000b (Read Mode 1)
  75.         out    dx,ax        ; AL := Mode reg number
  76.  
  77.         mov    ax,0F07h    ; AH := 00001111b (Color Compare reg value)
  78.         out    dx,ax        ; AL := Color Compare reg number
  79.  
  80. ; inspect the first byte for border pixels
  81.  
  82.         mov    al,es:[di]    ; AL := nonzero bits corresponding to
  83.                     ;  border pixels
  84.         inc    di        ; ES:DI -> next byte to scan
  85.         and    al,ch        ; apply bit mask
  86.         jnz    L01        ; jump if border pixel(s) found
  87.  
  88. ; scan remainder of line for border pixels
  89.  
  90.         mov    cx,BytesPerLine
  91.         sub    cx,si        ; CX := BytesPerLine - (byte offset of
  92.                     ;  starting pixel)
  93.         dec    cx        ; CX := # of bytes to scan
  94.  
  95.         repe    scasb        ; scan until nonzero byte read; i.e.,
  96.                     ;  border pixel(s) found
  97.  
  98. ; compute x value of border pixel
  99.  
  100.         mov    al,es:[di-1]    ; AL := last byte compared
  101.  
  102. L01:        sub    di,bx        ; DI := offset of byte past the one which
  103.                     ;  contains a border pixel
  104.         mov    cl,ByteOffsetShift
  105.         shl    di,cl        ; DI := x-coordinate of 1st pixel in byte
  106.  
  107.         mov    cx,8        ; CX := loop limit
  108.  
  109. L02:        shl    al,1        ; isolate first border pixel
  110.         jc    L03
  111.  
  112.         loop    L02
  113.  
  114. L03:        sub    di,cx        ; DI := x-coordinate of border pixel
  115.  
  116. ; restore default Graphics Controller state and return to caller
  117.  
  118.         mov    ax,2        ; AH := 0 (default Color Compare value)
  119.         out    dx,ax        ; restore Color Compare reg
  120.  
  121.         mov    al,5        ; AH := 0, AL := 5
  122.         out    dx,ax        ; restore Mode reg
  123.  
  124.         mov    ax,di        ; AX := return value
  125.  
  126.         pop    di        ; restore caller registers and return
  127.         pop    si
  128.         mov    sp,bp
  129.         pop    bp
  130.         ret
  131.  
  132. _ScanRight10    ENDP
  133.  
  134. _TEXT        ENDS
  135.  
  136.  
  137. _DATA        SEGMENT    word public 'DATA'
  138.  
  139.         EXTRN    _BorderValue:byte
  140.  
  141. _DATA        ENDS
  142.  
  143.         END
  144.