%define drive 0x80
%define ploc 0x9000
[BITS 16]
[org 0]
[section .text]

start:
	mov ax,cs
	mov ds,ax
	mov es,ax
	mov ax,0x7000
	mov ss,ax
	mov sp,ss
	cmp byte [first],0
	mov ax,0x2000
	mov gs,ax
	mov bx,0
	mov ah,03
	call _main

do_halt:
	jmp do_halt

global _open
global _flenth
global _readfile
global _writefile
global _close
global _spawnl
global _reboot
global _print
global _gets
global _memset
global _init
global _fin
global _main
global _print
global _init
global _fin
global _shutdown
global _gets
global _atoi
global _strcpy
global _strstr
global _memset
global _readfile
global _writefile
global _spawnl
global _strlen
global _open
global _close

[segment .text]

%include "kern_io.asm"

struc _File
   ._is_open resw 1
   ._name resb 16
   ._sector resw 1
endstruc

struc _Task
   ._id resw 1
   ._run_state resw 1
   ._exit_code resw 1
   ._name resb 16
   ._code resb 512
endstruc


%define _ret -2
%define _ptr 4

_atoi:
	push bp
	mov bp,sp
	sub sp,word 2
	mov [bp+_ret],word 0
	mov ax,word [bp+_ret]
	leave
	ret
%undef _ret
%undef _ptr

%define _fp 4

_close:
	push bp
	mov bp,sp
	mov [bp+_fp+_File._is_open],word 0
	leave
	ret

%undef _fp


_fin:
	push bp
	mov bp,sp
	nop
	leave
	ret


%define _rc -2
%define _buffer 4
%define _size 6

_gets:
	push bp
	mov bp,sp
	sub sp,word 2
	mov [bp+_rc],word 0
	mov ax,word [bp+_rc]
	leave
	ret
%undef _rc
%undef _buffer
%undef _size


_init:
	push bp
	mov bp,sp
	nop
	leave
	ret


%define _cmd -64

_main:
	push bp
	mov bp,sp
	sub sp,word 64
	call _init
	push C0
	call _print
	add sp,word 2
	push C1
	call _print
	add sp,word 2
	push C2
	call _print
	add sp,word 2
	push C3
	call _print
	add sp,word 2
	push C4
	call _print
	add sp,word 2
	push 64
	push 0
	lea ax,[bp+_cmd]
	push ax
	call _memset
	add sp,word 6
.L1:	
	push 64
	mov bx,word 1
	pop dx
	sub dx,bx
	push dx
	lea ax,[bp+_cmd]
	push ax
	call _gets
	add sp,word 4
	push ax
	mov bx,word 64
	pop dx
	cmp dx,bx
	jl .L2
	mov ax,word 0
	jmp .L3
.L2:	
	mov ax,word 1
.L3:	
	cmp ax,word 0
	je .L0
	lea ax,[bp+_cmd]
	push ax
	call _spawnl
	add sp,word 2
	push C4
	call _print
	add sp,word 2
	push 64
	push 0
	lea ax,[bp+_cmd]
	push ax
	call _memset
	add sp,word 6
	jmp .L1
.L0:	
	call _fin
	push C5
	call _print
	add sp,word 2
	call _shutdown
	leave
	ret

%undef _cmd

%define _ret -2
%define _buffer 4
%define _ch 6
%define _size 8

_memset:
	push bp
	mov bp,sp
	sub sp,word 2
	mov [bp+_ret],word 0
	mov ax,word [bp+_ret]
	leave
	ret
%undef _ret
%undef _buffer
%undef _ch
%undef _size

%define _i -2
%define _j -4
%define _ptr -6
%define _ptr0 -7
%define _buffer -17
%define _name 4
%define _fp 6

_open:
	push bp
	mov bp,sp
	sub sp,word 17
	mov [bp+_fp+_File._is_open],word 1
	push word [bp+_name]
	push word [bp+_fp+_File._name]
	call _strcpy
	add sp,word 4
	push word [bp+_name]
	lea ax,[_fat_table]
	push ax
	call _strstr
	add sp,word 4
	mov [bp+_ptr],ax
	push word [bp+_ptr]
	push word [bp+_name]
	call _strlen
	add sp,word 2
	push ax
	mov bx,word 1
	pop dx
	add dx,bx
	mov bx,dx
	pop dx
	add dx,bx
	mov [bp+_ptr],dx
	push C6
	push word [bp+_ptr]
	call _strstr
	add sp,word 4
	push ax
	cpop al
	pop ax
	mov [bp+_ptr0],ax
	push 10
	push 0
	lea ax,[bp+_buffer]
	push ax
	call _memset
	add sp,word 6
