Showing posts with label Advanced C Programming. Show all posts
Showing posts with label Advanced C Programming. Show all posts

Monday, February 23, 2009

BSS Section in Process Structure & Its Advantage

BSS Section in Process Structure & Its Advantage

---Global variable's life time is until the completion of the program. But what is the point of having two sections for global variables based on initialized and uninitialized. The point is to reduce the executable size of the program before going to discuss about the BSS section we need to understand what process structure, and how the structure looks in the memory (RAM) at the time of execution.




---I didn't say all process structure will be looks like the above, Its based on the operating system but the above section will be present in all operating system' s process structure(minimum section).

---Here we are to discuss about the BSS section. How it reduces the executable size of the program?

---If we are not initialized the global variables the loader will take care to initialize all global variables to zero (0). The loader is designed in such a way to initialize the global to zero while loading the process into RAM. If we are keeping initialized and uninitialized variable in the same block loader will initialize to zero for all the initialized variables too. Compiler developer decided to divide the global sectioninto two parts

1. DATA
2. .BSS

---All initialized variable will go to DATA section, this will be decided in compilation time itself. But uninitialized variable will go into BSS section. This block will be created at the time of loading so it reduces the executable size of the program.

---If you want to confirm this?
open Microsoft Visual C++ 6.0
Run the below two program using Visual C++ 6.0 see the executable file size


Size of this executable 167,969 bytes

------------------------------------------

size of this executable 48,168,993 bytes

--------------------------------------------




For Building the project



For executing the program


Where ever u saved this file there u can see another one directory called "Debug". if you open the directory you can find the executable file. check the size of the file

Thursday, January 17, 2008

Code Generation

☻ Understanding assembly code of C compiler

Here we are going to discuss about how compiler generates code in assembly language through that some tricks we will play in programming

Consider the below program.

/* Example 1*/
main()
{
int a,b;
printf("Enter a number : ");
scanf("%d", &a);
a / 3;
printf("Quotient: %d and Remainder: %d",_AX, _DX);
}



There is no machine instruction for finding remainder. for finding remainder we need to use div assembly instruction this will place remainder in dx reginster and quotient in ax register

If u want to find remainder and quotient of a particular number no need to use two operator. but after a particular expression we need to take care the result i.e we need to back up the results becuase results will not be in register for long time (i.e) before processor starts to execute next instruction we should back up all the values.


mov ax, the value that we entered
mov bx, divident
idiv bx


quotient will be in AX register
remainder will be in DX register


========================================

This program is some what different, just look at the program then we will discuss
/* Example 2*/
main()
{
fun("I am stalin");
printf("Inside main : %s",_CX);
}

fun(char *p)
{
printf("Inside function : %s\n",p);
*(p + 5) = '?';
*(p + 6) = '\0';
}

output:
Inside function : I am stalin
Inside main : I am ?


inside main we are calling function fun with argument "I am stalin" once control returned from fun function we are printing _CX register its printing "I am ?" how come?

we are passing string "I am stalin" to function fun but compiler will not pass the string as argument instead of that it will store "I am stalin" in DATA block then it will use the base address of the string. how it will send base address to function fun?. compiler generates code like

/* Example 2*/
mov ax, base address
push ax
call fun
pop cx


once control comes back, the address will be in cx register. so we can use the address for printing the string,
that string we changed in function fun. so if we know how compiler generates code, we can do what ever we want. Improving performance of a product plays wonderful role in software development for that programmer should know operating system, computer architecture and compiler understanding.
========================================

Tuesday, January 15, 2008

C Debugging

Hi Friends,
Here we are going to discuss about debugging on three different OS and compiler. The output is based on how compiler written. It’s not based on operating system. So if one could able to predict the output of a compiler, he is good in that compiler. But consider the situation, you are going to attend written test (1st round interview). Most of the companies they won’t specify which compiler’s output they are expecting they simply distribute the question paper they wont specify anything on that question paper you have to ask if you have doubt otherwise they wont tell. I am not saying all programs output will be different some logic the way compiler generates code for that, that’s different.


So here we will take windows, Solaris and Linux.

Windows --- Turbo compiler
Solaris --- GCC compiler
Linux --- GCC compiler



Now we will start understanding programs.

/* Example 1*/

main()
{
int i = 10;
i = ++i + ++i + ++i + ++i;
printf("%d",i);
}

Windows turbo compiler
56

Solaris GCC compiler
50

Linux GCC compiler
51


Explanation:

Turbo c compiler evaluates the above expression as

i = i + 1;
i = i + 1;
i = i + 1;
i = i + 1;


i = i + i + i + i; \\ 14 + 14 + 14 + 14

Solaris GCC compiler evaluates the above expression as


mov ax, 0
inc i;
add ax, i
inc i
add ax, i
inc i
add ax, i
inc i
add ax, i
mov i, ax;


solaris compiler generates code according to the human behaviour thats one by one execution.

Linux GCC compiler evaluates the above expression as


inc i
inc i
mov ax, i
add ax, i
inc i
add ax, i
inc i
add ax, i

Sunday, January 6, 2008

C compiler

1. How local variables organized by compiler?

Just consider the below example for how compiler generates code for local variables. We start with writing a simple c program and we compare with assembly program for understanding compiler’s code generation technique.

main()
{
int a, b, c;
a = 10;
b = 20;

c = a + b;

}

In this program we declared three variables all three are local variables we already know local variables are allocated in STACK block. So compiler should generate code for the three variables.

_main proc near

push bp
mov bp, sp
sub sp, 6

mov word ptr [bp-2], 10
mov word ptr [bp-4], 20

mov ax, word ptr [bp-2]
add ax, word ptr [bp-4]
mov word ptr [bp-6], ax


_end _main



The above code generated by turbo c++ compiler. Everybody may have knowledge about stack, that stack will grow from top to bottom. Stack pointer register holds top address of stack segment when ever memory need to allocate in stack, stack pointer will be decrement by some value. Now we need to allocate 6 bytes in stack because three integer variables need 6 bytes. So compiler generates code like “sub sp, 6”.

bp - 2 ---- variable a
bp - 4 ---- variable b
bp - 6 ---- varaible c


From the above information any one can easily understand.

2. How misusing data types will decrease product performance?

We might have heard this statement "Memory should be used very efficiently".
The way of using memory will cause problem
1. Memory not enough.
2. Performance issue.

