Wednesday, 20 April 2016

Display Real Time Clock TSR in Dos


TSR:

Terminate but Stay Resident Program is a type of assembly program which stays resident / or occupies memory on ram so that it can be executed upon interrupt.
TSR is actually one of the best ways to ways to write viruses. The best part is that until the computer is restarted the virus can stay in the memory.

It consists of three sections:-
which you are supposed to find out.

Problem Statement in Layman Terms
You have to display the time on screen using a TSR.



Algorithm/Steps

Initialization

  1. First store the old cs:ip for interrupt.
  2. Second load new cs:ip for interrupt.
  3. Reserve memory in dos. 

TSR execution

  1. Get the clock values.
  2. Set pointer to display ram.
  3. Print the values after converting them to ascii
  4. Jmp to old cs:ip

Code

.MODEL SMALL ;set the model to small
.STACK 100H ;set stack size 
ORG 100H ;assembler directive for setting the beginning of the code segment

CODE SEGMENT ;code segment begins
ASSUME CS:CODE,DS:CODE,ES:CODE ;assigning logical name to segment
OLD_IP DW 00 ;will be used to store the location of the orginal 
OLD_CS DW 00 ;address of interrupt vector
JMP INIT ;jmp to init section

MYTSR: 
PUSH AX  ;push all the registers into stack
PUSH BX
PUSH CX
PUSH DX
PUSH SI
PUSH DI
PUSH ES

MOV AH,02H ;function two interrup 1Ah
INT 1AH  ;GET THE TIME FROM BIOS CHIP
  ;CH=HOURS,CL=MINS;DH=SECONDS
  ;stored in bcd format
MOV AX,0B800H 
MOV ES,AX ;setting es to beginning of display ram
MOV DI,3650 ;setting offset to use in display ram 
  ;refer note 1
  ;displaying hours now
MOV BL,02H ;counter
LOOP1:  ;conversion from bcd to ascii
ROL CH,4
MOV AL,CH
AND AL,0FH
ADD AL,30H
MOV AH,17H ;refer note, static white on blue
MOV ES:[DI],AX ;refer note 
INC DI
INC DI
DEC BL
JNZ LOOP1

MOV AL,':'
MOV AH,97H ;refer note ,blinking white on blue
MOV ES:[DI],AX
INC DI
INC DI
  ;displaying minutes now
MOV BL,02
LOOP2:
ROL CL,04
MOV AL,CL
AND AL,0FH
ADD AL,30H
MOV AH,17H
MOV ES:[DI],AX
INC DI
INC DI
DEC BL
JNZ LOOP2

MOV AL,':'
MOV AH,97H
MOV ES:[DI],AX
INC DI
INC DI
  ;displaying the seconds now
MOV BL,02
LOOP3:
ROL DH,4
MOV AL,DH
AND AL,0FH
ADD AL,30H
MOV AH,17H
MOV ES:[DI],AX
INC DI
INC DI
DEC BL
JNZ LOOP3


POP ES  ;pop in LIFO fashion
POP DI
POP SI
POP DX
POP CX
POP BX
POP AX

JMP DWORD PTR CS:OLD_IP ;jump to old cs:ip

INIT:  ;THIS PORTION MAKES THE CODE RESIDENT

MOV AX,CS
MOV DS,AX ;initialize ds using cs value

MOV AH,35H ;function 31 of int 21h
MOV AL,08H ;store cs:ip for int8h
INT 21H  ;es:bx=cs:ip

MOV OLD_CS,ES
MOV OLD_IP,BX

LEA DX,MYTSR ;load effective address for tsr
  
MOV AH,25H ;function 25 of int21h
MOV AL,08H ;store new cs:ip for int 8h
INT 21H  ;new cs:ip=ds:dx

MOV DX,OFFSET INIT ;specifying the amount of memory requried
   ;for the resident portion of tsr


MOV AH,31H  ;function 31 of int21h  
STI   ;set interrupt flag
INT 21H   ;interrupt

CODE ENDS  
END

Note

1. The display ram starts at an 0B800h. This location refers to the first character on the first row of the screen. This display buffer is actually a 4000 byte buffer to display 2000 characters(25 rows * 80 cols) .So each character requires 2 bytes.  The upper byte specifies the method in which the character should be displayed. While the lower byte is for ascii code.
2. This upper byte has the following format

 Bl BBB FFFF

where,
Bl =1 Blinking, else static
BBB=first 3 bits for background color, fourth bit assumed to be 0
FFFF=4 bits for background color

Colors
A nibble represent color IRGB
where
  • I=Intensity
  • R=Red
  • G=Green
  • B=Blue 

So below is the color table

  • 0000= Black
  • 0001= Blue
  • 0010= Green
  • 0011= Cyan
  • 0100= Red
  • 0101=Magnetta
  • 0110=Brown
  • 0111=Light Gray
  • 1000=Gray
  • 1001=Light Blue
  • 1010= Light Green
  • 1100=Light Red
  • 1101=Yellow
  • 1111=White

How to run?

Steps for Ubuntu Linux

1.Install Dosbox.
sudo apt-get install dosbox

2.Paste Tasm compiler executables on /home/your_username
 link for tasm :mediafire-TASM
extract it and paste in in home folder

3.Open dosbox

4.Fire the following commands
  • mount c /home/your_username/TASM
  • c:
  • tasm clock.asm
  • ld clock.obj
  • clock.exe



Doubts

  • For additional information feel free to google.
  • In case you have any doubts feel free to bother yourself.

2 comments:

  1. why not have a keyboard shortcut to kill it? Or if I try to run it again and its loaded... it unloads it?

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete