[bits 16]
[org 0]

firstflag dw 1
temp dw 0
temp_word dw 0

hex db "0x0000",10,13,0
hexc db "0123456789ABCDEF"

erro db "Error loading sector",10,13,0
fail db "File not found !",10,13,0

time_type equ 01h

%define drive 0x80
%define ploc 0x9000
%define ftable 0x2000

_inc_charcount:
	push bp
	mov bp,sp
        inc byte [charcount]
	mov sp,bp
	pop bp 
	ret

_reset_charcount:
	push bp
	mov bp,sp
	mov [charcount],byte 0
	mov sp,bp
	pop bp 
	ret

_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 _k_print
                mov sp,bp
                popa
                ret


_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,[bp+20]
        mov bx,0
        mov dl,drive
        mov dh,0
        mov ch,[bp+22]
        ;mov ch,0
        mov ah,2
        int 0x13
        jnc success
        err:
           push erro
           call _k_print
        success:
           ;jmp ploc:0000  ;switching to our program
           mov sp,bp
           popa
           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

                mismatch_end:
                        mov sp,bp
                        popa
                        ret
                mismatch:
                        push fail
                        call _k_print
                        jmp mismatch_end

_k_exec:
	push bp
	mov bp,sp
	mov bx,[bp+8]
	push bx
	mov bx,[bp+6]
	push bx
	mov bx,[bp+4]
	push bx
	call _load
	jmp ploc:0000
	mov sp,bp
	pop bp
	ret

_load_file_table:
	push bp 
	mov bp,sp
	mov di,[bp+4]
        mov bx,0
        cld
loop_file_table:
	mov al,[gs:bx]
	cmp al,'}'
	je end_load_table
	inc bx
	mov [di],al
	inc di
	jmp loop_file_table
end_load_table:
	mov [di],byte '}'
	inc di
	mov [di],byte 0
	mov ax,bx
	mov sp,bp
	pop bp
	ret

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

        cmp ax,ax ; to set zero flag
	lea dx,[query]
        mov di,dx

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

                je complete

                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 fail
                call _k_print
                jmp en
        succ:

                inc bx
                push  bx

                call _findsect
        en:
                mov sp,bp
                popa
                ret


_k_putc:
        ;pusha
        push bp
        mov bp,sp

        ;inc word [charcount]

        mov al,[bp+4]
        ;mov al,[bp+18]
        mov ah,0eh
        int 10h

        mov sp,bp
        pop bp
        ;popa
        ret


_k_getch:
        push bp
        mov bp,sp
        mov ax,0
        mov ah,10h
        int 16h
        mov [temp_word],word 0
        mov [temp_word],byte al
        mov ax,[temp_word]
        mov sp,bp
        pop bp
        ret

_k_print:       ;print a zero terminated string
        ;pusha
        push bp
        mov bp,sp
        ;mov si,[bp+18]
        mov si,[bp+4]
        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
                pop bp
                ret


%macro cpush 1
        mov [temp_word],word 0
        mov [temp_word],%1
        mov ax,[temp_word]
        push ax
%endmacro

%macro cpop 1
        pop ax
        mov [temp_word],ax
        mov %1,[temp_word]
%endmacro

_is_first:
        push bp
        mov bp,sp
        mov ax,[firstflag]
        mov [firstflag],word 0
        mov sp,bp
        pop bp
        ret

_tohex:
        ;pusha
        push bp
        mov bp,sp
        ;mov dx, [bp+18]
        mov dx, [bp+4]

        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 _k_print
        mov sp,bp
        ;popa
        pop bp
        ret

_k_readline:
	push bp
	mov bp,sp
	sub sp,2
	push di
	mov di,[bp+4]
read_loop:
	mov ah,0
        int 16h
        cmp al,0dh
	je skip_loop
	cmp al,8
	je skip_loop2
	mov [temp],al
        mov ah,0eh
        int 10h
skip_loop2:
	mov al,[temp]
	mov [di],byte al
	inc di
	mov [di],byte al
        cmp di,0
	jmp read_loop
skip_loop:
	pop di
	xor ax,ax
	mov sp,bp
	pop bp
	ret


_getch:
	push bp
	mov bp,sp
	mov ah,0
	int 16h
	mov [the_ch],word 0
	mov [the_ch],al
	mov ax,[the_ch]
	mov sp,bp
	pop bp
	ret

error_message: db 'Unable to load sector...',13,10,0
ok_message: db 'Sector loaded...',13,10,0
the_ch: dw 0
main_ptr: dw 0
query times 30 db 0
arr times 10 db 0
first_time db 1
nl db 10,13,0
prompt db 10,13, "PCOS>",0
charcount dw 0

sect dw 0