First one is ok... because the name itself has meaning but the second one is somewhat difficult right?
That is going to explain now with practical example. Consider the below example
main()
{
int a,b,c;
a = 10;
b = 20;
c = a + b;
}
The above program is right version instead of writing like that programmer written like as follows
main()
{
long a,b,c;
a = 10;
b = 20;
c = a + b;
}
The only difference is the data type declaration. So how come this will decrease performance? Integer size is 2 bytes long byte is 4 byte so we can say unused memory is there so its not efficient way of programming, but this inefficient way of programming how will it decrease the performance? Consider the below assembly code then you will realize.
_main proc near
push bp
mov bp,sp
sub sp,12
;
; {
; long a,b,c;
; a = 10;
;
mov word ptr [bp-2],0
mov word ptr [bp-4],10
;
; b = 20;
;
mov word ptr [bp-6],0
mov word ptr [bp-8],20
;
; c = a + b;
;
mov ax,word ptr [bp-2]
mov dx,word ptr [bp-4]
add dx,word ptr [bp-8]
adc ax,word ptr [bp-6]
mov word ptr [bp-10],ax
mov word ptr [bp-12],dx
_main endp

Long data type is occupies 4 bytes but 16-bit turbo c compiler cannot process 32 bit data so compiler has to generate two 16-bit machine instruction for manipulating one 32-bit data so use of long instead of int will cause compiler to generate multiple instruction so once, number of machine instruction increases time taken to execute those machine instructions will increase so product performance will decrease so programmer should be very careful in declaring variable.


3. How constant string formal arguments passed by compiler?

Consider the below example for understanding the question.

main()
{
printf("Hello World\n");
}


printf is a function "Hello World\n" is the argument of the function. I put a question to the readers that
where the string "Hello World\n" will be stroed? whether it will be stored in stack or data block. while calling
printf function, will compiler put the string in stack or only the base address of the string? If it is base address then where the string will be stored?
all will be explained with the following assembly language

_main proc near
push bp
mov bp,sp
;
; {
; printf("Hello World\n");
;
mov ax,offset DGROUP:s@
push ax
call near ptr _printf
pop cx
;
; }
;
pop bp
ret
_main endp
?debug C E9
_TEXT ends
_DATA segment word public 'DATA'
s@ label byte
db 'Hello World'
db 10
db 0
_DATA ends

DATA block code of assembly language
_DATA segment word public 'DATA'
s@ label byte
db 'Hello World'
db 10
db 0
_DATA ends

so from the above code we can conclude Hello World string is stored in DATA block. and the base address used in printf function call for accessing the string
from the above problem we can conclude writitng constant formal arguments will increase usage of DATA block.

Thursday, January 3, 2008

Device Controller[ My, MCA mini Project]

Device Controller Description

This project is based on embedded system. Software is used to control devices that are attached with hardware kit. System parallel port is used for to control devices. A hardware kit connected with the system parallel port through cable with 25-pin male connector.
This project divided into five modules
(i) Light module
(ii) Fan module
(iii) Timing module
(iv) Mouse module
(v) Main module

Light module
Hardware kit designed to control three bulbs and one fan, in this light module eight routine written for controlling bulbs. We can switch on all the light in signal click.
We can also switch off all the light in signal click. We can also selectively switch on/off each light that are attached with the hardware kit. Parallel port’s data pins are used for controlling light, pin number 3,4,and 5 used for control bulbs.

Routine prototype for light module:
Void switch_on_all_light()
Void switch_on_first_light()
Void switch_on_second_light()
Void switch_on_third_light()
Void switch_off_first_light()
Void switch_off_second_light()
Void switch_off_third_light()
Fan module
This module controls fan motor speed, eight different fan speed achieved in this module. four pins are used for control fan speed and to switch ON/OFF fan, In this module ten routines written for controlling fan.

Routine prototype for fan module:
Void switch_on_fan();
Void switch_off_fan();
Void fan_speed_0();
Void fan_speed_1();
Void fan_speed_2();
Void fan_speed_3();
Void fan_speed_4();
Void fan_speed_5();
Void fan_speed_6();
Void fan_speed_7();
Timing module:
Timing module used to set timings for particular device to switch ON and to reduce or increase fan speed, in this module we just traced system timings, this module keep checks the system time with setting time. If the condition evaluates true, then the module sends signal to hardware kit, and it in turns controls devices.
Timing module prototype
(i) int check_time(int hour,int min)
(ii) void display_time()
(iii) void time_setting_bar()
(iv) void time_setting_bar_left_click()
(v) void time_setting_bar_left_release()
(vi) void time_setting_bar_right_click()
(vii) void time_setting_bar_right_release()
(viii) void time_home_page()
mouse module
Mouse is used for interactive input device, for that we written mouse module
(i) void initmouse()
(ii) void showmouse()
(iii) void hideMouseptr()
(iv) void getmouseposition(int *x,int *y,int *button);
Main module
This module entirely written for user interaction, user can interact with this module and this module in turns sends signal to kernel module, and kernel module in turns sends signal to hardware kit.

Screen shots

















Source code


/*FAN.H header file*/
#include
#include
unsigned char bit_manipulation(unsigned char value)
{
unsigned char bit_value;
bit_value=inportb(0x378);
//printf("Before manipulation %d",bit_value);
bit_value=bit_value value;
//printf("after manipulation %d",bit_value);
return bit_value;
}
void switch_on_fan()
{
unsigned char port_value;
port_value=bit_manipulation(64);
outportb(0x378,port_value);
}
void fan_speed_0()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
//port_value=bit_manipulation(0);
outportb(0x378,port_value);
}
void fan_speed_1()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
port_value=port_value0;
outportb(0x378,port_value);
}
void fan_speed_2()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
port_value=port_value1;
outportb(0x378,port_value);
}
void fan_speed_3()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
port_value=port_value3;
outportb(0x378,port_value);
}
void fan_speed_4()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
port_value=port_value4;
outportb(0x378,port_value);
}
void fan_speed_5()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
port_value=port_value5;
outportb(0x378,port_value);
}
void fan_speed_6()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
port_value=port_value6;
outportb(0x378,port_value);
}
void fan_speed_7()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&120;
port_value=port_value7;
outportb(0x378,port_value);
}
/*void switch_on_second()
{
unsigned char port_value;
port_value=bit_manipulation(16);
outportb(0x378,port_value);
}
void switch_on_third()
{
unsigned char port_value;
port_value=bit_manipulation(8);
outportb(0x378,port_value);
} */
void switch_off_fan()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value&63;
outportb(0x378,port_value);
}
void fan_home_page()
{
mainScreen();

depressedButton(20,200,100,230);
setcolor(BLACK);
outtextxy(26,210," O F F ");
rectbox(20,250,620,400,BLUE);
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(24,254,618,398);
settextstyle(0,0,2);
setcolor(YELLOW);
outtextxy(200,50,"FAN CONTROLLING");
settextstyle(0,0,1);

}
void speed_control_page()
{
pressedButton(25,300,100,340);
setcolor(BLUE);
outtextxy(28,315,"Low Speed");
setcolor(BLACK);
outtextxy(30,332," OFF ");
pressedButton(150,255,225,295);
setcolor(BLUE);
outtextxy(153,265," 1 ");
setcolor(BLACK);
outtextxy(155,280," OFF ");
pressedButton(280,255,355,295);
setcolor(BLUE);
outtextxy(283,265," 2 ");
setcolor(BLACK);
outtextxy(285,280," OFF ");
pressedButton(415,255,490,295);
setcolor(BLUE);
outtextxy(418,265," 3 ");
setcolor(BLACK);
outtextxy(420,280," OFF ");
pressedButton(540,300,615,340);
setcolor(BLUE);
outtextxy(543,310,"High Fast");
setcolor(BLACK);
outtextxy(545,325," OFF ");
pressedButton(150,350,225,390);
setcolor(BLUE);
outtextxy(153,360," 4 ");
setcolor(BLACK);
outtextxy(155,375," OFF ");
pressedButton(280,350,355,390);
setcolor(BLUE);
outtextxy(283,360," 5 ");
setcolor(BLACK);
outtextxy(285,375," OFF ");
pressedButton(415,350,490,390);
setcolor(BLUE);
outtextxy(418,360," 6 ");
setcolor(BLACK);
outtextxy(420,375," OFF ");
}

