introduction of tsr1

Upload: sumeet-sharma

Post on 08-Aug-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/22/2019 Introduction of TSR1

    1/42

    Welcome to Advanced Concepts in C

    Prerequisite:

    To understand these new concepts in C, the basic

    knowledge ofpointers in C and Internal Architecture of

    Microprocessoris required.

  • 8/22/2019 Introduction of TSR1

    2/42

    Internal Architecture of Microprocessor

  • 8/22/2019 Introduction of TSR1

    3/42

    EU(Execution Unit of Microprocessor) has Eight8-bit general-purpose registers,

    AH, AL

    BH, BL

    CH, CL DH and DL

    These registers can be used individually for temporary storage

    of 8-bit data.

    Register pairsAH-AL, BH-BL, CH-CL, and DH-DL can beused together to form registerAX, BX, CX, and DXand canbe used to store 16-bit data words.

    The AL register is also called theAccumulator.

    General-Purpose Registers

  • 8/22/2019 Introduction of TSR1

    4/42

    Segment Registers

    The 8086 microprocessor has FourSegment Registers:

    CS (Code Segment Register)DS (Data Segment Register)

    SS (Stack Segment Register)

    ES (Extra Segment Register)

    These registers are used to refer Segment part of Memory

    address.

    The 8086 microprocessor has Five Offset Registers:

    IP (Instruction Pointer or Program Counter)SP (Stack Pointer)

    BP (Base Pointer)

    SI (Source Index)

    DI (Destination Index)

    These registers are used to refer Offset part of Memory address.

  • 8/22/2019 Introduction of TSR1

    5/42

    Types of Pointers

    Near Pointers

    Far Pointers

    Huge Pointers

    Near Pointers:

    A Near Pointer is of 16-bits.

    It uses the current contents of the CS(Code Segment)

    register or DS(Data Segment) register for Segment partWhereas the offset part is stored in 16-bits NEARpointer.

    Clearly this type of Pointer limits us to 64KB memory ofa particular segment.

  • 8/22/2019 Introduction of TSR1

    6/42

    Far Pointers:

    A Far Pointer is of 32-bits long.

    This type of Pointer contains both segment and offset.

    By using it we can point to multiple segments so Limitof216 = 26 X 210 = 64kb does not exist for Far Pointers

    Hence with Far Pointers we can address more than64kb of data.

    Huge Pointers:

    A Huge Pointer is of 32-bits long.

    This is same as Far pointer except one difference that

    here Segment: Offset address is always Normalized. So the offset always contains value from 0 to F

    For Example : an Address 500D : 9407 will beconverted to 594D : 0007 as shown below:

  • 8/22/2019 Introduction of TSR1

    7/42

    Segment:

    500D0 (0 is appended automatically )

    + 9407After Addition we get 594D7Which is Normalized as 594D : 0007

  • 8/22/2019 Introduction of TSR1

    8/42

    Pointers and Typecasting

    We know that an Integer Pointer contain the address of an Integer.

    Similarly a Float pointer contain the address of an Float and so on

    But if we try to assign address of a Float to an Integer Pointer and

    Vice Versa then we get a typical Warning i.e. SuspiciousPointer Conversion

    Consider the program given below:

    main() /* SPC.C */

    {

    int i=10;float *p;

    p=&i; /* Suspicious Pointer Conversion */

    }

  • 8/22/2019 Introduction of TSR1

    9/42

    The problem ofSuspicious Pointer Conversion

    can be solved by using Type Casting as shown in

    the program given below:

    main() /* TYPEc.C */

    {

    int i=10;

    float *p;

    p=(float *)&i;

    }

  • 8/22/2019 Introduction of TSR1

    10/42

    Now let us see another program which tries to refer

    the address which lies outside the DS(Data Segment)

    of our program.For achieving this we have to use Far

    Pointer as Given below:

    main() /* SCR.C */{

    char far *screen;

    screen = (char far *)0xB8000000L;

    *screen = A;

    }

  • 8/22/2019 Introduction of TSR1

    11/42

    The address0xB8000000Lis a an address of the

    beginning of Colored VDU Memory Block. ForMonochrome Adapter the address is0xB0000000L.

    Each Character present on screen has TWO bytes

    corresponding to it in the VDU memory.

    The FIRST byte contains the ASCII Value of the character. The SECOND byte contains COLOR of that character.

    So if A is displayed onrow -0andcolumn -0 on the

    screen then address0xB8000000L contains ASCII value

    of A and address (0xB8000000L + 1) contains the

    COLOR ofA

    Hence if One character requiresTWO Bytesthe One

    Screenful of characters will require 80 * 25 * 2 = 4000

    Bytes .

    How it Works ?

  • 8/22/2019 Introduction of TSR1

    12/42

    Consider the program given below:

    main() /* SCRofA.C */{

    char far *screen = (char far *)0xB8000000L;

    int i;for ( i = 0 ; i

  • 8/22/2019 Introduction of TSR1

    13/42

    Interrupts and Interrupt Vector Table

    An Interrupt is a Signal to the Microprocessorto Stop current task

    and execute some other task.

    An Interrupt may be generated through Software by using int86()

    function in C or through Hardware e.g. When we Hit a key fromthe keyboard , a Hardware Interrupt is generated.

    When an Interrupt occurs, the Microprocessor stops its currentprocessing activity and executes an Interrupt Service Routine(ISR)

    corresponding to the interrupt generated.

    The address of the ISR is stored in Interrupt Vector Table(IVT)

    The moment Microprocessor findsAddress from IVT, it does stores

    current contents of various registers on the Stack. The control is then transferred to the ISR to be executed.

    Once the ISR has been executed, the Microprocessor returns back

    to its previous activity.

  • 8/22/2019 Introduction of TSR1

    14/42

    Interrupt No.

    PurposeDecimal Hexadecimal

    16 10 Video Display

    19 13 Diskette Services

    20 14 Communication

    Devices

    22 16 Keyboard Services

    23 17 Printer Services18 12 Memory size

    26 1A Time and Date

  • 8/22/2019 Introduction of TSR1

    15/42

    /* IVT.C */

    #include "stdio.h"

    #include "dos.h"main()

    {

    unsigned long far *address = (unsigned long far *)0x00000000;

    unsigned long intadd[256];

    unsigned int segment,offset;

    int i;FILE *fp;

    fp = fopen("IVT.TXT","wb");

    for(i=0 ; i

  • 8/22/2019 Introduction of TSR1

    16/42

    int86() Function

    This function is used to make a Software Interrupt.

    Here int stands for Interrupt and 86 stands for8086 family ofMicroprocessors.

    It has following Three arguments:

    Interrupt Number

    Two union variablesThe First union variable refers to values passed and Second for

    values returned by the int86() function.

    e.g. int86(16, &inregs, &outregs);Where

    16 is the Interrupt Number

    inregs represents register values sent to it

    outregs represents register values sent by it

  • 8/22/2019 Introduction of TSR1

    17/42

    Consider the program given below:/* MEMsize.C */

    #include dos.h /* union variables are declared in it */

    main(){

    union REGS inregs, outregs;

    int memsize;

    int86(18, &inregs, &outregs);

    memsize = outregs.x.ax;

    printf(\n Total Memory (RAM) = %d,memsize);

    }

    Using int86() Function to find Memory Size

  • 8/22/2019 Introduction of TSR1

    18/42

    In this program we have not passed any values to the inregs

    because the Interrupt Number 18 does not require it, but it

    returns memory size in the register ax whose contents canbe accessed by using outregs.x.ax, Where x is a variable of

    union variable REGS.

    Let us see another program to print a TEXT at any Row and

    Column on the Screen..

  • 8/22/2019 Introduction of TSR1

    19/42

    /* Textrc.C */

    #include dos.h /* union variables are declared in it */main()

    { union REGS inregs, outregs;

    inregs.h.ah = 2; /* Service Number */

    inregs.h.dh = 10;/* Row Number */inregs.h.dl = 2; /* Column number */

    int86(16, &inregs, &outregs);

    printf(\n Hello);

    }

  • 8/22/2019 Introduction of TSR1

    20/42

    In this program, We have passed some values to the

    function int86().

    Upon return from the function, the cursor is set to the

    position mentioned in the program.

    Hence depending upon the Service Numberof the

    Interrupt, we have to pass and receive the values through

    inregs and outregs variables.

  • 8/22/2019 Introduction of TSR1

    21/42

    Pointers to Functions

    As we know that Pointers can be used as Reference

    Variables to any of the Data types in C.

    Similarly Pointers can also be used to point C

    Functions. So if we know the functions address, we can point to

    it.

    In C, We can find the Address of any function.

    This Address then can be assigned to a Pointer.

  • 8/22/2019 Introduction of TSR1

    22/42

    Let us see the following program to Find Address of a

    function:main( ) /* p_fun.c */

    {

    int display( );

    printf ( \n Address of function display is %u, display);

    display( );}

    display( )

    {

    printf( \n In function display );

    }

  • 8/22/2019 Introduction of TSR1

    23/42

    So from above program it is clear that we can find the Address of any

    function.

    Let us see following program in which Address of Function is assigned

    to a Pointer as given below:

    main( ) /* p_fun2.c */{

    int display( );

    int (*fp)(); /* Declaration of Pointer to a Function */

    fp = display; /* Assigning Address of Function to Pointer */

    printf ( \n Address of function display is %u, display);

    (*fp)(); /* Invoking Function through Pointer Fp*/

    }display( )

    {

    printf( \n In function display );

    }

  • 8/22/2019 Introduction of TSR1

    24/42

    So from above program it is clear that we can call any function with

    the help of Pointers..

    Let us see following program in which Address of a number of

    Functions is assigned to an array of Pointers as given below:

  • 8/22/2019 Introduction of TSR1

    25/42

    main( ) /* p_fun3.c */

    {

    int i,fun1( ), fun2(), fun3();

    int (*fp[3])(); /* Declaration of Pointer Array to a Function */

    fp[0] = fun1; /* Assigning Address of Function to Pointer */fp[1] = fun2;

    fp[2] = fun1;

    for( i=0 ; i

  • 8/22/2019 Introduction of TSR1

    26/42

    So from above program it is clear that we can call any number of

    functions with the help of Pointers, which is an advantage of using

    Pointers.

    Let us see following program in which a ROM-BIOS (Read Only Memory

    Basic Input Output system)Function used to Exit from DOS, which

    resides at memory location 0xFFFF0000:

    #include dos.h /* ExitFrom

    .c*/

    main( )

    {

    void far (*p)( );

    p = 0xFFFF0000;

    (*p)(); /* Invoking ROM BIOS Function through Pointer p */

    }

  • 8/22/2019 Introduction of TSR1

    27/42

    Introduction of TSR

  • 8/22/2019 Introduction of TSR1

    28/42

    Introduction of TSR

    TSR stands for:

    Terminate-

    and Stay-Resident

    programs

  • 8/22/2019 Introduction of TSR1

    29/42

    Terminate-and-Stay-Resident (TSR) are programs which

    get loaded in memory and remain or stay there (resident)in memory permanently.

    They will be removed only when the computer is

    Rebooted or if the TSR is explicitly removed frommemory.

    Until then they will stay (resident) in memory active.

    Whats Special about TSR

    Working Technology of TSR

  • 8/22/2019 Introduction of TSR1

    30/42

    Working Technology of TSR

    When TSR is not running it does not affect the

    running of other DOS programs. It stays in

    memory in idle state.

    Only thing it does is it occupies memory space

    and the occupied TSR memory space cannot be

    occupied by other programs.

    The TSR which is now in memory is invoked onlywhen some special keys are pressed.

  • 8/22/2019 Introduction of TSR1

    31/42

    The First TSR

    Let us try to Change the contents ofIVT such that thelocation number 36 to 39 contain the Address ofOUR

    function , instead of the normal ROM-BIOSroutine.

    We will also make the program resident in memory even

    when its execution is terminated.

    Let us see following program in which a TSR changes characters on

  • 8/22/2019 Introduction of TSR1

    32/42

    /* TSR1.c */#include dos.hvoid interrupt our();

    void interrupt (*prev)();

    char far *scr = (char far *)0xB8000000L;

    main( )

    {

    unsigned long int far *p;

    p = (char far *)36; /* Declaration of Pointer to Location 36 of IVT */

    prev = *p; /* Save existing Address of ROM-BIOS Function to Pointer */

    *p = our; /* Set up New Address */

    keep(0,500); /* Make program resident in Memory */

    }

    Let us see following program in which a TSR changes characters on

    screen as given below:

  • 8/22/2019 Introduction of TSR1

    33/42

    /* TSR1.c Continued */void interrupt our( )

    {

    int i;

    for ( i = 0 ; i= A && *(scr + i) = a && *(scr + i)

  • 8/22/2019 Introduction of TSR1

    34/42

    Explanation of The First TSR

    Variable p is declared unsigned long int pointer because the address

    stored at location 36 to 39 is 4 bytes long. Keep() function is used to request DOS to allocate 500 * 16 Bytes

    to OUR program.

    It will make ourProgram Resident as this memory will never be given

    to any other program running underDOS.

    What happens actually when We hit a key from the keyboard

    Firstly, Interrupt Number 9 would be generated, then it is

    multiplied by 4 ( 9 * 4 = 36).

    Next theAddress present in locations 36 to 39 would be picked

    and control would be passed to this address, Which is address of

    actual ROM-BIOS Service Routine.

    But here we do a little trick, We change this address at 36 to 39

    locations.

    Now OUR function will be called which changes case of

    alphabets.

  • 8/22/2019 Introduction of TSR1

    35/42

    The Second TSR

    Let us try to write a TSR to permanently keep theCaps Lock on.

    For this we have to catch Interrupt Number 9 and then

    call our routine to keep Caps on.

  • 8/22/2019 Introduction of TSR1

    36/42

    /* TSR2.c */#include dos.h

    void interrupt our();

    void interrupt (*prev)();

    char far *kb = (char far *)0x417;

    main( )

    {

    prev = getvect(9); /* To get address of Interrupt number 9 */setvect(9,our);/* To set address of our function */

    keep(0,500);/* Make program resident in Memory */

    }

    void interrupt our()

    {

    (*prev)();

    *kb = *kb | 64;

    }

  • 8/22/2019 Introduction of TSR1

    37/42

    The Status ofCaps Lock is stored in the sixth bit of byteat 0x417.

    Interrupt Number 9 is generated once when we hit a key

    and once when we release it.

    Now when any key is released, Our routine makes CapsLock on.

    How it Works ?

    7 6 5 4 3 2 1 0

    Ins Caps Num

    Lock

    Scroll

    Lock

    Alt Ctrl Left

    Shift

    Right

    Shift

  • 8/22/2019 Introduction of TSR1

    38/42

    The Third TSR/* TSR2.c */#include dos.h

    void interrupt our();

    void interrupt (*prev)();

    char far *kb = (char far *)0x417;

    main( )

    {

    prev = getvect(9); /* To get address of Interrupt number 9 */setvect(9,our); /* To set address of our function */

    keep(0,500); /* Make program resident in Memory */

    }

    void interrupt our()

    {

    (*prev)();

    (*prev)();

    }

    L t t t it Ti B d TSR hi h Ch

  • 8/22/2019 Introduction of TSR1

    39/42

    #include "dos.h /* TIMEB.C */void interrupt our();

    void interrupt (*prev)();

    char far *scr = (char far *)0xB8000000L;

    int ticks;unsigned char color;

    main()

    {

    prev = getvect(8);

    setvect(8,our);keep(0,500);

    }

    Let us try to write a Time Bound TSR which Changes

    Color of Screen after a Fixed Time as given below:

  • 8/22/2019 Introduction of TSR1

    40/42

    void interrupt our() /* TIMEB.C Contd. */

    {

    int i;

    ticks++;if(ticks == 182)

    {

    for(i=1;i

  • 8/22/2019 Introduction of TSR1

    41/42

    #include "dos.h /* RAIN.C */

    #include "stdlib.h"

    void interrupt our();

    void interrupt (*prev)();char far *scr = (char far *)0xB8000000L;

    int ticks;

    main()

    {

    prev = getvect(8);setvect(8,our);

    keep(0,1000);

    }

    id i ()

  • 8/22/2019 Introduction of TSR1

    42/42

    void interrupt our()

    {

    int i,col=1,row;

    char far *v,ch;

    while(1) /* Infinite loop */{

    row = 1;

    ch = *(scr+row * 160 +col*2);

    for(;row