;-------------------------------------------------------;
;               Loading a com file demo                 ;
;             Coded by Dex(Craig Bamford)               ;
;-------------------------------------------------------;
;  Assemble with fasm C:\fasm ComLoad.asm ComLoad.com   ;
;                                                       ;
;                    www.dex4u.com             21/02/08 ;
;-------------------------------------------------------;
org  100h				    ; Just normal com file org 100h .
	mov   al,'S'			    ; Lets print the letter S for start.
	mov   ah,0x0e			    ; Fill the function number
	int   10h			    ; Call the int

	xor   ax,ax			    ; Zero ax
	int   16h			    ; Wait for a keypress

	call  InStallInts20h		    ; Call the function to hook our int 20h

	mov   ax,2010h			    ; Here the the segment address of place to reload the 'test.com'
	mov   es,ax			    ; We can not write directly to segment reg, so we move then in a gemeral reg first
	xor   di,di			    ; Zero the offset
	mov   si,ImageLoadOffset	    ; Fill in the source reg with the 'test.com' file
	mov   cx,0xfff0 		    ; Fill in number of bytes to move.
	mov   bp,sp			    ; Save SP to re-use later
	mov   word[SaveSP],bp		    ; Store it
	rep   movsb			    ; Now move 0xfff0 from what ds:si points to what es:di points to.
	mov   ax,2000h			    ; Now we set segment regs to 100h less than load address (as per com spec)
	mov   es,ax			    ;
	mov   ds,ax			    ;
	mov   ss,ax			    ;
	xor   sp,sp			    ; Zero the stack pointer

	jmp far 0x2010:0000		    ; Do a far jump to the load address

int20:					    ; Int 20h ends up here.
	cli				    ; Turn off int's
	push  cs			    ; Push CS on stack
	pop   bx			    ; BX now = CS
	mov   ds,bx			    ; Move BX into DS
	mov   es,bx			    ; Move BX into ES
	mov   ss,bx			    ; Move BX into SS
	mov   bp,word[SaveSP]		    ; Put the stored SP in the base pointer
	mov   sp,bp			    ; Than move it in to the Stack pointer
	sti				    ; Turn on int's

	mov   al,'B'			    ; Lets print the letter B for returned back OK.
	mov   ah,0x0e			    ; Fill the function number
	int   10h			    ; Call the int

	xor   ax,ax			    ; Zero ax
	int   16h			    ; Wait for a keypress

	call  InStallOldInts20H 	    ; Restore the old int 20h
	int   20h			    ; Exit to dos

InStallInts20h:
	push  ds			    ; Save DS
	cli				    ; Turn off int's
	xor   ax,ax			    
	mov   ds,ax			    ; 0 DS

	mov   ax,word [ds:20h*4]	    ; save the old offset
	mov   [Oldint20h],ax		    ;
	mov   ax,word [ds:20h*4+2]	    ; save the old segment
	mov   [OldCs],ax		    ;

	mov   word [ds:20h*4],int20	    ; load int vecter with the new int20h address
	mov   word [ds:20h*4+2],cs	    ; + CS
	sti				    ; Turn on int's
	pop   ds			    ; restore DS
	ret				    ; Return

InStallOldInts20H:
	push  ds			    ; Save DS
	cli				    ; Turn off int's
	xor   ax,ax			    ; Zero AX
	mov   ds,ax			    ; 0 DS
	mov   ax,[Oldint20h]		    ;
	mov   word [ds:20h*4],ax	    ; Restore the old offset
	mov   ax,[OldCs]		    ;
	mov   word [ds:20h*4+2],ax	    ; Restore the old segment
	sti				    ; Turn on int's
	pop   ds			    ; restore DS
	ret				    ; Return


Oldint20h   dw 0			    ; Old int 20h offset var
OldCs	    dw 0			    ; Old int 20h segment var
SaveSP	    dw 0			    ; Old stack pointer var

ImageLoadOffset:			    ; Temp store of the com file to reload and run.
       file   "test.com"