/*LIGHT.H header file*/


#include
#include
int light1,light2,light3;
void switch_on_all_light()
{
unsigned char port_value;
port_value=bit_manipulation(56);
outportb(0x378,port_value);
}
void switch_on_first_light()
{
unsigned char port_value;
port_value=bit_manipulation(8);
outportb(0x378,port_value);
}
void switch_on_second_light()
{
unsigned char port_value;
port_value=bit_manipulation(16);
outportb(0x378,port_value);
}
void switch_on_third_light()
{
unsigned char port_value;
port_value=bit_manipulation(32);
outportb(0x378,port_value);
}
void switch_off_first_light()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value = port_value & 119;
outportb(0x378,port_value);
}
void switch_off_second_light()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value & 111;
outportb(0x378,port_value);
}
void switch_off_third_light()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value & 95;
outportb(0x378,port_value);
}
void switch_off_all_light()
{
unsigned char port_value;
port_value=inportb(0x378);
port_value=port_value & 71;
outportb(0x378,port_value);
}
void light_home_page()
{
mainScreen();
settextstyle(0,0,2);
setcolor(YELLOW);
outtextxy(150,40," CONTROLLING LIGHTS");
settextstyle(0,0,1);
depressedButton(50,80,150,110);
setcolor(GREEN);
outtextxy(56,88," O N ");
setcolor(LIGHTBLUE);
outtextxy(200,85," First Light");
depressedButton(50,150,150,180);
setcolor(GREEN);
outtextxy(56,158," O N ");
setcolor(LIGHTBLUE);
outtextxy(200,160," Second Light");
depressedButton(50,220,150,250);
setcolor(GREEN);
outtextxy(56,228," O N ");
setcolor(LIGHTBLUE);
outtextxy(200,230," Third Light");
depressedButton(50,290,150,320);
setcolor(GREEN);
outtextxy(56,298," ALL ON ");
setcolor(LIGHTBLUE);
outtextxy(200,300," All Light controlling");
}
/*TIMING.H header file*/

#include
#include
int hour_bar_left,hour_bar_right,HOUR,MINU;
int set_light1,l1_hour,l1_min,set_light2,l2_hour,l2_min,set_light3,l3_hour,l3_min;
int set_low,set_normal,set_high,set_ON_OFF,low_fan_hour,low_fan_min,normal_fan_hour,normal_fan_min,high_fan_hour,high_fan_min;
int ON_OFF_hour,ON_OFF_min;
int check_time(int hour,int min)
{
struct time t;
gettime(&t);
if (t.ti_hour==hour && t.ti_min==min)
return 1;
else
return 0;
}
void display_time()
{
char str[20];
int prev,cur;
struct time t;
gettime(&t);
setfillstyle(SOLID_FILL,BLACK);
bar(510,20,620,40);
sprintf(str,"TIME:%2d:%2d",t.ti_hour,t.ti_min);
rectbox(510,20,620,40,3);
setcolor(9);
outtextxy(520,30,str);
delay(100);
}
void time_setting_bar(int x1,int y1,int x2,int y2)
{
int val;
setfillstyle(SOLID_FILL,WHITE);
bar(x1,y1,x2,y2);
val=(x2-x1)/3;
switch_pressedButton(x1-val,y1,x1-2,y2);
switch_pressedButton(x2+3,y1,x2+val,y2);
}
void time_setting_bar_left_click(int x1,int y1,int x2,int y2)
{
int val;
setfillstyle(SOLID_FILL,WHITE);
bar(x1,y1,x2,y2);
val=(x2-x1)/3;
switch_depressedButton(x1-val,y1,x1-2,y2);
}
void time_setting_bar_left_release(int x1,int y1,int x2,int y2)
{
int val;
setfillstyle(SOLID_FILL,WHITE);
bar(x1,y1,x2,y2);
val=(x2-x1)/3;
switch_pressedButton(x1-val,y1,x1-2,y2);
}
void time_setting_bar_right_click(int x1,int y1,int x2,int y2)
{
int val;
setfillstyle(SOLID_FILL,WHITE);
bar(x1,y1,x2,y2);
val=(x2-x1)/3;
switch_depressedButton(x2+3,y1,x2+val,y2);
}
void time_setting_bar_right_release(int x1,int y1,int x2,int y2)
{
int val;
setfillstyle(SOLID_FILL,WHITE);
bar(x1,y1,x2,y2);
val=(x2-x1)/3;
switch_pressedButton(x2+3,y1,x2+val,y2);
}
void time_home_page()
{
mainScreen();
rectbox(10,10,630,440,WHITE);
settextstyle(0,0,2);
setcolor(YELLOW);
outtextxy(140,50,"TIME SETTING FOR DEVICES");
settextstyle(0,0,1);
setcolor(12);
outtextxy(20,110,"Hour :");
outtextxy(20,160,"Minute :");
time_setting_bar(150,100,250,130);
time_setting_bar(150,150,250,180);
setcolor(12);
rectbox(50,200,480,300,LIGHTBLUE);
outtextxy(60,220,"LIGHT SETTING");
depressedButton(70,250,150,280);
setcolor(BLACK);
outtextxy(80,260,"L_1 OFF");
depressedButton(170,250,250,280);
setcolor(BLACK);
outtextxy(180,260,"L_2 OFF");
depressedButton(270,250,350,280);
setcolor(BLACK);
outtextxy(280,260,"L_3 OFF");
depressedButton(370,250,450,280);
setcolor(BLACK);
outtextxy(380,260,"CLEAR");
setcolor(12);
rectbox(50,300,480,400,LIGHTBLUE);
outtextxy(60,320,"FAN SETTING");
depressedButton(70,340,150,370);
setcolor(BLACK);
outtextxy(80,350," LOW");
depressedButton(170,340,250,370);
setcolor(BLACK);
outtextxy(180,350," NORMAL");
depressedButton(270,340,350,370);
setcolor(BLACK);
outtextxy(280,350," HIGH");
depressedButton(370,340,450,370);
setcolor(BLACK);
outtextxy(380,350," CLEAR ");
depressedButton(460,100,620,130);
setcolor(17);
outtextxy(470,110,"Clear All Timeing");
}
/*MOUSE.H Header file*/


#include
union REGS i,o;
void initMouse()
{
i.x.ax=0;
int86(0x33,&i,&o);
}
void showMouse()
{
i.x.ax=1;
int86(0x33,&i,&o);
}
void hideMouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}
void getMousePosition(int *b,int *x,int *y)
{
i.x.ax=3;
int86(0x33,&i,&o);
*b=o.x.bx;
*x=o.x.cx;
*y=o.x.dx;
}
void restrictMousePtr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
}
/* void mouseLeftPressed(void far *)
{
int x,y,b;
getMousePos(&x,&y,&b);
while(b!=0);
buttonDepressed(
}*/


/* MAININTERFACE.C */

