Saturday, May 13, 2017

Playing with mouse using C language

Graphics in C – Playing with Mouse


Mouse is an integral part of any computer. You can interact with mouse using C programming language. C computer language has the ability to interact with hardware.

Before we can start programming the mouse we need some hardware knowledge and knowledge of some C language functions and terms. Please go through these things given below-

REGISTERS:

You all know that the part of computer where all instructions are executed is called CPU (Central Processing Unit) or simply microprocessor. The microprocessors have their own internal storage which is used to temporarily hold the data. This internal storage is called registers. The microprocessor moves the data which is to be processed from memory to a register. After processing the data, result is returned to memory. Intermediate results too are held in registers.

In the 8086 family of microprocessors, there are fourteen registers, each with some specific purpose. These are classified as below-

1.   General Purpose Registers:

These registers are four in number. These are called data registers too and are used to temporarily hold the operands and intermediate results of arithmetic and logical operations. These registers are given below-
AX       Accumulator
BX       Base
CX       Count
DX       Data

Each of the four registers is 16 bits in size and further subdivided and separately used as two 8-bit registers. The high order 8-bit registers are known as AH, BH, CH, and DH, and the low order 8-bit registers are known as AL, BL, CL, and DL.

2.   Segment Registers:

Every memory location is referred by two values. One is 16-bit segment value and second is 16-bit offset within the segment. Specific segments of memory are identified by segment registers, and offset within a segment are identified by offset registers. Memory is divided in four segments, hence, there are four segment registers. These are given below-

CS à  This register identifies the code segment, which contains the program to be executed.
DS à   This register refers to the data segment
ES à    Refers to extra segment
DS and ES stores the data  used in the program
SS à     Refers the stack segment

3.   Offset Registers :

 Five offset registers, which contain the offset within the segment, are used with the segment registers to refer an address in memory. These are given below-

IP à The Instruction Pointer, also called program counter. It contains the offset within the code segment where the current program is executing. It is used with CS segment register to track the location of the next instruction to be executed.

SP à Stack Pointer or stack register
BP à Base Pointer
These two pointers provide the offset within the stack segment
SI à Source Index
DI à Destination Index
These registers are used for the general purpose addressing of data.

4.   Flags Register:

A flag is a flip-flop which indicates some condition produced by the execution of an instruction or controls the certain operation of EU(Execution Unit). A 16-bit flag register in EU contains  nine 1-bit active flags. Seven bits are unused. These flags are divided into two categories-

Conditional Flags:

These are set by some conditions generated as a result of the last mathematical or logical instructions executed. These are given below-
CF à Carry Flag
PF à Parity Flag
AF à Auxiliary Flag
ZF à Zero Flag
SF à Sign Flag
OF à Overflow Flag

Control Flags:

These flags are set or reset deliberately to control the operation of the EU. These are given below-
TF à Trap Flag
IF à Interrupt enable Flag
DF à Direction Flag (string)

INTERRUPTS:

An interrupt is a signal to the microprocessor which tells the microprocessor that its immediate attention is needed. Whatever task the microprocessor is doing is halted and some other task for which interruption is occurred is executed.
The signal may be generated through either hardware or software. For example, if a key is hit, a hardware interrupt has occurred, and function int86() in C generates a software interrupt.

Numbers for Interrupts:

Every interrupt has a specific number. For example, on hitting a key on the keyboard, interrupt number 9 is generated. The 8086 family supports total 256 interrupts, numbered from 0 to 255.

ROM-BIOS Functions or Routines:

These functions are present in memory whenever the computer is on. These functions work directly with the hardware and peripheral devices and perform some of the system’s most fundamental tasks, for example, reading data from the disk. Unlike other functions, ROM-BIOS functions do not have names. They are called by obtaining their addresses in memory and passing control to these addresses. How? All ROM-BIOS functions are invoked through interrupts. Each interrupt instruction selects a particular address in IVT and passes control to this address in memory. The addresses of various ROM-BIOS functions are stored in low area of memory, in Interrupt Vector Table (IVT). Each address is four bytes long and is in the form of segment:offset.

UNIONS:

Unions are the derived data types, and are used to group a number of different types of variables together. Unlike structures, a union enables us to treat the same space in memory as a number of different variables. In other words, in a union, a section of memory can be treated as a variable of one type at one time, and the same memory location can be treated as a different variable of different type at another time. A union can be nested in another union. A union can have structures too.

STRUCTURE BYTEREGS & WORDREGS:

These are structures defined in dos.h file of C language. These structures are used for storing byte and word registers.

