Topic : The VGA Training Program
Author : Grant Smith
Page : << Previous 22  Next >>
Go to page :


start of the
y-line (see earlier trainers for a description of how SEGMENT:OFFSET
works.

IN   : x1 , x2, y, color, where

           asm
             mov    ax,where
             mov    es,ax
             mov    di,y
             mov    ax,y
             shl    di,8   { di:=di*256 }
             shl    ax,6   { ax:=ax*64 }
             add    di,ax  { di := (y*256)+(y*64) := y*320 Faster then a
                             straight multiplication }

Right, now you add the first x value to get your startoff.
             add    di,x1
Move the color to store into ah and al
             mov    al,color
             mov    ah,al       { ah:=al:=color }
then get CX equal to how many pixels across you want to go
             mov    cx,x2
             sub    cx,x1   { cx:=x2-x1 }
Okay, as we all know, moving a word is a lot faster then moving a byte,
so we halve CX
             shr    cx,1    { cx:=cx/2 }
but what happens if CX was an odd number. After a shift, the value of
the last number is placed in the carry flag, so what we do is jump over
a single byte move if the carry flag is zero, or execute it if it is
one.

            jnc     @Start  { If there is no carry, jump to label Start }
            stosb           { ES:[DI]:=al ; increment DI }
        @Start :            { Label Start }
            rep     stosw   { ES:[DI]:=ax ; DI:=DI+2; repeat CX times }

Right, the finished product looks like this :

[Pascal]

  Procedure Hline (x1,x2,y:word;col:byte;where:word); assembler;
    { This draws a horizontal line from x1 to x2 on line y in color col }
  asm
    mov   ax,where
    mov   es,ax
    mov   ax,y
    mov   di,ax
    shl   ax,8
    shl   di,6
    add   di,ax
    add   di,x1

    mov   al,col
    mov   ah,al
    mov   cx,x2
    sub   cx,x1
    shr   cx,1
    jnc   @start
    stosb
  @Start :
    rep   stosw
  end;

[C++]

  void Hline (word X1, word X2, word Y, byte Col, word Where) {
    asm {
      mov     ax, [Where]  // move segment of Where to AX
      mov     es, ax       // set ES to segment of Where
      mov     ax, [Y]      // set AX to Y
      mov     di, ax       // set DI to Y
      shl     ax, 8        // shift AX left 8 places (multiply Y by 256)
      shl     di, 6        // shift DI left 6 places (multiply Y by 64)
      add     di, ax       // add AX to DI (Y*64 + Y*256 = Y*320)
      add     di, [X1]     // add the X1 offset to DI
      mov     al, [Col]    // move Col to AL
      mov     ah, al       // move Col to AH (we want 2 copies for word moving)
      mov     cx, [X2]     // move X2 to CX
      sub     cx, [X1]     // move the change in X to CX
      shr     cx, 1        // divide change in X by 2 (for word moving)
      jnc     Start        // if we have an even number of moves, go to Start
      stosb                // otherwise, move one byte more
    }
    Start: asm {
      rep     stosw        // do it!
    }
  }

Done!



  In closing

This 3D system is still not perfect. It needs to be faster, and now I
have also dumped the problem of face-sorting on you! Nyahahahaha!

           [ My sister and I were driving along the other day when she
               asked me, what would I like for my computer.
             I thought long and hard about it, and came up with the
               following hypothesis. When a girl gets a Barbie doll, she
               then wants the extra ballgown for the doll, then the
               hairbrush, and the car, and the house, and the friends
               etc.
             When a guy gets a computer, he wants the extra memory, the
               bigger hard drive, the maths co-pro, the better
               motherboard, the latest software, and the bigger monitor
               etc.
             I told my sister all of this, and finished up with : "So as
               you can see, computers are Barbie dolls for MEN!"
             She called me a chauvinist. And hit me. Hard.
                                                

Page : << Previous 22  Next >>