#include
#include
#include
#include "mouse.h"
#define OFF 0
#define ON 1
char str[2];
int SPECIAL_STATUS=OFF;
int all_light=0,light_property=0,fan_property=0,time_property=0;
int fan_on_off_property,FAN_ON_OFF=OFF,fan_speed=0;
void rectbox(int,int,int,int,int);
/*
void depressedButton(int,int,int,int);
void pressedButton(int,int,int,int);
void mainScreen();*/
unsigned char bit_manipulation(unsigned char value);
unsigned char status,status1,status2,status3;
union REGS i,o;
void digitDisplay()
{
int i;
settextstyle(0,0,1);
setcolor(BLACK);
for(i=2;i<620;i+=16) x="5;" y="430;">100)
{
x=5;
y=430;
break;
}
}
}
void pressedButton(int x1,int y1,int x2,int y2)
{
hideMouseptr();
setfillstyle(SOLID_FILL,WHITE);
bar(x1,y1,x2,y2);
setcolor(DARKGRAY);
line(x2,y2,x2,y1);
line(x2+1,y2,x2+1,y1);
line(x2,y2,x1,y2);
line(x2,y2+1,x1,y2+1);
showMouse();
}
void depressedButton(int x1,int y1,int x2,int y2)
{
hideMouseptr();
setfillstyle(SOLID_FILL,WHITE);
bar(x1,y1,x2,y2);
setcolor(DARKGRAY);
line(x1,y1,x1,y2);
line(x1+1,y1,x1+1,y2);
line(x1,y1,x2,y1);
line(x1,y1+1,x2,y1+1);
showMouse();
}

void switch_pressedButton(int x1,int y1,int x2,int y2)
{
hideMouseptr();
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(x1,y1,x2,y2);
setcolor(BLUE);
line(x2,y2,x2,y1);
line(x2+1,y2,x2+1,y1);
line(x2,y2,x1,y2);
line(x2,y2+1,x1,y2+1);
showMouse();
}
void switch_depressedButton(int x1,int y1,int x2,int y2)
{
hideMouseptr();
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(x1,y1,x2,y2);
setcolor(BLUE);
line(x1,y1,x1,y2);
line(x1+1,y1,x1+1,y2);
line(x1,y1,x2,y1);
line(x1,y1+1,x2,y1+1);
showMouse();
}
void mainScreen()
{
rectbox(10,10,630,440,WHITE);
setfillstyle(SOLID_FILL,BLACK);
bar(18,18,624,434);
setfillstyle(SOLID_FILL,LIGHTBLUE);
bar(10,443,632,480);
depressedButton(20,445,100,470);
setcolor(RED);
outtextxy(28,452,"L I G H T");
depressedButton(200,445,280,470);
setcolor(RED);
outtextxy(210,452,"F A N");
depressedButton(380,445,460,470);
setcolor(RED);
outtextxy(390,452,"TIMING");
depressedButton(560,445,620,470);
setcolor(RED);
outtextxy(566,452," EXIT");
settextstyle(0,0,2);
// setcolor(13);
// outtextxy(190,50,"BISHOP HEBER COLLEGE");
settextstyle(0,0,1);
showMouse();
}

void rectbox(int x1,int y1,int x2,int y2,int color)
{
setfillstyle(SOLID_FILL,color);
bar(x1,y1,x2,y1+2);
bar(x1,y1,x1+2,y2);
bar(x2,y1,x2+2,y2);
bar(x1,y2,x2+2,y2+2);
}
#include "light.h"
#include "fan.h"
#include "timing.h"


