5. Low Level Disk I/O
main ( int argc, char *argv[ ] )
{
int in
if ( argc != 3 )
{ puts ( ”Improper Usage !” ) ;
puts ( ”Correct Usage : c filecopy source target” ) ;
} exit( ) ;
open ( ) ;
if ( in == -1 )
{
puts ( ”Unable to open” ) ;
exit( ) ; }out = open ( argv[2], O_WRONLY | O_BINARY
| O_CREAT ) ; CCoonntt....
, out ;
CCffiilleeccooppyy PPRR11..CC NNEEWWPPRR11..CC
in = argv[1], O_RDONLY | O_BINARY
6. ....CCoonntt
if ( out == -1 )
{
puts ( ”Unable to open” ) ;
close ( in ) ;
}w
hile ( read ( ) )
write ( out, buffer, n ) ;
close ( in ) ;
close ( out ) ;
}
exit( ) ;
( n = in, buffer, 512 ) != 0
# include ”fcntl.h”
# include ”types.h”
# include ”stat.h”
int n ;
char buffer[512] ;
# include ”fcntl.h”
# include ”types.h”
# include ”stat.h”
int n ;
char buffer[512] ;
7. Interaction with Hardware
Through C
Using Standard Library Functions
Directly programming The Hardware
Calling BIOS / DOS Routines
8. Steps in Calling BIOS / Dos routine
Issue an interrupt
Find interrupt number
Keyboard 9
Disk PPuusshh
19
Printer 23
Mouse 51
VDU 16
Timer 8
Let BIOS / DOS routine get executed
Collect values returned by BIOS/ DOS routines into
ordinary variables
PPoopp
Multiply interrupt no. by 4
Pick address of BIOS / DOS routine from IVT
Store current CPU register values in stack
Setup CPU registers with values required by
BIOS /DOS routine being called
Transfer control to BIOS / DOS routine
Restore values from stack back into CPU registers
Resume original interrupted activity
9. CPU Registers
Data Registers
AX - Accumulator
BX - Base
CX - Count
DX - Data
Segment Registers
CS - Code Segment
DS - Data Segment
ES - Extra Segment
SS - Stack Segment
Offset Registers
BP - Base Pointer
SP - Stack Pointer
IP - Instruction Pointer
SI - Source index
DI - Destination index
Flag Registers
AX
AH AL
- AH, Al
- BH, BL
- CH, CL
- DH, DL
11. Structures
z.i z.ch[0] z.ch[1]
main( )
{
struct a
{
int i ;
char ch[2] ;
} ;
struct a z ;
z.i = 512 ;
printf ( ”%d %d %d”, z.i, z.ch[0], z.ch[1] ) ;
printf ( ”%d”, sizeof ( z ) ) ;
}
4
512 G G
Size of a structure is sum of sizes of
its elements.
Size of a structure is sum of sizes of
its elements.
12. Unions z.i
00000000 00000010
Permits access to
same memory
locations in more
than one way
main( ) z.ch[0] z.ch[1]
{
union a
{
int i ;
char ch[2] ;
} ;
union a z ;
z.i = 512 ;
printf ( ”%d”, sizeof ( z ) ) ;
Low High
00000010 00000000
printf ( ”%d %d %d”, z.i, z.ch[0], z.ch[1] ) ;
}
2
0 2
High Low
512
Binary of 512
Permits access to
same memory
locations in more
than one way
13. How Many Bytes
zz..cchh[[00]]
union a
{
double d ;
z.d
float f[2] ;
} ; char ch[5] ;
union a z ; z.f[0] z.f[1]
zz..cchh[[11]]
Size of a union is size of the longest
element of the union.
Size of a union is size of the longest
element of the union.
14. Positioning Cursor
main( )
{ struct WORDREGS
{
int ax, bx, cx, dx, si, di, cflag, flags ;
} ;
struct BYTEREGS
{
char al, ah, bl, bh, cl, ch, dl, dh ;
} ;
union REGS
{
struct WORDREGS x ;
struct } ; BYTEREGS h ;
union REGS i, o ;
clrscr( ) ;
i.h.dh = 10 ; i.h.dl = 20 ; i.h.ah = 2 ; i.h.bh = 0 ;
int86 ( 16, i, o ) ;
printf ( ”Hi” ) ;
}
15. gotorc( )
# include ”dos.h”
main( )
{
}
}
printf ( ”Hi” ) ;
union REGS i, o ;
i.h.ah = 6 ; i.h.al = 0 ;
Intensity
bit
Color Byte
B/g F/g
i.h.bh = 7 ;
i.h.ch = 0 ;
int86 ( 16, i, 0 ) ;
}
cls( ) ;
gotorc ( 10, 20 ) ;
gotorc ( int r, int c )
{
union REGS i, o ;
i.h.ah = 2 ; i.h.bh = 0 ;
i.h.dh = r ; i.h.dl = c ;
int86 ( 16, i, 0 ) ;
cls( )
{
i.h.cl = 0 ;
i.h.dh = 24 ; i.h.dl = 79 ;
VDU Page no.
Lines to scroll, 0 to clear
Filler Attribute
Blinking
bit
R G B R G B
16. Delete Files
# include ”dos.h”
main( )
{
union REGS i, o ;
char fname[67] ;
printf ( ”Enter file name” ) ;
gets ( fname ) ;
i.h.ah = 65 ;
i.x.dx = fname ;
intdos ( i, o ) ;
if ( o.x.cflag == 0 )
printf ( ”Successful’ ) ;
else
printf ( ”Unable to delete” ) ;
}
17. Renaming Files
# include ”dos.h”
main( )
{
union REGS i, o ;
char old[67], new[67] ;
puts ( ”Old Name:” ) ;
gets ( old ) ;
puts ( ”New Name” ) ;
gets ( new ) ;
i.h.ah = 86 ;
i.x.dx = old ;
intdos ( i, o ) ;
i.x.di = new ;
if ( o.x.cflag == 0 )
printf ( ”Successful” ) ;
else
printf ( ”Unable to rename” ) ;
}