.L5:	
	push word [bp+_ptr]
	mov bx,word [bp+_ptr0]
	pop dx
	cmp dx,bx
	jne .L6
	mov ax,word 0
	jmp .L7
.L6:	
	mov ax,word 1
.L7:	
	cmp ax,word 0
	je .L4
	push word 0
	mov bx,word [bp+_j]
	pop dx
	add bx,dx
	push bx
	lea bx,[bp+_buffer]
	pop dx
	add bx,dx
	push bx
	mov di,[bp+_ptr]
	mov al,[di]
	pop di
	mov [di],al
	cpush al
	pop ax
	push word [bp+_ptr]
	mov bx,word 1
	pop dx
	add dx,bx
	mov [bp+_ptr],dx
	push word [bp+_j]
	mov bx,word 1
	pop dx
	add dx,bx
	mov [bp+_j],dx
	jmp .L5
.L4:	
	lea ax,[bp+_buffer]
	push ax
	call _atoi
	add sp,word 2
	mov [bp+_fp+_File._sector],ax
	mov ax,word 0
	leave
	ret
%undef _i
%undef _j
%undef _ptr
%undef _ptr0
%undef _buffer
%undef _name
%undef _fp

%define _message 4

_print:
	push bp
	mov bp,sp
	mov ax,word 0
	leave
	ret
%undef _message

%define _rc -2
%define _fp 4
%define _buffer 6
%define _size 8

_readfile:
	push bp
	mov bp,sp
	sub sp,word 2
	mov [bp+_rc],word 0
	mov ax,word [bp+_rc]
	leave
	ret
%undef _rc
%undef _fp
%undef _buffer
%undef _size


_shutdown:
	push bp
	mov bp,sp
	nop
	leave
	ret


%define _fp -20
%define _rc -22
%define _code -534
%define _pf -536
%define _name 4

_spawnl:
	push bp
	mov bp,sp
	sub sp,word 536
	mov [bp+_rc],word 0
	lea ax,[bp+_fp]
	push ax
	push word [bp+_name]
	call _open
	add sp,word 4
	push 512
	lea ax,[bp+_code]
	push ax
	push word [bp+_fp]
	call _readfile
	add sp,word 6
	push word [bp+_fp]
	call _close
	add sp,word 2
	lea ax,[bp+_code]
	mov [bp+_pf],ax
	call [bp+_pf]
	mov [bp+_rc],ax
	push ax
	mov ax,word [bp+_rc]
	leave
	ret
%undef _fp
%undef _rc
%undef _code
%undef _pf
%undef _name

%define _ptr1 4
%define _ptr2 6

_strcpy:
	push bp
	mov bp,sp
	mov ax,word 0
	leave
	ret
%undef _ptr1
%undef _ptr2

%define _ptr 4

_strlen:
	push bp
	mov bp,sp
	mov ax,word 0
	leave
	ret
%undef _ptr

%define _ptr -2
%define _buffer 4
%define _key 6

_strstr:
	push bp
	mov bp,sp
	sub sp,word 2
	mov [bp+_ptr],word 0
	mov ax,word [bp+_ptr]
	leave
	ret
%undef _ptr
%undef _buffer
%undef _key

%define _fp 4
%define _buffer 6
%define _size 8

_writefile:
	push bp
	mov bp,sp
	mov ax,word 0
	leave
	ret
%undef _fp
%undef _buffer
%undef _size



[section .data]

C0: db 'Wellcome to PCOS...',13,10,13,10,0
C1: db 'Programmer: Matthew W. Coan',13,10,0
C2: db 'Email: matthewcoan1976@hotmail.com',10,0
C3: db 'Web: http://matthewcoan.com/',13,10,13,10,0
C4: db 'PCOS> ',0
C5: db 'sutdown...',10,0
C6: db '-',0

[section .bss]

first: resw 1
answer: resb 20
_fat_table: resb 1