void speed_zero()
{
hideMouseptr();
speed_control_page();
depressedButton(25,300,100,340);
setcolor(BLUE);
outtextxy(28,315,"Low Speed");
setcolor(RED);
outtextxy(30,332," ON ");
showMouse();
fan_speed=0;
fan_speed_0();
}
void speed_one()
{
hideMouseptr();
speed_control_page();
depressedButton(150,255,225,295);
setcolor(BLUE);
outtextxy(153,265," 1 ");
setcolor(RED);
outtextxy(155,280," ON ");
fan_speed=1;
showMouse();
fan_speed_1();
}
void speed_two()
{
hideMouseptr();
speed_control_page();
depressedButton(280,255,355,295);
setcolor(BLUE);
outtextxy(283,265," 2 ");
setcolor(RED);
outtextxy(285,280," ON ");
fan_speed=2;
showMouse();
fan_speed_2();
}
void speed_three()
{
hideMouseptr();
speed_control_page();
depressedButton(415,255,490,295);
setcolor(BLUE);
outtextxy(418,265," 3 ");
setcolor(RED);
outtextxy(420,280," ON ");
fan_speed=3;
showMouse();
fan_speed_3();
}
void speed_four()
{
hideMouseptr();
speed_control_page();
depressedButton(150,350,225,390);
setcolor(BLUE);
outtextxy(153,360," 4 ");
setcolor(RED);
outtextxy(155,375," ON ");
fan_speed=4;
showMouse();
fan_speed_4();
}
void speed_five()
{
hideMouseptr();
speed_control_page();
depressedButton(280,350,355,390);
setcolor(BLUE);
outtextxy(283,360," 5 ");
setcolor(RED);
outtextxy(285,375," ON ");
fan_speed=5;
showMouse();
fan_speed_5();
}
void speed_six()
{
hideMouseptr();
speed_control_page();
depressedButton(415,350,490,390);
setcolor(BLUE);
outtextxy(418,360," 6 ");
setcolor(RED);
outtextxy(420,375," ON ");
fan_speed=6;
showMouse();
fan_speed_6();
}
void speed_seven()
{
hideMouseptr();
speed_control_page();
depressedButton(540,300,615,340);
setcolor(BLUE);
outtextxy(543,310,"High Fast");
setcolor(RED);
outtextxy(545,325," ON ");
fan_speed=7;
showMouse();
fan_speed_7();
}
void light1_on_logic()
{
hideMouseptr();
pressedButton(50,80,150,110);
setcolor(BLACK);
outtextxy(56,88," O F F ");
showMouse();
switch_on_first_light();
light1=ON;
}
void main()
{
int b,x,y;
int status=1;
int gd=DETECT,gm;
initgraph(&gd,&gm,"");//"d:\cpp\bgi\cgi.bgi");
initMouse();
hideMouseptr();
welComeScreen();
cleardevice();
mainScreen();
outportb(0x378,0);
showMouse();
while(1)
{
getMousePosition(&b,&x,&y);
gotoxy(10,10);
if(x>=560 && x<=620 && y>=445 && y<=470 && b==1 && status3==0) { hideMouseptr(); pressedButton(560,445,620,470); setcolor(BLACK); outtextxy(566,452," EXIT"); outportb(0x378,0); showMouse(); delay(300); exit(0); } if (x>=380 && x<=460 && y>=445 && y<=470 && b==1 && status2==0) { pressedButton(380,445,460,470); hideMouseptr(); setcolor(BLACK); light_property=OFF; fan_property=OFF; time_property=ON; outtextxy(388,452,"TIMING"); time_home_page(); sprintf(str,"%d",HOUR); outtextxy(200,110,str); sprintf(str,"%d",MINU); outtextxy(200,160,str); if(set_light1==ON) { pressedButton(70,250,150,280); setcolor(GREEN); outtextxy(80,260,"L_1 ON"); } if(set_light2==ON) { pressedButton(170,250,250,280); setcolor(GREEN); outtextxy(180,260,"L_2 ON"); } if(set_light3==ON) { pressedButton(270,250,350,280); setcolor(GREEN); outtextxy(280,260,"L_3 ON"); } if(set_low==ON) { pressedButton(70,340,150,370); setcolor(GREEN); outtextxy(80,350," LOW"); } if(set_normal==ON) { pressedButton(170,340,250,370); setcolor(GREEN); outtextxy(180,350," NORMAL "); } if(set_high==ON) { pressedButton(270,340,350,370); setcolor(GREEN); outtextxy(280,350," HIGH"); } if(set_ON_OFF==ON) { } showMouse(); status2=1; } if (x>=380 && x<=460 && y>=445 && y<=470 && b==0 && status2==1) { depressedButton(380,445,460,470); hideMouseptr(); setcolor(RED); outtextxy(390,452,"TIMING"); showMouse(); status2=0; } if(x>=200 && x<=280 && y>=445 && y<=470 && b==1 && status1==0) { pressedButton(200,445,280,470); hideMouseptr(); setcolor(BLACK); outtextxy(210,452,"F A N"); light_property=OFF; fan_property=ON; time_property=OFF; fan_home_page(); if(FAN_ON_OFF==ON) { pressedButton(20,200,100,230); setcolor(GREEN); outtextxy(26,210," O N "); showMouse(); speed_control_page(); switch(fan_speed) { case 0:speed_zero();break; case 1:speed_one();break; case 2:speed_two();break; case 3:speed_three();break; case 4:speed_four();break; case 5:speed_five();break; case 6:speed_six();break; case 7:speed_seven();break; } } status1=1; showMouse(); } if(x>=200 && x<=280 && y>=445 && y<=470 && b==0 && status1==1) { depressedButton(200,445,280,470); hideMouseptr(); setcolor(RED); outtextxy(213,452,"F A N"); showMouse(); status1=0; } getMousePosition(&b,&x,&y); if(x>=20 && x<=100 && y>=445 && y<=470 && b==1 && status==0) { pressedButton(20,445,100,470); hideMouseptr(); setcolor(BLACK); outtextxy(28,452,"L I G H T"); light_property=ON; fan_property=OFF; time_property=OFF; light_home_page(); if(light1==ON) { pressedButton(50,80,150,110); setcolor(BLACK); outtextxy(56,88," O F F "); switch_on_first_light(); light1=ON; } if(light2==ON) { pressedButton(50,150,150,180); setcolor(BLACK); outtextxy(56,158," O F F "); switch_on_second_light(); light2=ON; } if(light3==ON) { pressedButton(50,220,150,250); setcolor(BLACK); outtextxy(56,228," O F F "); switch_on_third_light(); } if(light1==ON && light2==ON && light3==ON) { hideMouseptr(); pressedButton(50,290,150,320); setcolor(BLACK); outtextxy(56,298," ALL OFF"); showMouse(); } status=1; } if(x>=20 && x<=100 && y>=445 && y<=470 && b==0 && status==1) { depressedButton(20,445,100,470); hideMouseptr(); setcolor(RED); outtextxy(30,452,"L I G H T"); showMouse(); status=0; } getMousePosition(&b,&x,&y); if(x>=50 && x<=150 && y>=80 && y<=110 && light1==OFF && b==1 && light_property==ON) { /* hideMouseptr(); pressedButton(50,80,150,110); setcolor(BLACK); outtextxy(56,88," O F F "); showMouse(); switch_on_first_light(); light1=ON;*/ light1_on_logic(); } if(x>=50 && x<=150 && y>=80 && y<=110 && light1==ON && b==2 && light_property==ON) { hideMouseptr(); depressedButton(50,80,150,110); pressedButton(50,290,150,320); setcolor(GREEN); outtextxy(56,88," O N "); setcolor(GREEN); outtextxy(56,298," ALL ON "); all_light=OFF; showMouse(); switch_off_first_light(); light1=OFF; } if(x>=50 && x<=150 && y>=150 && y<=180 && light2==OFF && b==1 && light_property==ON) { hideMouseptr(); pressedButton(50,150,150,180); setcolor(BLACK); outtextxy(56,158," O F F "); showMouse(); switch_on_second_light(); light2=ON; } if(x>=50 && x<=150 && y>=150 && y<=180 && light2==ON && b==2 && light_property==ON) { hideMouseptr(); depressedButton(50,150,150,180); setcolor(GREEN); outtextxy(56,158," O N "); pressedButton(50,290,150,320); setcolor(GREEN); outtextxy(56,298," ALL ON "); showMouse(); switch_off_second_light(); light2=OFF; all_light=OFF; } if(x>=50 && x<=150 && y>=220 && y<=250 && light3==OFF && b==1 && light_property==ON) { hideMouseptr(); pressedButton(50,220,150,250); setcolor(BLACK); outtextxy(56,228," O F F "); showMouse(); switch_on_third_light(); light3=ON; } if(x>=50 && x<=150 && y>=220 && y<=250 && light3==ON && b==2 && light_property==ON) { hideMouseptr(); depressedButton(50,220,150,250); setcolor(GREEN); outtextxy(56,228," O N "); pressedButton(50,290,150,320); setcolor(GREEN); outtextxy(56,298," ALL ON "); all_light=OFF; showMouse(); switch_off_third_light(); light3=OFF; } if(light1==ON &&light2==ON && light3==ON && all_light==OFF && light_property==ON) { hideMouseptr(); pressedButton(50,80,150,110); setcolor(BLACK); outtextxy(56,88," O F F "); pressedButton(50,150,150,180); setcolor(BLACK); outtextxy(56,158," O F F "); pressedButton(50,220,150,250); setcolor(BLACK); outtextxy(56,228," O F F "); pressedButton(50,290,150,320); setcolor(BLACK); outtextxy(56,298," ALL OFF"); showMouse(); switch_on_all_light(); light1=light2=light3=ON; all_light=ON; } if(x>=50 && x<=150 && y>=290 && y<=320 && all_light==0 && b==1 && light_property==ON) { hideMouseptr(); pressedButton(50,80,150,110); setcolor(BLACK); outtextxy(56,88," O F F "); pressedButton(50,150,150,180); setcolor(BLACK); outtextxy(56,158," O F F "); pressedButton(50,220,150,250); setcolor(BLACK); outtextxy(56,228," O F F "); pressedButton(50,290,150,320); setcolor(BLACK); outtextxy(56,298," ALL OFF"); showMouse(); switch_on_all_light(); light1=light2=light3=ON; all_light=ON; } if(x>=50 && x<=150 && y>=290 && y<=320 &&all_light==1 && b==2 && light_property==ON) { hideMouseptr(); depressedButton(50,80,150,110); setcolor(GREEN); outtextxy(56,88," O N "); depressedButton(50,150,150,180); setcolor(GREEN); outtextxy(56,158," O N "); depressedButton(50,220,150,250); setcolor(GREEN); outtextxy(56,228," O N "); depressedButton(50,290,150,320); setcolor(GREEN); outtextxy(56,298," ALL ON "); showMouse(); switch_off_all_light(); light1=light2=light3=OFF; all_light=OFF; } if(x>=20 && x<=100 && y>=200 && y<=230 && fan_property==ON && (b==1 SPECIAL_STATUS==ON) && FAN_ON_OFF==OFF) { hideMouseptr(); pressedButton(20,200,100,230); setcolor(GREEN); outtextxy(26,210," O N "); showMouse(); speed_control_page(); switch_on_fan(); FAN_ON_OFF=ON; switch(fan_speed) { case 0:speed_zero(); pressedButton(25,300,100,340); setcolor(BLUE); outtextxy(28,315,"Low Speed"); setcolor(GREEN); outtextxy(30,332," ON "); break; case 1:speed_one(); depressedButton(150,255,225,295); setcolor(BLUE); outtextxy(153,265," 1 "); setcolor(GREEN); outtextxy(155,280," ON "); break; case 2:speed_two(); depressedButton(280,255,355,295); setcolor(BLUE); outtextxy(283,265," 2 "); setcolor(GREEN); outtextxy(285,280," ON "); break; case 3:speed_three(); pressedButton(415,255,490,295); setcolor(BLUE); outtextxy(418,265," 3 "); setcolor(GREEN); outtextxy(420,280," OFF "); break; case 4:speed_four(); depressedButton(150,350,225,390); setcolor(BLUE); outtextxy(153,360," 4 "); setcolor(GREEN); outtextxy(155,375," ON "); break; case 5:speed_five(); depressedButton(280,350,355,390); setcolor(BLUE); outtextxy(283,360," 5 "); setcolor(GREEN); outtextxy(285,375," ON "); break; case 6:speed_six(); depressedButton(415,350,490,390); setcolor(BLUE); outtextxy(418,360," 6 "); setcolor(GREEN); outtextxy(420,375," ON "); break; case 7:speed_seven(); depressedButton(540,300,615,340); setcolor(BLUE); outtextxy(543,310,"High Fast"); setcolor(GREEN); outtextxy(545,325," ON "); break; } } if(x>=20 && x<=100 && y>=200 && y<=230 && fan_property==ON && b==2 && FAN_ON_OFF==ON) { SPECIAL_STATUS=OFF; hideMouseptr(); depressedButton(20,200,100,230); setcolor(BLACK); outtextxy(26,210," O F F "); switch_off_fan(); showMouse(); fan_home_page(); switch_off_fan(); FAN_ON_OFF=OFF; } if(x>=25 && x<=100 && y>=300 && y<=340 && b==1 && fan_speed != 0 && fan_property==ON && FAN_ON_OFF==ON) { speed_zero(); } if(x>=150 && x<=225 && y>=255 && y<=295 && b==1 && fan_speed != 1 && fan_property==ON && FAN_ON_OFF==ON) { speed_one(); } if(x>=280 && x<=355 && y>=255 && y<=295 && b==1 && fan_speed!=2 && fan_property==ON && FAN_ON_OFF==ON) { speed_two(); } if(x>=415 && x<=490 && y>=255 && y<=295 && b==1 && fan_speed!=3 && fan_property==ON && FAN_ON_OFF==ON) { speed_three(); } if(x>=150 && x<=225 && y>=350 && y<=390 && b==1 && fan_speed!=4 && fan_property==ON && FAN_ON_OFF==ON) { speed_four(); } if(x>=280 && x<=355 && y>=350 && y<=390 && b==1 && fan_speed!=5 && fan_property==ON && FAN_ON_OFF==ON) { speed_five(); } if(x>=415 && x<=490 && y>=350 && y<=390 && b==1 && fan_speed!=6 && fan_property==ON && FAN_ON_OFF==ON) { speed_six(); } if(x>=540 && x<=615 && y>=300 && y<=340 && b==1 && fan_speed!=7 && fan_property==ON && FAN_ON_OFF==ON) { speed_seven(); } if(x>=127 && x<=148 && y>=100 && y<=130 && time_property==ON && b==1 ) { hideMouseptr(); time_setting_bar_left_click(150,100,250,130); showMouse(); delay(50); hideMouseptr(); time_setting_bar_left_release(150,100,250,130); if(HOUR==0) HOUR=23; else HOUR--; sprintf(str,"%d",HOUR); delay(150); outtextxy(200,110,str); showMouse(); } if(x>=253 && x<=286 && y>=100 && y<=130 && time_property==ON && b==1) { hideMouseptr(); time_setting_bar_right_click(150,100,250,130); showMouse(); delay(50); hideMouseptr(); time_setting_bar_right_release(150,100,250,130); if(HOUR==23) HOUR=0; else HOUR++; sprintf(str,"%d",HOUR); delay(150); outtextxy(200,110,str); showMouse(); } if(x>=127 && x<=148 && y>=150 && y<=180 && time_property==ON && b==1 ) { hideMouseptr(); time_setting_bar_left_click(150,150,250,180); showMouse(); delay(50); hideMouseptr(); time_setting_bar_left_release(150,150,250,180); if(MINU==0) MINU=59; else MINU--; sprintf(str,"%d",MINU); delay(150); outtextxy(200,160,str); showMouse(); } if(x>=253 && x<=286 && y>=150 && y<=180 && time_property==ON && b==1) { hideMouseptr(); time_setting_bar_right_click(150,150,250,180); showMouse(); delay(50); hideMouseptr(); time_setting_bar_right_release(150,150,250,180); if(MINU==59) MINU=0; else MINU++; sprintf(str,"%d",MINU); delay(150); outtextxy(200,160,str); showMouse(); } if(x>=70 && x<=150 && y>=250 && y<=280 && time_property==ON && b==1 && set_light1==OFF) { hideMouseptr(); pressedButton(70,250,150,280); setcolor(GREEN); outtextxy(80,260,"L_1 ON"); set_light1=ON; l1_hour=HOUR; l1_min=MINU; showMouse(); } if(set_light1==ON && check_time(l1_hour,l1_min)) { switch_on_first_light(); light1=ON; set_light1=OFF; if(time_property==ON) { hideMouseptr(); depressedButton(70,250,150,280); setcolor(BLACK); outtextxy(80,260,"L_1 OFF"); showMouse(); } if(light_property==ON) { hideMouseptr(); pressedButton(50,80,150,110); setcolor(BLACK); outtextxy(56,88," O F F "); showMouse(); } } if(x>=170 && x<=250 && y>=250 && y<=280 && time_property==ON && b==1 && set_light2==OFF) { hideMouseptr(); pressedButton(170,250,250,280); setcolor(GREEN); outtextxy(180,260,"L_2 ON"); set_light2=ON; l2_hour=HOUR; l2_min=MINU; showMouse(); } if(set_light2==ON && check_time(l2_hour,l2_min)) { switch_on_second_light(); light2=ON; set_light2=OFF; if(time_property==ON) { hideMouseptr(); depressedButton(170,250,250,280); setcolor(BLACK); outtextxy(180,260,"L_2 OFF"); showMouse(); } if(light_property==ON) { hideMouseptr(); pressedButton(50,150,150,180); setcolor(BLACK); outtextxy(56,158," O F F "); showMouse(); } } if(x>=270 && x<=350 && y>=250 && y<=280 && time_property==ON && b==1 && set_light3==OFF) { hideMouseptr(); pressedButton(270,250,350,280); setcolor(GREEN); outtextxy(280,260,"L_3 ON"); set_light3=ON; l3_hour=HOUR; l3_min=MINU; showMouse(); } if(set_light3==ON && check_time(l3_hour,l3_min)) { switch_on_third_light(); light3=ON; set_light3=OFF; if(time_property==ON) { hideMouseptr(); depressedButton(270,250,350,280); setcolor(BLACK); outtextxy(280,260,"L_3 OFF"); showMouse(); } if(light_property==ON) { hideMouseptr(); pressedButton(50,220,150,250); setcolor(BLACK); outtextxy(56,158," O F F "); showMouse(); } } if(x>=370 && x<=450 && y>=250 && y<=280 && time_property==ON && b==1 ) { hideMouseptr(); set_light1=set_light2=set_light3=OFF; pressedButton(370,250,450,280); setcolor(GREEN); outtextxy(380,260,"CLEAR"); delay(100); depressedButton(370,250,450,280); setcolor(BLACK); outtextxy(380,260,"CLEAR"); depressedButton(70,250,150,280); setcolor(BLACK); outtextxy(80,260,"L_1 OFF"); depressedButton(170,250,250,280); setcolor(BLACK); outtextxy(180,260,"L_2 OFF"); depressedButton(270,250,350,280); setcolor(BLACK); outtextxy(280,260,"L_3 OFF"); depressedButton(370,250,450,280); depressedButton(370,250,450,280); setcolor(BLACK); outtextxy(380,260,"CLEAR"); showMouse(); } if(x>=70 && x<=150 && y>=340 && y<=370 && time_property==ON && b==1) { hideMouseptr(); set_low=ON; pressedButton(70,340,150,370); setcolor(GREEN); outtextxy(80,350," LOW"); low_fan_hour=HOUR; low_fan_min=MINU; showMouse(); } if(set_low==ON && check_time(low_fan_hour,low_fan_min)) { FAN_ON_OFF=ON; SPECIAL_STATUS=ON; switch_on_fan(); fan_speed=0; if(time_property==ON) { hideMouseptr(); pressedButton(70,340,150,370); setcolor(BLACK); outtextxy(80,350," LOW"); showMouse(); } set_low=OFF; } if(x>=170 && x<=250 && y>=340 && y<=370 && time_property==ON && b==1) { hideMouseptr(); set_normal=ON; pressedButton(170,340,250,370); setcolor(GREEN); outtextxy(180,350," NORMAL"); normal_fan_hour=HOUR; normal_fan_min=MINU; showMouse(); } if(set_normal==ON && check_time(normal_fan_hour,normal_fan_min)) { FAN_ON_OFF=ON; SPECIAL_STATUS=ON; switch_on_fan(); fan_speed_4(); fan_speed=4; if(time_property==ON) { hideMouseptr(); pressedButton(170,340,250,370); setcolor(BLACK); outtextxy(180,350," NORMAL "); showMouse(); } set_normal=OFF; } if(x>=270 && x<=350 && y>=340 && y<=370 && time_property==ON && b==1) { hideMouseptr(); set_high=ON; pressedButton(270,340,350,370); setcolor(GREEN); outtextxy(280,350," HIGH"); high_fan_hour=HOUR; high_fan_min=MINU; showMouse(); } if(set_high==ON && check_time(high_fan_hour,high_fan_min)) { FAN_ON_OFF=ON; SPECIAL_STATUS=ON; pressedButton(370,340,450,370); setcolor(GREEN); switch_on_fan(); fan_speed_7(); fan_speed=7; if(time_property==ON) { hideMouseptr(); pressedButton(270,340,350,370); setcolor(BLACK); outtextxy(280,350," HIGH "); showMouse(); } set_high=OFF; } if(x>=460 && x<=620 && y>=100 && y<=130 && b==1 && time_property==ON) { hideMouseptr(); pressedButton(460,100,620,130); setcolor(BLACK); outtextxy(470,110,"Clear All Timeing"); delay(150); depressedButton(460,100,620,130); setcolor(17); outtextxy(470,110,"Clear All Timeing"); set_light1=set_light2=set_light3=OFF; set_low=set_normal=set_high=OFF; time_home_page(); sprintf(str,"%d",HOUR); outtextxy(200,110,str); sprintf(str,"%d",MINU); outtextxy(200,160,str); showMouse(); } if(x>=370 && x<=450 && y>=340 && y<=370 && b==1 && time_property==ON) { hideMouseptr(); set_low=set_normal=set_high=OFF; pressedButton(370,340,450,370); setcolor(GREEN); delay(150); depressedButton(370,340,450,370); setcolor(BLACK); outtextxy(380,350," CLEAR "); depressedButton(70,340,150,370); setcolor(BLACK); outtextxy(80,350," LOW"); depressedButton(170,340,250,370); setcolor(BLACK); outtextxy(180,350," NORMAL"); depressedButton(270,340,350,370); setcolor(BLACK); outtextxy(280,350," HIGH"); depressedButton(370,340,450,370); setcolor(BLACK); outtextxy(380,350," CLEAR "); showMouse(); } if(time_property==ON) display_time(); } }

