.286
.model small
.code
org 100h
;--------------------

 Black			equ	0
 Blue			equ 1
 Green			equ 2
 Cyan			equ 3
 Red			equ 4
 Magenta		equ 5
 Brown			equ 6
 LightGray		equ 7
 DarkGray		equ 8
 LightBlue		equ 9
 LightGreen		equ 10
 LightCyan		equ 11
 LightRed		equ 12
 LightMagenta	equ 13
 Yellow			equ 14
 White			equ 15
; Color Defenition
FileColor  	equ		128
FileBg		equ		22
;--------------------
start:
		mov ax,	1130h
		mov bh, 06h
		int 10h
		mov FontSeg, es
		mov FontOffs, bp
		mov ax, 13h
        int 10h

        mov ax,	3d00h	
        mov dx, offset fname
        int 21h
        jc MainEnd
        mov bx, ax

		mov ah,	3fh
		mov cx, 1f8h
		mov dx, offset buf
		int 21h
		cmp ax, 0
		je FileEnd 
		mov cx, ax
		mov di, dx
ReadChar:
		mov ah, 0
		mov al, byte ptr [di]
		cmp al, 0dh
		jne writenorm
		inc di
		cmp byte ptr [di], 0ah
		jne NewLine
		inc di
		dec cx
		jmp NewLine		
writenorm:
		push charx
		push chary
		push ax
		push filecolor
		push filebg
		push fontoffs
		push fontseg
		Call OutCharXY

		inc	 di
		cmp charX, 39
		jne VeryGood
		cmp byte ptr [di], 0Dh
		loope ReadChar
		jmp	FileEnd
VeryGood:
		inc charX
		loop ReadChar
		jmp FileEnd
NewLine:
		cmp charY, 11
		je	FileEnd
		inc charY
		mov charX, 0
		loop ReadChar

FileEnd:
		mov ah,3eh
		int 21h        
MainEnd:
        mov ah, 8h		; ReadKey
        int 21h			; 
        mov ax, 3h		;
        int 10h			; Set Text mode
        int 20h			; Quit
;---------------------------------------------
OutCharXY proc near 		  ;OutCharXY(X,Y,Char,Color,bg,TableOffs,TableSeg)
	push bp
	mov bp, sp
TableSeg  	equ word ptr [bp+4]
TableOffs 	equ word ptr [bp+6]
Color		equ byte ptr [bp+10]
Bg	 		equ byte ptr [bp+8]
Char 		equ byte ptr [bp+12]
posY		equ	word ptr [bp+14]
posX		equ	word ptr [bp+16]
    pusha
    push es
;----------------------------
;  Character Creation
    	mov di, TableOffs
    	mov es,	TableSeg  
    	mov ah, 0
    	mov al, Char
        shl ax, 4
		add di,	ax
        mov cx,	16
		mov si,	offset face
MainLoop:
		mov bh, es:di
		push cx
		mov cx, 8
LineLoop:
		test bh, 80h
        jz	BackGround
        mov ah, Color
        mov byte ptr ds:[si], ah
		jmp LoopEnd
BackGround:
        mov ah, Bg
	    mov byte ptr ds:[si], ah
LoopEnd:
		shl bh, 1
		inc si
		loop LineLoop
		pop cx
		inc di
		loop MainLoop
; End of Character creation
;=======================================
	mov ax,	posX
	shl ax, 3
	push ax				; X position

	mov ax,	posY
	shl ax, 4
	push ax 			; Y position

	push offset face    ; Character bitmap
	push 8				; SizeX
	push 16				; SizeY
    call WriteSprite	; WriteChar

    pop es
    popa
	pop bp
	ret 14
endp
;-------------------------------------------
WriteSprite proc near         ;writesprite(x,y,char *sprt,razmx,razmy)
    push bp
    mov bp,sp
razmy equ word ptr [bp+4]
razmx equ word ptr [bp+6]
sprt  equ word ptr [bp+8]
posy  equ word ptr [bp+10]
posx  equ word ptr [bp+12]
    pusha
    push es

    mov ax, 0A000h
    mov es, ax
    mov ax, posy
    mov bx, ax
    shl ax, 8
    shl bx, 6
    add ax, bx
    add ax, posx
    mov di, ax
    mov ax, sprt
    mov si, ax
    mov cx, razmy
lup:
    push cx
    mov cx, razmx
    repe movsb
    add di, 320
    sub di,razmx
    pop cx
    loop lup

    pop es
    popa
    pop bp
ret 10
endp

face		db 128 dup (?)
FontOffs	dw ?
FontSeg		dw ?
fname 		db 'colors.txt' 
charx		dw 0
chary		dw 0
buf 		db 1f8h dup (?)
end start