Declaration:

struct BYTEREGS {
      unsigned char al, ah, bl, bh;
      unsigned char cl, ch, dl, dh;
};

struct WORDREGS {
      unsigned int ax, bx, cx, dx;
      unsigned int si, di, cflag, flags;
};

UNION REGS:

The union REGS is used to pass information to and from the function int86(), int86x(), etc. This has also been defined in dos.h file of C language.

union REGS {
        struct WORDREGS x;
        struct BYTEREGS h;
};

FUNCTION int86():

This function is used to make a software interrupt occur.

Declaration:

int int86(int intno, union REGS *inregs, union REGS *outregs);

Where ‘intno’ is the interrupt number corresponding to the ROM-BIOS function to be invoked, ‘*inregs’ and ‘*outregs’ are the pointers to the union structure variables defined above.

Syntax for calling this function is given below-

int86(intno, &inregs, &outregs);

Before executing the software interrupt, function int86() copies register values from ‘inregs’ into the registers.

After executing the software interrupt, this function copies the following-

-      current register values to ‘outregs
-      status of the carry flag to the ‘x.cflag’ field in ‘outregs
-      value of 8086 flags register to the ‘x.flags’ field in ‘outregs
If the carry flag is set, it indicates that an error has occurred. ‘inregs’ can point to the same structure that ‘outregs’ point to.

SERVICES:

There are so many services available under every interrupt number. Here we will focus our attention on mouse. The interrupt number for mouse is ‘33h’ in hexadecimal and ‘51’ in decimal number system. Anything to be done with mouse is done by these services available under interrupt number 33h. Some of these services and their tasks are given below- 

Interrupt : 33h

Service : 0
Call with AX = 0
Returns
AX = 0000 (if mouse driver is not installed)
AX = FFFFh (if mouse driver is installed)
BX = number of buttons
- resets mouse to default driver values:

à mouse is positioned to screen center
à mouse cursor is reset and hidden
à no interrupts are enabled (mask = 0)

Service : 1
Shows mouse pointer
Call with AX = 1
Returns nothing

Service : 2
Hides mouse pointer
Call with AX = 2
Returns nothing

Service : 3
Gets mouse position and button status
Call with AX = 3
Returns BX = Mouse button status (1=Corresponding button  pressed)
Bit 0 à left button is down
Bit 1 à right button is down
Bit 2 à center button is down (if present)
Bits 3-15 à Cleared to 0.
CX = x coordinate
DX = y coordinate

Service : 4
Set mouse pointer position
Call with AX = 4
CX = x coordinate
DX = y coordinate
Returns nothing

Service : 5
Gets mouse button press information
Call with AX = 5
BX = 0 (Left button)
          1 (Right button)
Returns-
BX = Count of button press (0-32767). Set to 0 after call
CX = Horizontal position of last press
DX = Vertical position of last press
AX = Status
        Bit 0à Left button (1 = button is pressed)
        Bit 1à Right button (1 = button is pressed)

Service : 6
Gets mouse button release information
Call with AX = 6
BX = 0 (Left button)
          1 (Right button)
Returns-
BX = Count of button releases (0-32767)
Set to 0 after call
CX = Horizontal position at last release
DX = Vertical position at last release
AX = Status
           Bit 0à Left button (1 = button is pressed)
           Bit 1à Right button (1 = button is pressed)

Service : 7
Sets horizontal limits for pointer
Call with AX = 7
CX = minimum x coordinate
DX = maximum x coordinate
Returns nothing
à Restricts mouse horizontal movement to window
à If min value is greater than max value they are swapped

Service : 8
Sets vertical limit for pointer
Call with AX = 8
CX = minimum y coordinate
DX = maximum y coordinate
Returns nothing
à Restricts mouse vertical movement to window
à If min value is greater than max value they are swapped

Service : 9
Sets mouse graphics cursor
Call with AX = 9
BX = horizontal hot spot (-16 to 16)
CX = vertical hot spot (-16 to 16)
ES : DX = pointer to screen and cursor masks

Returns nothing

è 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.
è Bytes 0-7 form the screen mask bitmap
è Bytes 8-F form the cursor mask bitmap

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. This hot spot allows us to know where the entire image is located on the screen relative to the mouse position.  Initially, the hot spot is in the upper-left corner of the default mouse cursor (the arrow).

That’s all you need to know for mouse programming. In the next post you’ll see an interesting C language program.


You would also like these programs given below:



No comments:

Post a Comment

Please give your comment