System Side Programming in Turbo C

/* 1.INTERRUPT TO READ BASE MEMORY SIZE */
int baseMemory()
{
int memsize;
asm int 18h
asm mov memsize,ax
return(640);
/* return (memsize); */
}

/* 2.INTERRUPT TO READ CURSOR POSITION */
void setCursorPos()
{
i.h.ah=2;
i.h.dh=20;
i.h.dl=6;
int86(0x16,&i,&o);
}

/* 3.INTERRUPT TO READ CURSOR POSITION */
void readCursorPos(unsigned char *page_no,unsigned char *start_line,unsigned char *end_line,unsigned char *row,unsigned char *coloumn)
{
unsigned char val,sl,el,r,c;
val=*page_no;
asm mov ah,0x03
asm mov bh,val
asm int 10h
asm mov sl,ch
asm mov el,cl
asm mov r,dh
asm mov c,dl
*start_line=sl;
*end_line=el;
*row=r;
*coloumn=c;
}


/* 4.INTERRUPT TO SET VIDEO MODE */
void setVideoMode(unsigned char videomode)
{
asm mov ah,0h
asm mov al,videomode
asm int 10h
}

/* 5.INTERRUPT TO SET CURSOR SIZE */
void setCursorSize(unsigned char start_scan_line,unsigned char end_scan_line)
{
asm mov ah,0x01
asm mov ch,start_scan_line
asm mov cl,end_scan_line
asm int 10h
}


