%define drive 0x80
%define ploc 0x9000
[bits 16]
[org 0]

start:
        mov ax,cs
        mov ds,ax
        mov es,ax

	;push msg
	;call print

	;push prompt2
	;call print

	;push query
	;call readcmd

	;push crlf
	;call print

	call search

        ;mov al,13
        ;mov ah,0eh
        ;int 10h
        ;mov al,10
        ;mov ah,0eh
        ;int 10h

jmp 0x1000:0


readcmd:	; read an exe name from the user
	pusha
	mov bp,sp
	cld
	mov byte  [charcount],0
	mov di,[bp+18]
	continue_read:
		mov ah,0
		int 16h
		cmp al,0dh
		jz fin
		mov ah,0x0e
		mov bx,0
		int 10h
		stosb
		inc byte [charcount]
		jmp continue_read	
	fin:
		push nl
		call print
		mov sp,bp
		popa
		ret


search:		; search the filetable for the file
	pusha
	mov bp,sp 

	cmp ax,ax ; to set zero flag
	mov di,query

	mov bx,0
	cld
	cont_chk:
		mov al,[gs:bx]
		cmp al,'}'
		
		je complete	

		mov al,[gs:bx]
		cmp al,','
		je print_ln
		cmp al,'{'
		je skip_print_char
		mov [buffer],byte 1
		;skip_buffer:
		mov ax,[buffer]
		cmp ax,0
		jz skip_print_char
		jmp print_char

		print_ln:

		mov al,13
		mov ah,0eh
		int 10h
		mov al,10
		mov ah,0eh
		int 10h

		mov [buffer],byte 1

		jmp skip_print_char

		print_char:

		mov al,[gs:bx]
		mov ah,0eh
		int 10h

                ;mov [buffer],al
                ;push buffer
                ;call k_print
		;add sp,2

 		skip_print_char:

		cmp al,[di]
		je chk
		inc bx
		jmp cont_chk

	chk:
		push bx
		mov cx, [charcount] 
	check:
		mov al,[gs:bx]
		inc bx

		scasb
		loope check
		
		je succ

		mov di,query
		pop bx
		inc bx
		jmp cont_chk
	
	complete:
		;push crlf
		;call k_print
		;add sp,2
		jmp en
	succ:
		
		;push crlf
		;call print

		inc bx
		push  bx
		
		call findsect

		mov [buffer],byte 0
	en:
		mov sp,bp
		popa
		ret

k_print:
        push bp
        mov bp,sp
        mov si,[bp+4]
        again:
        mov al,[si]
        mov ah,0eh
        int 10h
        inc si
        mov al,[si]
        cmp al,0
        je endit
        jmp again
        endit:
        mov sp,bp
        pop bp
        ret



findsect:	; find the sector containing the given file	
	pusha
	mov bp,sp
	mov bx,[bp+18]
	cld
	mov word [sect],0
	mov cl,10
	cont_st:
		mov al,[gs:bx]
		inc bx

		cmp al,','
		jz finish
		cmp al,48
		jl mismatch
		cmp al,58
		jg mismatch

		sub al,48
		mov ah,0
		mov dx,ax
		mov ax,word [sect]
		mul cl
		add ax,dx
		mov word [sect],ax		
		jmp cont_st
		finish:
			;push word [sect]
			
			;call load 	;load exe in memory

		
			;jmp ploc:0000  ;switching to our program 

			jmp cont_st
			;push found_it
			;call print
			;mov sp,bp
			;popa
			;ret

		mismatch_end:
			mov sp,bp
			popa
			ret
		mismatch:
			;push fail
			;call print

			jmp mismatch_end

load:		;load the specified sector into RAM
	pusha
	mov bp,sp
	
	mov ah,0
	mov dl,0x80
	int 0x13

	mov ax,ploc
	mov es,ax
	mov cl,[bp+18] 
	mov al,1
	mov bx,0
	mov dl,drive 
	mov dh,0 
	mov ch,0 
	mov ah,2
	int 0x13
	jnc success
	err:
			push erro
			call print
	success:
		mov sp,bp
		popa
		ret

		
print:	;print a zero terminated string
	pusha
	mov bp,sp
	mov si,[bp+18] 
	cont:
		lodsb
		or al,al
		jz dne
		mov ah,0x0e
		mov bx,0
		mov bl,7
		int 10h
		jmp cont
	dne:
		mov sp,bp
		popa
		ret


tohex:
	pusha
	mov bp,sp
	mov dx, [bp+18]
	
	mov cx,4
	mov si,hexc
	mov di,hex+2
	
	stor:
	
	rol dx,4
	mov bx,15
	and bx,dx
	mov al, [si+bx]
	stosb
	loop stor
	push hex
	call print
	mov sp,bp
	popa
	ret


sector_msg: db 'sector: ',0
prompt2: db 'Search For File: ',0
found_it: db 'Found it...',13,10,0
msg db "Directory Listing:",13,10,0
welcome db "Welcome ! Type help or about to know more",10,13,0
erro db "Error loading sector",13,10,0
fail db "File not found !",13,10,0
buffer times 1024 db 0
query times 30 db 0
arr times 10 db 0
first_time db 1
nl db 10,13,0
prompt db 10,13, ">>",0
charcount dw 0
crlf db 13,10,0

hex db "0x0000",10,13,0
hexc db "0123456789ABCDEF"
first db 0
sect dw 0

