Showing posts with label Example. Show all posts
Showing posts with label Example. Show all posts

Monday, February 7, 2011

System Level Programming by 'C' Programming Language


In the header file dos.h there are two important structures and union (Remember this structure)

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

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

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

There is function int86() which has been defined in  dos.h header file. It is general 8086 software interrupt interface. It will better to explain it by an example.


Write a c program to display mouse pointer?
Answer:

#include
#include
void main()
{
union REGS i,o;
i.x.ax=1;
int86(0x33,&i,&o);
getch();
}

Explanation: To write such program interrupt table is necessary. 




















Complete interrupt table
It is a small part of interrupt table. It has four field input, output, service number and purpose,now see the first line which is inside the rectangle. To display the mouse pointer assign ax equal to 1 i.e. service number while ax is define in the WORDREGS

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


and WORDRGS is define in the union REGS

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

So to access the ax first declare a variable of REGS i.e. REGS i,o;
To access the ax write i.x.ax (We are using structure variable i because ax is input, see interrupt table)
So to display mouse pointer assign the value of service number:
i.x.ax=1;
 
To give the this information to microprocessor, We use int86 function. It has three parameters
1. Interrupt number i.e. 0x33
2. union REGS *inputregiste i.e. &i
3. union REGS *outputregiste i.e. &o;

So write: int86 (0x33, &i, &o);

 

Thursday, December 30, 2010

Can you write a program whice invoke another C program ?

#include<dos.h>
void main()
{
system("tcc another.c");
system("another.exe");
}

Can you write a program which produces own source code as output ?

#include <stdio.h>
void main()
 {
   FILE *fd;
   int c;

   fd= fopen("./itself.c","r");
   while ( (c=fgetc(fd)) != EOF)
     {
       printf("%c", c);
     }
  
fclose(fd);
}

Tuesday, December 28, 2010

Reading and Writing Serial Port



#include<dos.h>
void main()
  {
    char data;
    int choice;
   
  printf("Enter the choice to send or receive from COM1::");
  scanf("%d",&choice);


  if(choice==1)
    {
      printf("Enter data to send::");
      scanf("%c",data);
      inportb(0x03f8,data);
    }
  else
    {
       data=outport(0x3f8);
       printf("reding from  COM1::%d",data);
    }
getch();
}

Write a C program to convert Decimal to Hex


void dec2hex(int num)
{
char hx[16]={"0123456789ABCDEF"};
if(num>15)
{
cout<<(num/16);
dec2hex(num%16);
}
else
cout<<hx[num];
}

Power Of Two

Can you write a program to find, if the number if power of 2 or not..

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter The Number:";
cin>>num;
if(! (num &(num-1)))
cout<<"\nNumber is Power of 2";
else
cout<<"\nNumber is NOT Power of 2";
getch();
}

Write a C program to show day and time

#include <stdio.h>
void main()
{
  printf("Date : %s\n", __DATE__);
  printf("Time : %s\n", __TIME__);
}

Can you Count the Number of bits set in an integer ?

First method:

#include<stdio.h>
void main()
{
int i,num,sum=0;
printf("enter the number:");
scanf("%d",&num);
 while(num)
   {
   sum=sum+num%2;
   num/=2;
   }
printf("\n sum is:%d",sum);
getch();
}

Second method:

#include<stdio.h>
void main()
{
int i,num,sum=0;
printf("enter the number:");
scanf("%d",&num);
 while(num)
   {
   sum=sum+ (num & 1);
   num=num>>1;
   }
printf("\n sum is:%d",sum);
getch();
}

Monday, December 27, 2010

Can you tell if the given number is even or odd ?

You have to tell the given number is even or odd, but the condition is you can not use any conditional statements or looping statements (if, for, while, goto etc)..

# include<stdio.h>
void main()
{
char *a[2]={"even", "odd"};
int num;
printf("enter Number:");
scanf("%d",&num);

printf("The number is %s",a[num%2]);
}

A "Hello World" program

Why HelloWorld ??
If you will see any C/Cpp book, you will find the first program will always start with a "HelloWorld" program.. So I thought why not I also start with the same but in a different way...

This program is going to print "Hello World" but the "Hello" will be in the IF part of the conditional IF-ELSE  and "World" will be in the ELSE part..
Interesting na ?

#include<stdio.h>
void main()
{
    if(!printf("Hello"))
      {}
    else
      {
      printf("World");
      }
}