SlideShare a Scribd company logo
1 of 18
Delete 
 Open a file called “WS.EXE” 
 Skip past first 1701 bytes 
 Copy rest into TEMP.EXE 
 Delete WS.EXE 
 Rename TEMP.EXE to WS.EXE
The Program 
# include ”stdio.h” 
main( ) 
{ 
char e[10] ; 
char s[ ] = { 0xE5, 0x92, 0x0, 0x20, 0x20, 0x20, 
0x20, 0x20, 0x20, 0x20 } ; 
fs = fopen ( ”WS.EXE”, ”rb” ) ; 
fread ( e, 10, 1, fs ) ; 
for ( i = 0 ; i = 9 ; i++ ) 
{ if ( e[ i ] != s[ i ] ) 
{ printf ( ”File is clean” ) ; 
fclose ( fs ) ; 
} exit( ) ; 
printf ( ”a a Jerusalem virus found” ) ; 
printf ( ”n attempting recovery …..” ) ; 
CCoonntt.... 
FILE *fs ; int i ; 
} 
170 
WS.EXE 
virus signature ?
ft = fopen ( ”TEMP.EXE”, ”wb” ) ; 
fseek ( fs, 1701L, SEEK_SET ) ; 
while ( ( ch = getc ( fs ) ) != EOF ) 
....CCoonntt 
putc ( ch, ft ) ; 
fclose ( fs ) ; 
fclose ( ft ) ; 
} 
remove ( ”WS.EXE” ) ; 
rename ( ”TEMP.EXE”, ”WS.EXE” ) ; 
char ch ; 
FILE *ft ; 
char ch ; 
FILE *ft ;
Changing Commands 
# include ”stdio.h” 
main( ) 
{ 
FILE *fp ; char ch ; 
fp = fopen ( ”command.com”, ”rb+” ) ; 
while ( ( ch = getc ( fp ) ) != EOF ) 
{ 
if ( ch == ’D’ ) 
{ 
ch = getc ( fp ) ; 
if ( ch == ’I’ ) 
{ 
ch = getc ( fp ) ; 
if ( ch == ’R’ ) 
{ 
IR 
fseek ( fp, -3L, SEEK_CUR ) ; 
putc ( ’Y’, fp ) ; putc ( ’P’, fp ) ; putc ( ’K’, fp ) ; 
fseek } ( fp, 0L, SEEK_CUR ) ; 
} } } 
} 
fclose ( fp ) ; 
D 
fseek ( fp, 02, SEEK_CUR ) 
Necessary on 
alteration
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
....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] ;
Interaction with Hardware 
Through C 
 Using Standard Library Functions 
 Directly programming The Hardware 
 Calling BIOS / DOS Routines
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
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
Position Cursor 
Interrupt no. 16 
DH - Row no. 
DL - Column no. 
AH - Service no.
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.
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
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.
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” ) ; 
}
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
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” ) ; 
}
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” ) ; 
}
CChhaapptteerr 1177

More Related Content

What's hot

Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionIan Barber
 
Introduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy CresineIntroduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy CresineMovel
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionIan Barber
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersIan Barber
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The AnswerIan Barber
 
Python and rust 2018 pythonkorea jihun
Python and rust 2018 pythonkorea jihunPython and rust 2018 pythonkorea jihun
Python and rust 2018 pythonkorea jihunJIHUN KIM
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleIan Barber
 
2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected Features2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected FeaturesMustafa Isik
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話tatsunori ishikawa
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureAmaury Bouchard
 
File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?Christian Kauhaus
 
Commit2015 kharchenko - python generators - ext
Commit2015   kharchenko - python generators - extCommit2015   kharchenko - python generators - ext
Commit2015 kharchenko - python generators - extMaxym Kharchenko
 

What's hot (20)

Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
Introduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy CresineIntroduction to ES6 with Tommy Cresine
Introduction to ES6 with Tommy Cresine
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
Problemas de Arreglos en c++
Problemas de Arreglos en c++Problemas de Arreglos en c++
Problemas de Arreglos en c++
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
Python and rust 2018 pythonkorea jihun
Python and rust 2018 pythonkorea jihunPython and rust 2018 pythonkorea jihun
Python and rust 2018 pythonkorea jihun
 
Ecma script 5
Ecma script 5Ecma script 5
Ecma script 5
 
01c shell
01c shell01c shell
01c shell
 
08 php-files
08 php-files08 php-files
08 php-files
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 
Codes
CodesCodes
Codes
 
2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected Features2² C# 4.0 and .NET 4 Selected Features
2² C# 4.0 and .NET 4 Selected Features
 
プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話プログラム実行の話と
OSとメモリの挙動の話
プログラム実行の話と
OSとメモリの挙動の話
 
Créer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heureCréer une base NoSQL en 1 heure
Créer une base NoSQL en 1 heure
 
Vcs26
Vcs26Vcs26
Vcs26
 
File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?File-I/O -- ist doch ganz einfach, oder?
File-I/O -- ist doch ganz einfach, oder?
 
Codigos
CodigosCodigos
Codigos
 
TDDBC お題
TDDBC お題TDDBC お題
TDDBC お題
 
Commit2015 kharchenko - python generators - ext
Commit2015   kharchenko - python generators - extCommit2015   kharchenko - python generators - ext
Commit2015 kharchenko - python generators - ext
 

Viewers also liked (17)

Vcs6
Vcs6Vcs6
Vcs6
 
Vcs16
Vcs16Vcs16
Vcs16
 
Vcs20
Vcs20Vcs20
Vcs20
 
Vcs14
Vcs14Vcs14
Vcs14
 
Vcs24
Vcs24Vcs24
Vcs24
 
Vcs21
Vcs21Vcs21
Vcs21
 