/* 6.INTERRUPT TO SCROLL WINDOW UP */
void scrollWindowUp(unsigned char no_lines,unsigned char filler,unsigned char upper_row,unsigned char left_coloumn,unsigned char lower_row,unsigned char right_coloumn)
{
asm mov ah,0x06
asm mov al,no_lines
asm mov bh,filler
asm mov ch,upper_row
asm mov cl,left_coloumn
asm mov dh,lower_row
asm mov dl,right_coloumn
asm int 10h
}


/* 7.INTERRUPT TO SCROLL WINDOW DOWN */
void scrollWindowDown(unsigned char no_lines,unsigned char filler,unsigned char upper_row,unsigned char left_coloumn,unsigned char lower_row,unsigned char right_coloumn)
{
asm mov ah,0x07
asm mov al,no_lines
asm mov bh,filler
asm mov ch,upper_row
asm mov cl,left_coloumn
asm mov dh,lower_row
asm mov dl,right_coloumn
asm int 10h
}

/* 8.INTERRUPT TO PRINT SCREEN */
void printScreen()
{
asm int 5h
}


/* 9.INTERRUPT TO WRITE PIXEL */
void writePixel(pixel_value,pixel_row,pixel_col,page)
unsigned char pixel_value,page;
unsigned int pixel_row,pixel_col;
{
asm mov ah,0x0c
asm mov al,pixel_value
asm mov cx,pixel_col
asm mov dx,pixel_row
asm mov bh,page
asm int 10h
}


