406 Programming and Data Structures
{
ctoar nattfttfSJ;
in t age;
union r e s u lt p e r f;
} data;
c lrs c r O ;
printf ("Size of union; %dn",$izeof(data.perf));
printf ("Size of Structure: %dn", sizeoffdata));
i
Q.UXBJI;
Size of union: 2
Size of Structure : 19
Explanation Union contains two variables of data types in t & char respectively. The in t &
char requires 2 & 1 bytes respectively for storage. According to the theory the union reserves two
bytes because in t takes more space than char data type.
The structure res are defined immediately after union, which contains data type ch ar, i n t , &
union variable. The size of structure is printed which are nothing but sum of 15 bytes of char
array, two bytes o f in t and two bytes of union variable perf. Thus, the total size of structure is
19.
12.12 CALLING BIOS AND DOS SERVICES
We can access BIOS and DOS services through C language. Few simple examples are illustrated by
making use of BIOS and DOS services. The programmer does not have direct access to the CPU
registers. However, the C language provides an interface that allows the programmer to access any
BIOS or DOS service. The C language defines a union type called "REGS" and structure type called
"SREGS" in the file nDOS.H" which escorts the C compiler. These data types are used to pass
parameters to the CPU registers. In order to have access to these definitions the programmer has to
include "d o s . h" header file.
The union type "REGS" contains the CPU registers. These registers are used for holding the data
temporarily. The microprocessors 8086, 8088, 80186, 80286, and 80386 use a variety of registers for
arithmetic and logical operations. Also they are used to receive instructions and pass data to and
from memory.
The CPU registers are 16 bits in size. They are AX, BX, CX, DX, SI, Dl, and CFLAG, which is carry
flag. The size of CFLAG is also 16 bits. The structure that defines these 16 bits registers is called %x '.
The ' x f stands for a register pair. Size of each register is 8 bits. Thus, the size of pair registers ' x ' is
16 bits.
The union also allows us to refer 8 bit registers of above type processors. These 8 bits registers are
AL, AH, BL, BH, CL, CH, DL and DH. The structure that defines these 8 bit registers is called 'h'.
Here, 'h' stands for a half register i.e.8 bit register.
There are also segment registers in the above processors. The size of segment registers is 16 bits.
These segment registers DS, ES, CS and SS. The structure type "SREGS" defines these segment
registers.
Structure and Union 407
The C language provides the functions such as intdos ( ) , intdox () and segread () to access dos
services. The intdos () is used for calling dos interrupt. The intdos () invokes aDOS function, by
issuing software interrupt INT 21H.
We can also invoke BIOS services by using C functions such as in t 8 6 () andint86x().Theint86 ()
stands for 8086 interrupt. The in t 8 6 () functions invoke a BIOS function by issuing a software interrupt.
The in t 8 6 () function can be invoked with the following three arguments.
Interrupt type number corresponding to RO M -B IO S service.
Two arguments of U N IO N type REGS. They are %x ' and 'h '.
The in t 8 6 () function takes the values of input registers from one "REGS" union and returns the
output registers in another. The int86x () function is similar to int86 ( ) . The difference is
int86x () requires an input argument in DS or ES register. Given below are few examples of
int86 () functions.
12.29 Write a program to find memory size of the computer.
# include <stdio.h>
# include <conio.h>
# include <dos.h>
/* Finding Memory Size */
void main ()
{
union REGS in,out;
int86 (18r&in,&out);
c l r s c r ( ) ;
printf (" n Total Mem ory = % d KB",outjc.ax);
}
OUTPUT:
Total Memory = 640 KB
Explanation In the above program in and out are objects of type union REGS. In this example
we need not require to send any values to the R O M -B IO S function. Thus, nothing is to be passed to
any of the registers before invoking in t 8 6 (). Here, the first argument 18 is the interrupt number
followed by two arguments of union REGS. The result obtained here is the memory size displayed by
calling ax register. Here, extended memory of PC is displayed.
1230 Write a program to display the system time at specified cursor position.
# include <stdio.h>
# include <conio.h>
# include <dos.h>
/* Positioning Cursor on'the console */
void main()
{
union REGS in,out;
in.h. ah-2;
408 Programming and Data Structures
in.h.dhp20;
in.h.£1=15;
int86 (16,&in,&out);
printf (" % s " , _ T I M E _ );
}
Explanation In the above program the register ah contains service number, dh contains row number,
and dl contains column number. Here, interrupt number is 16 which is placed in the in t 8 6 ()
function with two union variables. Thus, system time is displayed at the specified position.
12.31 Write a program to change the cursor in different sizes.
# include <stdio.h>
# include <conio*h>
# include <dos.h>
/* Cursor size s */
void mainO
{
union REGS in,out;
in.h.ah=0x01;
in.h.ch=0;
in.h.cl=10;
int86(0xl0,&in,Scout);
getcheO
I
Explanation In the above program 0x01 is the service number under interrupt number 0x10. The CH
register contains starting scan line and CL contains ending scan line. After execution of the function
the cursor is modified.
The programs given below use intdosO function.
12.32 Write a program to create a directory using dos interrupt.
# include <stdio.h>
# include <conio.h>
# inclu de%<dos.h>
void main()
{
union REGS in ,o u t;
char dir [11];
clrscrO ;
puts(n Enter Directory Name : " );
scanf ("% s",d ir);
in.x.dx=(int) &dir;
in.h.ah=0x39;
Structure and Union 409
intdos(&in,&out);
if(outjc.cflag!=0)
printf("Directory %s not created",dir);
else
printf (" n Directory % s created",dir);
I
OUTPUT:
Enter Directory Name : XYZ
Directory XYZ created
Explanation The program prompts to enter the name of the directory to be created. The name
entered is stored in the variable dir. The address of d ir is assigned to register DX. Now, the DX
register points to directory name. The dos service number 0x39 is called to create the directory. If
the carry flag is zero it means the operation is successfully carried otherwise an error occurrs.
Appropriate messages are displayed on the screen.
12.33 Write a program to display the given character on the screen.
# include <stdio.h>
# include <conio.h>
# include <dos.h>
/* displa ys s p e c ifie d character */
void main()
{
union REGS in ,o u t;
in.h.oh-02;
in.h.dl=67;
intdos(Scin,&cout);
I
OUTPUT;
Explanation In the above program the register variable ah is initialized with service number 02
which controls standard output devices. The register variable d l is initialized with ASCII code of
character that is to be displayed on the screen. After execution of intdos () function. The given
character is displayed on the screen.
12.34 Write a program to display the attributes of a file using DOS interrupt
# include <std io.h >
# include <conio.h>
410 Programming and Data Structures
# Include <dos.h>
/* Getting attrribute of a f i l e */
void main ()
{ union RBGS in, out;
char f i le [15];
int mask, j;'
clrscr();
puts( Enter a file name: “);
scanf ("% s",file);
inj:.dx=(int) &file;
in.hM=0;
in.h.ah=0x43;
intdos(&in,&out);
if(outjc.cflag!^0)
printf ("n File not found");
else
{ printf (" n File attributes o f %saren",file);
if(out.h.cl=0)
puts("n Normalfile");
else
{ mask-1;
fo r (f=l,j<=6,-++j)
I
sivitch(outJt.cl & mask)
{
easel : puts (" [* ] Read Only”); break;,
case 2 : puts ( [* ] Hidden “); break;
case 4 : puts ( [* ] System"); break;
case 8 : puts ( [*] Volume L ib el"); break;
case 16: puts ( [* ] Subdirectory"); break;
case 32; puts ("1*1 Archive"); break;
}
tnask=mask*2;
}
I
I
I
OUTPUT;
Enter a file name:
C:IO.SYS
File attributes of C:IO.SYS are
[*] Kead Only
[*] Hidden
I4] System
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232