home *** CD-ROM | disk | FTP | other *** search
- ;
- ; crt.asm 'small' C startup and runtime library for TSR programs
- ;
- ; Author: HIRANO Satoshi
- ; (C) 1990 Halca Computer Science Laboratory TM
- ; University of Tokyo
- ;
- ; 'Standard Free Software Statement' here :-)
- ;
- ; usage:
- ; masm -Mx crt.asm;
- ; cl -Ox -c tsr.c
- ; link crt+tsr,tsr;
- ;
- ; crt.asm calls main() in tsr.c after stack/data segment adjustment.
- ; There are two method to exit from main() function.
- ; First is _exit(n) function which exits from the program normally
- ; with return code 'n'.
- ; Second is implied return from main() function which terminates the
- ; program but stay it regident.
- ;
- ; library:
- ; crt.asm provides some useful library functions.
- ;
- ; void _exit(n)
- ; int n;
- ; Terminate the program. never return.
- ;
- ; void putMsg(p)
- ; char *p;
- ; Print message to console. message must be terminated by '$' char.
- ;
- ; void (interrupt far *getVect(vec))()
- ; int vec;
- ; Return far address of interrupt handler corresponding with 'vec'.
- ;
- ; void setVect(vec, address)
- ; int vec;
- ; void (interrupt far *address())();
- ; Set interrupt handler 'address' to specified vector 'vec'.
- ;
- ; int pspSeg;
- ; PSP segment
- ;
-
- name crt
-
- _TEXT SEGMENT WORD PUBLIC 'CODE'
- assume cs:_TEXT
- org 2ch
- envSeg dw ? ; segment of environment area
- org 100h
- _TEXT ENDS
- _DATA segment word public 'DATA'
- public __acrtused ; MSC5.1 magic variable for debugger
- __acrtused = 9876h
- public _pspSeg
- _pspSeg dw 0
- even
- db 256 dup(?)
- stackTop label word
- _DATA ends
- CONST SEGMENT WORD PUBLIC 'CONST'
- CONST ENDS
- _BSS SEGMENT WORD PUBLIC 'BSS'
- _BSS ENDS
- DGROUP GROUP CONST, _BSS, _DATA
- ASSUME CS:_TEXT,DS:DGROUP,SS:DGROUP
-
- extrn _main:near
- extrn _endAddress:byte
-
- _TEXT segment word public 'CODE'
- ASSUME CS:_TEXT,DS:DGROUP,SS:DGROUP
-
- ;
- ; program entry point
- ;
- public __main
- __main:
- mov di,DGROUP
- mov ds,di
- mov ss,di
- xor bp,bp ; clear bp for debugger
- mov sp,offset ds:stackTop ; set stack
- mov ds:_pspSeg,es ; psp segment address
- push es ; PSP segment
- mov ax,81h ; PSP:0x81 = argument area
- push ax ; push (far) argument pointer
- ; free environment variable area
- push es
- mov es,es:[envseg]
- mov ah,49h
- int 21h
- pop es
- mov es:[envseg],0
- ; call main program
- call _main ; call main(char far *argp)
- ; terminate but stay regident the program
- mov dx,offset cs:_endAddress
- add dx,10fh
- mov cl,4
- shr dx,cl ; dx := paragraph size
- mov ax,3100h
- int 21h ; terminate but stay regident
- ; fall thru if error
-
-
- ;
- ; micro library for TSR program
- ;
-
- ; _exit(code)
- ; int code
- public __exit
- __exit:
- ; terminate process
- mov ah,4ch ; terminate process with AL
- int 21h
- safety: jmp safety ; I don't believe MS-DOS.
-
- ;
- ; putMsg(p) /* put message */
- ; char *p;
- ;
- public _putMsg
- _putMsg:
- push bp
- mov bp,sp
- mov dx,[4+bp] ; string address
- mov ah,9 ; display string
- int 21h
- pop bp
- ret
-
- ;
- ; void (interrupt far *getVect(vecNo))()
- ; int vecNo;
- public _getVect
- _getVect:
- push bp
- mov bp,sp
- push bx
- push es
- mov ax,[4+bp]
- mov ah,35h ; al := vectNo, ah := get interrupt vector
- int 21h
- mov dx,es ; dx:ax := interrupt handler address
- mov ax,bx
- pop es
- pop bx
- pop bp
- ret
-
- ;
- ; setVect(vectNo, address)
- ; int vectNo;
- ; void far *address;
- public _setVect
- _setVect:
- push bp
- mov bp,sp
- push ds
- push dx
- mov ax,[4+bp] ; vectNo
- mov ah,25h ; set interrupt vector
- mov ds,[8+bp]
- mov dx,[6+bp] ; ds:dx := handler address
- int 21h
- pop dx
- pop ds
- pop bp
- ret
-
- public _int5
- _int5:
- int 5h
- ret
-
- public _int6
- _int6:
- int 6h
- ret
-
- _TEXT ends
- end __main
-