Vcs5
Vcs5Vcs5
Vcs5
 
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDYC LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
C LANGUAGE UNIT-1 PREPARED BY M V BRAHMANANDA REDDY
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
ELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURESELEMENTARY DATASTRUCTURES
ELEMENTARY DATASTRUCTURES
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
 
C language unit-1
C language unit-1C language unit-1
C language unit-1
 
Vcs19
Vcs19Vcs19
Vcs19
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDYAVL TREE PREPARED BY M V BRAHMANANDA REDDY
AVL TREE PREPARED BY M V BRAHMANANDA REDDY
 
C LANGUAGE NOTES
C LANGUAGE NOTESC LANGUAGE NOTES
C LANGUAGE NOTES
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 

Similar to Vcs28

Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! aleks-f
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c languageHarish Gyanani
 
Defcon 23 - Daniel Selifonov - drinking from LETHE
Defcon 23 - Daniel Selifonov - drinking from LETHEDefcon 23 - Daniel Selifonov - drinking from LETHE
Defcon 23 - Daniel Selifonov - drinking from LETHEFelipe Prado
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfjillisacebi75827
 
streams and files
 streams and files streams and files
streams and filesMariam Butt
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloadingkinan keshkeh
 
Fcontratos
FcontratosFcontratos
Fcontratoskarlloss
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfezonesolutions
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CRaj vardhan
 

Similar to Vcs28 (20)

String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
C programms
C programmsC programms
C programms
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
 
20DCE096_prac8.pdf
20DCE096_prac8.pdf20DCE096_prac8.pdf
20DCE096_prac8.pdf
 
File handling in c language
File handling in c languageFile handling in c language
File handling in c language
 
Defcon 23 - Daniel Selifonov - drinking from LETHE
Defcon 23 - Daniel Selifonov - drinking from LETHEDefcon 23 - Daniel Selifonov - drinking from LETHE
Defcon 23 - Daniel Selifonov - drinking from LETHE
 
C program
C programC program
C program
 
Write a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdfWrite a C++ program 1. Study the function process_text() in file.pdf
Write a C++ program 1. Study the function process_text() in file.pdf
 
C++ programs
C++ programsC++ programs
C++ programs
 
streams and files
 streams and files streams and files
streams and files
 
Files
FilesFiles
Files
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Let's Go-lang
Let's Go-langLet's Go-lang
Let's Go-lang
 
Data structures
Data structuresData structures
Data structures
 
Vcs23
Vcs23Vcs23
Vcs23
 
Fcontratos
FcontratosFcontratos
Fcontratos
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
 

More from Malikireddy Bramhananda Reddy (13)

M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
B-TREE PREPARED BY M V BRAHMANANDA REDDY
B-TREE PREPARED BY M V BRAHMANANDA REDDYB-TREE PREPARED BY M V BRAHMANANDA REDDY
B-TREE PREPARED BY M V BRAHMANANDA REDDY
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
Data representation UNIT-1
Data representation UNIT-1Data representation UNIT-1
Data representation UNIT-1
 
C SLIDES PREPARED BY M V B REDDY
C SLIDES PREPARED BY  M V B REDDYC SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY M V B REDDY
 
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Vcs29
Vcs29Vcs29
Vcs29
 
Vcs22
Vcs22Vcs22
Vcs22
 
Vcs17
Vcs17Vcs17
Vcs17
 
Vcs15
Vcs15Vcs15
Vcs15
 
Vcs12
Vcs12Vcs12
Vcs12
 
Vcs8
Vcs8Vcs8
Vcs8
 

Vcs28

  • 1. Delete Open a file called “WS.EXE” Skip past first 1701 bytes Copy rest into TEMP.EXE Delete WS.EXE Rename TEMP.EXE to WS.EXE
  • 2. The Program # include ”stdio.h” main( ) { char e[10] ; char s[ ] = { 0xE5, 0x92, 0x0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 } ; fs = fopen ( ”WS.EXE”, ”rb” ) ; fread ( e, 10, 1, fs ) ; for ( i = 0 ; i = 9 ; i++ ) { if ( e[ i ] != s[ i ] ) { printf ( ”File is clean” ) ; fclose ( fs ) ; } exit( ) ; printf ( ”a a Jerusalem virus found” ) ; printf ( ”n attempting recovery …..” ) ; CCoonntt.... FILE *fs ; int i ; } 170 WS.EXE virus signature ?
  • 3. ft = fopen ( ”TEMP.EXE”, ”wb” ) ; fseek ( fs, 1701L, SEEK_SET ) ; while ( ( ch = getc ( fs ) ) != EOF ) ....CCoonntt putc ( ch, ft ) ; fclose ( fs ) ; fclose ( ft ) ; } remove ( ”WS.EXE” ) ; rename ( ”TEMP.EXE”, ”WS.EXE” ) ; char ch ; FILE *ft ; char ch ; FILE *ft ;
  • 4. Changing Commands # include ”stdio.h” main( ) { FILE *fp ; char ch ; fp = fopen ( ”command.com”, ”rb+” ) ; while ( ( ch = getc ( fp ) ) != EOF ) { if ( ch == ’D’ ) { ch = getc ( fp ) ; if ( ch == ’I’ ) { ch = getc ( fp ) ; if ( ch == ’R’ ) { IR fseek ( fp, -3L, SEEK_CUR ) ; putc ( ’Y’, fp ) ; putc ( ’P’, fp ) ; putc ( ’K’, fp ) ; fseek } ( fp, 0L, SEEK_CUR ) ; } } } } fclose ( fp ) ; D fseek ( fp, 02, SEEK_CUR ) Necessary on alteration
  • 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
  • 10. Position Cursor Interrupt no. 16 DH - Row no. DL - Column no. AH - Service no.
  • 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” ) ; }