[bits 16]

global _getch
global _print

firstflag dw 1
temp dw 0
temp_word dw 0

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

time_type equ 01h

%define drive 0x80
%define ploc 0x9000

%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

_get_main_ptr:
	push bp
	mov bp,sp
	mov ax,[main_ptr]
	mov sp,bp
	pop bp
	ret

_set_main_ptr:
	push bp
	mov bp,sp
	mov ax,[bp+4]
	mov [main_ptr],ax
	mov sp,bp
	pop bp
	ret

_call_main:
	push bp
	mov bp,sp
	mov dx,[bp+4]
	call dx
	pop bp
	ret

_get_fat:
	push bp
	mov bp,sp
	mov bx,[bp+4]
	mov al,[gs:bx]
	cpush al
	pop ax
	mov sp,bp
	pop bp
	ret

_readfile:
	pusha
	mov bp,sp

        mov ah,0
        mov dl,0x80
        int 0x13

        mov ax,ds
        mov es,ax
        mov cl,[bp+18]
        mov al,1
        mov bx,[bp+20]
        mov dl,drive
        mov dh,0
        mov ch,0
        mov ah,2
        int 0x13
	jc err
	jmp done

err:
	push error_message
	call _print
	add sp,2
	jmp end

done:
	push ok_message
	call _print
	add sp,2

end:
	mov sp,bp
	popa
	ret

_array_get:
	push bp
	mov bp,sp

	mov bx,[bp+4]
	mov dx,[bp+6]
	add bx,dx
	mov ax,[bx];

	mov sp,bp
	pop bp
	ret

_putc:
        push bp
        mov bp,sp

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

        mov sp,bp
        pop bp
        ret


;_putc:
        ;push bp
        ;mov bp,sp
	;pusha

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

        ;mov sp,bp
        ;pop bp
	;popa
        ;ret

_get_dir:
	push bp
	mov bp,sp
	mov bx,0

	mov di,[bp+4]
.LL0:	mov al,[gs:bx]
	mov [di],al
	inc di
	cmp al,'}'
	je .END

	inc bx
	jmp .LL0

.END:	mov sp,bp
	pop bp
	ret

_k_bzero:
	mov bp,sp
	lea si,[bp+4]
	mov bx,[bp+8]
.LBZ:
	mov [si],byte 0
	inc si 
	dec bx
	mov ax,bx
	cmp ax,0
	jne .LBZ
	leave
	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 _print
        mov sp,bp
        ;popa
        pop bp
        ret

_get_time_day:
        push bp
        mov bp,sp

        mov ah,04h
        int 1Ah

        mov [temp],word 0
        mov [temp],dl
        mov ax,[temp]

        mov sp,bp
        pop bp
        ret

_get_time_month:
        push bp
        mov bp,sp

        mov ah,04h
        int 1Ah

        mov [temp],word 0
        mov [temp],dh
        mov ax,[temp]

        mov sp,bp
        pop bp
        ret



_get_time_year:
        push bp
        mov bp,sp

        mov ah,04h
        int 1Ah

        mov [temp],word 0
        mov [temp],cl
        mov ax,[temp]

        mov sp,bp
        pop bp
        ret

_get_time_cent:
	push bp
	mov bp,sp

	mov ah,04h
	int 1Ah

	mov [temp],word 0
	mov [temp],ch
	mov ax,[temp]

	mov sp,bp
	pop bp
	ret

_get_time_hours:
	push bp
	mov bp,sp

	mov [temp],word 0
	mov dl,time_type
	mov ah,02h
	int 1Ah
	mov [temp],ch
	mov ax,[temp]

	mov sp,bp
	pop bp
	ret

_get_time_min:
	push bp
	mov bp,sp

	mov [temp],word 0
	mov dl,time_type
	mov ah,02h
	int 1Ah
	mov [temp],cl
	mov ax,[temp]

	mov sp,bp
	pop bp
	ret

_get_time_sec:
	push bp
	mov bp,sp

	mov [temp],word 0
	mov dl,time_type
	mov ah,02h
	int 1Ah
	mov [temp],dh
	mov ax,[temp]

	mov sp,bp
	pop bp
	ret

_get_time:
	push bp
	mov bp,sp
	mov dx,70h
	mov al,0
	out dx,al
	inc dx
	in al,dx

	mov [temp],al
	mov [temp+1],byte 0
	mov ax,[temp]

	mov sp,bp
	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


short_inc:
	push bp
	mov bp,sp
	mov ax,[bp+4]
	inc ax
	mov sp,bp
	pop bp
	ret

short_neq:
        push bp
        mov bp,sp
        mov bx,[bp+4];
        mov dx,[bp+6];
        cmp bx,dx
        jz end_eq3
        mov ax,0
        jmp end_eq4
end_eq3:
        mov ax,1
end_eq4:
        mov sp,bp
        pop bp
        ret

short_eq:
	push bp
	mov bp,sp
	mov bx,[bp+4];
	mov dx,[bp+6];
	cmp bx,dx
	je end_eq
	mov ax,0
	jmp end_eq2
end_eq:
	mov ax,1
end_eq2:
	mov sp,bp
	pop bp
	ret

short_lt:
	push bp
	mov bp,sp
	mov bx,[bp+4]
	mov dx,[bp+6]
	cmp bx,dx
	jl set_val
	mov ax,0
	jmp skip_val
set_val:
	mov ax,1
skip_val:
	mov sp,bp
	pop bp
	ret

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

_print:
	push bp
        mov bp,sp
        mov si,[bp+4]
        again:
        mov al,[si]
	cpush al
	call _putc
	add sp,2
        inc si
        mov al,[si]
        cmp al,0
        je endit
        jmp again
        endit:
	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
