Sample Programs-System Programming-Lecture Notes, Study notes of System Programming

This lecture handout is for System Programming course. It was provided by Prof. Indubhushan Vijayabhas at Ambedkar University, Delhi. It includes: Sample, Program, Loop, While, Divisor, Output, Frequency, Speaker, Approximation, Spectrum, Sound

Typology: Study notes

2011/2012

Uploaded on 08/07/2012

anishay
anishay 🇮🇳

4.2

(25)

118 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture # 10
Sample Program
unsigned long int * time = (unsigned long int *) 0x0040006C
void main()
{ unsigned long int tx;
unsigned int divisor = 0x21FF;
while (divisor >= 0x50) {
outportb(0x43,0xB4);
outportb(0x42,*((char*)(&divisor)));
outportb(0x42,*(((char*)(&divisor))+1));
outportb(0x61,inportb(0x61) | 3);
tx = *time;
tx = tx + 4;
while (*time <= tx);
divisor =divisor -30;
}
}
The inner while loop in the program is used to induce delay. The outer loop simply
reloads the divisor value each time it iterates after reducing this value by 30. In this way
the output frequency of the interval timer changes after every quarter of a second
approximately. The speaker will turn on with a low frequency pitch and this frequency
will increase gradually producing a spectrum of various sound pitches.
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Sample Programs-System Programming-Lecture Notes and more Study notes System Programming in PDF only on Docsity!

Lecture # 10

Sample Program

unsigned long int * time = (unsigned long int *) 0x0040006C

void main()

{ unsigned long int tx;

unsigned int divisor = 0x21FF;

while (divisor >= 0x50) {

outportb(0x43,0xB4);

outportb(0x42,((char)(&divisor)));

outportb(0x42,(((char)(&divisor))+1));

outportb(0x61,inportb(0x61) | 3);

tx = *time;

tx = tx + 4;

while (*time <= tx);

divisor =divisor -30;

The inner while loop in the program is used to induce delay. The outer loop simply

reloads the divisor value each time it iterates after reducing this value by 30. In this way

the output frequency of the interval timer changes after every quarter of a second

approximately. The speaker will turn on with a low frequency pitch and this frequency

will increase gradually producing a spectrum of various sound pitches.

Sample Program

#include <dos.h>

#include <bios.h>

void interrupt (*oldint15) ( );

void interrupt newint15 (unsigned int BP, unsigned int DI,

unsigned int SI, unsigned int DS, unsigned int ES,

unsigned int DX, unsigned int CX, unsigned int BX,

unsigned int AX, unsigned int IP, unsigned int CS,

unsigned int flags);

void main ( )

oldint15 = getvect (0x15);

setvect (0x15, newint15);

keep (0, 1000);

void interrupt newint15( unsigned int BP, unsigned int DI,

unsigned int SI, unsigned int DS, unsigned int ES,

unsigned int DX, unsigned int CX, unsigned int BX,

unsigned int AX, unsigned int CS, unsigned int IP,

unsigned int flags)

if (_AH = = 0x4F)

if (_AL == 0x1F)

outport (0x43, 0xB4);

outport (0x42, 0xFF);

outport (0x42, 0x21);

outport (0x61, inport(0x61) ^ 3);

else

(*oldint15) ( );

The above program is a TSR program that can be used to turn the speaker on/off. The

above program intercepts the int 15h. Whenever this interrupt occurs it looks for service

# 0x4f (keyboard hook). If ‘S’(0x1f scan code) has been pressed it toggles the speaker.

Sample Program

#include <stdio.h> #include <dos.h> #include <bios.h> struct tagTones { unsigned int divisor; unsigned int delay; }; struct tagTones Tones[4]={ {0x21ff,3},{0x1d45,2},{0x1b8a,3},{0x1e4c,4}}; int i,ticks,flag=0 ; void interrupt (oldint15)(); void interrupt (oldint8)(); void interrupt newint15(); void interrupt newint8();

unsigned char far *scr = (unsigned char far *)(0x00400017);

void main ()

oldint15=getvect(0x15);

setvect(0x15,newint15);

oldint8=getvect(0x08);

setvect(0x08,newint8);

keep(0,1000);

This is an interrupt driven version of the previous program. This program makes use of

the timer interrupt rather than a loop to vary the divisor value. Moreover interrupt 15 is

used to turn the speaker on /off.

void interrupt newint15() { if (_AH==0x4f) { if ((_AL==0x1f)&&(((scr)&12)==12)) { ticks=0; i=0; outport(0x43,0xb4); outport(0x42,((c har )(& Tones[i].divisor))); outport (0x42,(((char )(& Tones[i].divisor))+1)); outport(0x61,inport(0x61)|3); flag=1; } else if ((_AL==0x1E)&& (((scr)&12)==12)) { outport(0x61,inport(0x61)&0xfc); flag=0; } return; } (*oldint15)(); }

The speaker turns on whenever ‘S’ (scan code 0x1f) is pressed and turns off whenever

‘A’ (scan code 0x1E) is pressed.

void interrupt newint8() { if (flag==1) { ticks++; if (ticks == Tones[i].delay) { if (i==3) i=0; else i++; outport (0x43, 0xB4); outport(0x42,*((char )(&Tones[i].divisor))); outport(0x42,(((char )(&Tones[i].divisor))+1)); outport(0x61,inport(0x61)|3); ticks = 0; } } (oldint8)(); }

The timer interrupt shift the divisor value stored in the tones structure whenever the

required numbered of ticks( timer counts) have passed as required by the value stored in

the delay field of the tone structure.

More such divisor values and their delays can be initialized in the tones structure to

generate an alluring tune.

Parallel Communication

CPU^ Parallel Input Device

D

I/O Controller

Input

D

DR

CPU

D D

Advantages of Parallel communication

Parallel Communication

Faster

Only Economically Feasible For

Small Distances