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);
}

Some Good Questions

  1. How can I convert integers to binary or hexadecimal?
  2. How can I call a function, given its name as a string?
  3. How do I access command-line arguments?
  4. How can I return multiple values from a function?
  5. How can I invoke another program from within a C program?
  6. How can I access memory located at a certain address?
  7. How can I allocate arrays or structures bigger than 64K?
  8. How can I find out how much memory is available?
  9. How can I read a directory in a C program?
  10. How can I increase the allowable number of simultaneously open files?
  11. What’s wrong with the call fopen (”c:\newdir\file.dat”, “r”)?
  12. How do you write a program which produces its own source code as its output?
  13. How can I find the day of the week given the date?
  14. Why doesn’t C have nested functions?
  15. What is the most efficient way to count the number of bits which are set in a value?

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();
}

Creating CON folder in windows

CON is a reserve word in windows operating system. that's why we can not create the folder named as CON.

But by command prompt we can do this
STEP1: goto command prompt
STEP2: type in prompt e:\> "mkdir \\.\e:\con"
STEP3: verify by typing "dir \\.\e:\con"
STEP4: to delete the file or folder "rmdir \\.\e:\con"

following file names in Windows are reserved because they represent devices:
con, con.* -> the console
prn, prn.* -> the default printer, as a character device
aux, aux.* -> the default serial terminal, as a character device
lpt1, lpt2, lpt3, lpt4, lpt5, lpt6, lpt7, lpt8, lpt9 -> the parallel ports, as character devices
com1, com2, com3, com4, com5, com6, com7, com8, com9 -> the COM ports
nul, nul.* -> the NUL or "waste bit bucket" or "black hole for bits" or "/dev/null" device

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];
}