Accessing VDU memory using C Language

C language provide built in function for print information on the screen. Like printf(), puts() etc. But if we want to print information on the screen without using any built in function so we have to use VDU (Visual Display Unit) memory. The microprocessor doesn’t have the ability to produce image or text directly on the screen. It writes the information on the VDU memory. VDU memory present in the display adapter.

VDU memory has a one base address. The bas address of VDU memory changes from different display adapter. E.g. for VGA/EGA/CGA adapter base address is 0XB8000000 (hex), and for MA adapter base address is 0XB0000000 (hex).

For store single character in to VDU memory needs 2 bytes. First byte for the ASCII (American Standard Code for Information Interchange) value and second byte for the color attributes.

In color attributes first 4 bits for the foreground color, 3 bits for the background color and last one for the blinking.

In console application default screen is arranged as 80 characters (columns) by 25 lines (rows). We can also set default screen arrangement using C language for that read this article.

In below example I write one function for print 0 to 9 using VDU memory.
#include<stdio.h>
#include<conio.h>
char far *vdmem = (char far*)0xB8000000;
void printchar(int,int,char,int);
void main()
{
 int i;
 clrscr();
 for(i=48;i<58;i++)
  printchar(0,i-48,i,112);
 getch();
}
void printchar(int r,int c,char ch,int clr)
{
 char far *v;
 v = vdmem + r * 160 + c * 2; //Calculate Address
 *v = ch; //Store Character on VDU Memory
 v++;
 *v = clr;//Set color attribute
}

printchar() function definition we can say that gotoxy(c,r) is equal to vdmem + r * 160 + c * 2, a represent each character need two bytes, so multiples of 2 with c (column) and one row contains 80 character, so multiples of 160 with r (row).

No comments:

Post a Comment