10.2 Mouse

The mouse is controlled using the mouse functions at interrupt 33h. There are many functions, but this section will only cover the basic set required to get things going. For more information, see the references on the web page.

In order to use the mouse, you must first call Function 0000h (Reset Driver and Read Status). This initializes the drivers and hardware. The mouse cursor will initially be hidden, so you must use Function 0001h (Show Mouse Cursor) to make it visible. From then on, just call Function 0003h (Return Position and Button Status) to get the position and button status whenever your program needs it. Make sure you hide the mouse cursor before your program exits.

10.2.1 Mouse Interrupts (INT 33h)

10.2.1.1 Function 0000h: RESET DRIVER and READ STATUS

This function initializes the hardware and software so the mouse is ready to be used. The mouse will initially be hidden.

Inputs
  • AX = 0000h

Outputs
  • AX = Status

    • 0000h : Error. Hardware/software not installed.

    • FFFFh : OK. Hardware/software installed.

  • BX = Number of buttons

    • FFFFh : Two buttons.

    • 0000h : Other than two buttons.

    • 0003h : Three buttons.

10.2.1.2 Function 0001h: SHOW MOUSE CURSOR

This function makes the mouse cursor visible on the screen. If you are programming text or graphics by writing directly to the video memory, you should hide the mouse cursor before doing so to stop the mouse from leaving graphic junk on the screen.

Inputs
  • AX = 0001h

Outputs
  • (None)

10.2.1.3 Function 0002h: HIDE MOUSE CURSOR

This function makes the mouse cursor invisible. Multiple calls to this function require multiple calls to Function 0001h (Show Mouse Cursor) before the mouse cursor will appear again, because the mouse driver keeps a count of the number of times the mouse has been hidden.

Inputs
  • AX = 0002h

Outputs
  • (None)

10.2.1.4 Function 0003h: RETURN POSITION AND BUTTON STATUS

This function returns the current mouse cursor position and button status. Position is measured in pixels, with the origin (0,0) at the upper left corner of the screen. In text mode, each character is assumed by the mouse driver to correspond to eight pixels horizontally and eight pixels vertically. Thus, to get the row and column position of the mouse cursor in text mode, divide the values in CX and DX by eight.

Inputs
  • AX = 0003h

Outputs
  • BX = Button status (1 = corresponding button pressed)

    • Bit 0 : Left mouse button.

    • Bit 1 : Right mouse button.

    • Bit 2 : Middle mouse button (if present).

    • Bits 3-15 : Cleared to 0.

  • CX = Pixel column position.

  • DX = Pixel row position.

10.2.1.5 Function 0004h: POSITION MOUSE CURSOR

This function will position the mouse cursor on the screen. As in function 0003h, position is measured in pixels, with the origin (0,0) at the upper left corner of the screen. See the description of function 0003h for more information.

Inputs
  • AX = 0004h

  • CX = Column position

  • DX = Row position

Outputs
  • (None)

10.2.1.6 Function 0007h: DEFINE HORIZONTAL CURSOR RANGE

This function will limit the horizontal position of the mouse cursor to a defined section on the screen. The column positions are given in pixels.

Inputs
  • AX = 0007h

  • CX = Leftmost column boundary

  • DX = Rightmost column boundary

Outputs
  • (None)

10.2.1.7 Function 0008h: DEFINE VERTICAL CURSOR RANGE

This function will limit the vertical position of the mouse cursor to a defined section on the screen. The row positions are given in pixels.

Inputs
  • AX = 0008h

  • CX = Upper row boundary

  • DX = Lower row boundary

Outputs
  • (None)

10.2.2 Changing the Mouse Cursor

When the mouse cursor is enabled, the actual appearance of the cursor is dependent on the current video mode. If the video mode is text, then the cursor defaults to a character-sized block of color. If the video mode is a graphics mode, then the cursor appears as an arrow. Oftentimes you will want to change the appearance of the mouse cursor to better facilitate the application in which you are using it. For example, if the application is a paint program, you may want to the mouse cursor to appear as a paint brush perhaps. In a video game, you may want the mouse cursor to appear as a crosshair for targeting enemy space ships.

There are two basic ways in which this can be accomplished. First, you could manually create your own mouse "cursor" by simply reading the position of the mouse and manually drawing and erasing whatever graphic image you desire. However, you would necessarily have to be sure to restore the contents of the screen under which the mouse cursor is moving as the mouse is repositioned.

The second method is to use functions 0009h and 000Ah of INT 33h to redefine the appearance of the mouse cursor. This method is more attractive because you do not need to concern yourself with restoring the screen contents under the mouse cursor as it moves this would be done automatically as it normally is. Function 0009h is used more often than 000Ah, so it is the only one discussed below.

10.2.2.1 Function 0009h:

This function will redefine the appearance of the mouse cursor when the screen is in a graphics mode.

Inputs
  • AX = 0009h

  • BX = Column of cursor hot spot in bitmap (-16 to 16)

  • CX = Row of cursor hot spot in bitmap (-16 to 16)

  • ES:DX = Pointer to cursor bitmap

Outputs
  • (None)

The hot spot is a term given to the pixel location within the mouse cursor image whose coordinate on the screen is the same as the position of the mouse cursor. Essentially, this hot spot allows us to know where the entire image is located on the screen relative to the mouse position (returned in function 0003h, for instance). Initially, the hot spot is in the upper-left corner of the default mouse cursor (the arrow).

The cursor bitmap can be a 16x16 pixel image which is defined in memory as follows:

Offset Size Description
00h 16 words Screen Mask
20h 16 words Cursor Mask

Each word defines the sixteen pixels of a row, with the rightmost pixel being the least significant bit. The image is defined beginning with the top row of pixels in the image.

The image is formed on the screen by first ANDing the pixels on the screen with the Screen Mask image, then XORing the pixels on the screen with the Cursor Mask image.