Tuesday, August 03, 2010

DOS Interrupts

As this is a very essential thing to know about virus programming i would like you to read it carefully, though extensive reading is not required.....

You can browse it on later when ever you feel that you ain't able to recognize any commands......

Don't worry it's very easy......

You just have to set the right register of the CPU using assembly language at the right time before invoking the functions. There are many DOS functions which are codes which is to be sent to the AH register and an Interrupt INT is called so that the function gets executed.....it is very simple just look at the examples....this will also help you understand my series on OS Programming.

Assembly Code To Call The DOS Interrupts.....

MOV AH,
INT 21H

Some Examples........
  • To read a character from the keyboard
    mov     ah, 01h
            int     21h
    
    When the above code is executed what happens is that the interrupt (int 21h) is called and ( al ) will read from the keyboard and contains the ASCII code of the input character. The character is displayed on the screen. If you do not want to display the character on screen then use 08h instead of 01h in the first line.
    
    
  • To read a string from keyboard
    section .data
    Buffer  db      bufferSize         ;bufferSize is the maximum number of chars to read from the keyboard and that is less than or equal to 255
            resb    bufferSize + 1
    
            section .code
            mov     dx, Buffer
            mov     AH, 0Ah
            int     21h
    
    When the interrupt (int 21h) is called what happens is BYTE [Buffer + 1] will contain the number of characters read from keyboard and the characters will be stored from BYTE [Buffer + 2] and the characters will be terminated by a carriage return (ASCII code 13) and it will not be counted in the characters count. 
    
    
  • To output a character to the screen
    mov     dl, ...
            mov     ah, 02h
            int     21h
    To write a character to the screen what we have to do is first load the ( dl ) register with the desired character and then call the interrupt with 02h as the function code.
  • To output a string to the screen
    mov     dx, ...
            mov     ah, 09h
            int     21h
    What we have to do over here is that first of all we have to store the string in a buffer and terminate it with a '$' sign and then get the start address of that string and load that address in the ( dx ) register. After that its simple as before. Just call the interrupt with the function code 09h in the ( ah ) register.
  • To exit
    mov     al, ...
            mov     ah, 4ch
            int     21h
    
    Now to exit normally what we have to do is that load with return code 0 and to exit with an error we have to load it with non zero return value in the ( al ) register. Then call the interrupt with function code 4ch. This is the way to exit DOS but if you are using the DEBUG program then you will just exit the DEBUG prompt.


As simple as that......the list of DOS codes is given below.....

01            read a character from standard input device like keyboard (STDIN)

02 
           write a character to standard output device like screen (STDOUT)


05            write a character to the printer


06            console input or output


07            to directly read a character from STDIN without echo


08            to read a character from STDIN without echo


09            to write a string to STDOUT


0A            for buffered input


0B            to get the status of STDIN


0C            to flush the buffer from STDIN


0D            to reset the disk


0E            to select the default drive like the usual C: drive


19            to get the current default drive


25            to set the interrupt vector


2A            to get the system date


2B            to set the system date


2C            to get the system time


2D            to set the system time


2E            to set the verify flag


30            to get the version of DOS


35            to get the interrupt vector


36            to get the amount of free disk space 


39            to create a subdirectory


3A            to remove a subdirectory


3B            to set the working directory


3C            to create a file


3D            to open a file


3E            to close a file


3F            to read a file


40            to write a file


41            to delete a file


42            to seek a file


43            GET/SET file attributes


47            to get the current directory


4C           to exit a program


4D           to get the return code


54            to get the verify flag


56            to rename a file


57            GET/SET the file date