



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The typical sequence in which registers will be pushed and poped into the stack on invocation and on return can be best described by the following diagrams
Push flags, CS, IP
Pop IP,CS,flags
Push AX,BX,CX,DX,ES,DS,SI,DI,BP
**Push AX, Push BX, Push CX, Push DX, Push ES, Push DS, Push SI, Push DI, Push BP
Pop BP, Pop DI, Pop SI , Pop DS, Pop ES, Pop DX, Pop CX, Pop BX, Pop AX, IRET**
The registers Flags, CS and IP are pushed on execution of INT instruction and executions branches to the interrupt procedure. The interrupt procedure pushes register AX, BX, CX, DX, ES, DS, SI, DI, BP in this order. The interrupt procedure then executes, before returning it pops all the registers in the reverse order as BP, DI, SI, DS, ES, DX, CX, BX and AX. IP, CS and flags are poped on execution of the IRET instruction. Next diagram shows the status of the stack after invocation of the interrupt procedure.
The arguments in simple procedure or functions are saved in the stack for the scope of the function/procedure. When an argument is accessed in fact stack memory is accessed. Now we will take a look how stack memory can be accessed for instance in case of interrupt procedures to modify the value of register in stack.
Accessing Stack Example void interrupt newint ( 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) //corrected { a = AX; b = BX; d = ES; }
Although interrupt do not take parameters through stack but an interrupt procedure can still have parameters. This parameter list can be used to access the stack. The leftmost parameter accesses the item on top of the stack and the rest of the parameters accesses deeper into the stack according to its order toward left. In the above example value of AX in stack is moved in a, the value of BX is moved into b and the value of ES is moved into d.
#pragma inline #include <dos.h> #include <bios.h> void interrupt (*oldtsr) ( ); void interrupt newtsr (unsigned int BP, …, flags); //must provide all the arguments void main ( ) { oldtsr = getvect (0x13); setvect(0x13, newtsr); //corrected keep (0, 1000); } void interrupt newtsr(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) //corrected { if ( _AH == 0x03) if(( _DH == 1 && _CH == 0 && _CL == 1)&& _DL >= 0x80) { asm clc; asm pushf; asm pop flags; return; } _ ES = ES; _DX = DX; _CX = CX; _BX = BX; _AX = AX; *oldtsr; asm pushf; asm pop flags; AX = _AX; BX = _BX; CX = _CX; DX = _DX; ES = _ES; }
The above program intercepts interrupt 13H. The new interrupt procedure first check AH for service number and other parameters for the address of boot block. If the boot block is to be written it simply returns and clears the carry flag before returning to fool the calling program that the operation was successful. And if the boot block is not to be written then it places the original parameters back into the registers and calls the original interrupt. The values returned by the original routine are then restored to the corresponding register values in the stack so that they maybe updated into the registers on return.
The keyboard Hook
The service 15H/4FH is called the keyboard hook service. This service does not perform any useful output, it is there to be intercepted by applications which need to alter the keyboard layout. It called by interrupt 9H after it has acquired the scan code of input character from the keyboard port while the scan code is in AL register. When this service returns interrupt 9H translates the scan code into ASCII code and places it in buffer. This service normally does nothing and returns as it is but a programmer can intercept it in order to change the scan code in AL and hence altering the input or keyboard layout.
The following application show how this can be done.
#include <dos.h> #include <bios.h> #include <stdio.h> void interrupt (oldint15) ( ); void interrupt newint15(unsigned int BP, …, 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 IP, unsigned int CS, unsigned int flags) { if ((((char)&AX) + 1) == 0x4F ) { if (((char)&AX) == 0x2C) (((char)&AX)) = 0x1E; else if (((char*)&AX) == 0x1E) ((char)&AX) = 0x2C; //corrected }