/* 10.INTERRUPT TO READ PIXEL */
void readPixel(unsigned char *pixel_value,unsigned int pixel_row,unsigned int pixel_col,unsigned char page)
{
unsigned char val;
asm mov ah,0x0d
asm mov cx,pixel_col
asm mov dx,pixel_row
asm mov bh,page
asm int 10h
asm mov val,al
*pixel_value=val;
}


/* 11.INTERRUPT TO GET PERIPHERAL EQUIPMENT LIST */
int getPeripheralEquipList()
{
int k;
asm int 11h
asm mov k,ax
return(k);
}



/* 12.INTERRUPT TO GET DOS VERSION */
int getDosVersion()
{
asm mov ah,0x30
asm int 21h
return(5);
/* return _AH; */
}


/* 13.INTERRUPT TO SET CTRL BREAK OFF */
int set_CTRL_BREAK_OFF()
{
asm mov ah,0x33
asm mov al,1
asm mov dl,0
asm int 21h
if(_DL==0)
return 1;
else
return 0;
}


/* 14.INTERRUPT TO SET CTRL BREAK ON */
int set_CTRL_BREAK_ON()
{
asm mov ah,0x33
asm mov al,1
asm mov dl,1
asm int 21h
if(_DL==1)
return 1;
else
return 0;
}


/* 15.INTERRUPT TO REBOOT */
void reBoot()
{
asm int 0x19;
}

/*void reStart()
{
void far (*p)();
p=0xffff0000;
(*p)();
} */

/* 16.INTERRUPT TO GET EXTENDED MEMORY SIZE */
void get_Extended_Memory_Size()
{
asm mov ah,0x88
asm int 15h
}


/* 17.INTERRUPT TO READ SCAN ASCII CODE */
void readScanAscii(unsigned char *scan,unsigned char *ascii)
{
unsigned char sc,as;
asm mov ah,0x00
asm int 0x16
asm mov sc,ah
asm mov as,al
*scan=sc;
*ascii=as;
}


/* 18.INTERRUPT TO MAKE DIRECTORY */
int makeDirectory(char *dname)
{
struct SREGS s;
union REGS i,o;
i.h.ah=0x39;
s.ds=FP_SEG(dname);
i.x.dx=FP_OFF(dname);
int86x(0x21,&i,&o,&s);
if (o.x.cflag==0)
return 1;
else
return 0;

}


/* 19.INTERRUPT TO REMOVE DIRECTORY */
int removeDirectory(char *dname)
{
struct SREGS s;
union REGS i,o;
i.h.ah=0x3a;
s.ds=FP_SEG(dname);
i.x.dx=FP_OFF(dname);
int86x(0x21,&i,&o,&s);
if (o.x.cflag==0)
return 1;
else
return 0;
}


/* 20.INTERRUPT TO DELETE FILE */
int deleteFile(char *fname)
{
union REGS i,o;
struct SREGS s;

i.h.ah=0x41;
s.ds=FP_SEG(fname);
i.x.dx=FP_OFF(fname);
int86x(0x21,&i,&o,&s);
if(o.x.cflag==0)
return 1;
else
return 0;
}


/* 21.INTERRUPT TO GET FILE ATTRIBUTE */
int getFileAttribute(char *fname)
{
union REGS i,o;
struct SREGS s;
i.h.ah=0x43;
i.h.al=0;
s.ds=FP_SEG(fname);
i.x.dx=FP_OFF(fname);
int86x(0x21,&i,&o,&s);
if(o.x.cflag==0)
return o.x.cx;
else
return 0;
}


/* 22.INTERRUPT TO SET FILE ATTRIBUTE */
int setFileAttribute(char *fname,int attr)
{
union REGS i,o;
struct SREGS s;
i.h.ah=0x43;
i.h.al=1;
i.x.cx=attr;
s.ds=FP_SEG(fname);
i.x.dx=FP_OFF(fname);
int86x(0x21,&i,&o,&s);

if(o.x.cflag==0)
return o.x.cx;
else
return 0;
}


/* 23.INTERRUPT TO GET CURRENT DIRECTORY */
int getCurrentDirectory(char *dname)
{
union REGS i,o;
struct SREGS s;

i.h.ah=0x47;
i.h.dl=0x4;
s.ds=FP_SEG(dname);
i.x.dx=FP_OFF(dname);
int86x(0x21,&i,&o,&s);
if(o.x.cflag==0)
return 1;
else
return o.x.ax;
}


/* 24.INTERRUPT TO CREATE BUTTON */
void rectbox(int x1,int y1,int x2,int y2,int color)
{
setfillstyle(SOLID_FILL,color);
bar(x1,y1,x2,y1+2);
bar(x1,y1,x1+2,y2);
bar(x2,y1,x2+2,y2);
bar(x1,y2,x2+2,y2+2);
}


/* 25.INTERRUPT TO PRESS A BUTTON */
void pressedButton(int x1,int y1,int x2,int y2,int bcolor,int fillstyle)
{
setfillstyle(SOLID_FILL,fillstyle);
bar(x1,y1,x2,y2);
setcolor(bcolor);
line(x2,y2,x2,y1);
line(x2+1,y2,x2+1,y1);
line(x2,y2,x1,y2);
line(x2,y2+1,x1,y2+1);
}


/* 26.INTERRUPT TO DEPRESSED BUTTON */
void depressedButton(int x1,int y1,int x2,int y2,int bcolor,int fillstyle)
{
setfillstyle(SOLID_FILL,fillstyle);
bar(x1,y1,x2,y2);
setcolor(bcolor);
line(x1,y1,x1,y2);
line(x1+1,y1,x1+1,y2);
line(x1,y1,x2,y1);
line(x1,y1+1,x2,y1+1);
}


/* 27.MOUSE INTERRUPT */
void initMouse()
{
_AX=0;
geninterrupt(0x33);
}


/* 28.INTERRUPT TO SHOW MOUSE POINTER */
void showMousePtr()
{
_AX=1;
geninterrupt(0x33);
}


/* 29.INTERRUPT TO GET MOUSE POINTER */
void getMousePos(int *x,int *y,int *b)
{
union REGS i,o;
i.x.ax=3;
int86(0x33,&i,&o);
*x=o.x.cx;
*y=o.x.dx;
*b=o.x.bx;
}


/* 30.INTERRUPT TO HIDE MOUSE POINTER */
void hideMousePtr()
{
_AX=2;
geninterrupt(0x33);
}


/* 31.INTERRUPT TO SET MOUSE POINTER */
void setMousePtrPos(int x,int y)
{
_AX=4;
_CX=x;
_DX=y;
geninterrupt(0x33);
}


/* 32.INTERRUPT TO RESTRICT MOUSE POINTER */
void restrictMousePtr(int x1,int y1,int x2,int y2)
{
union REGS i,o;
i.x.ax=7;
i.x.cx=x1;
i.x.dx=y1;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=x2;
i.x.dx=y2;
int86(0x33,&i,&o);
}

void setMousePos(int x,int y)
{
_AX=4;
_CX=x;
_DX=y;
geninterrupt(0x33);
}

Wednesday, December 26, 2007