[BITS 16]
[ORG 0]

mov	ax,cs
mov	ds,ax
mov	es,ax
mov	ax,0x7000
mov	ss,ax
mov	sp,ss

mov	ax,0x2000
mov	gs,ax
mov	bx,0

	call	_main

%include "kern.asm"

_first:
dw 	1
_k_fgets:
push	bp
mov	bp,sp
push	di
push	si
add	sp,-4
mov	bx,word [bp+4]
mov	word [bp-8],bx
dec	sp
dec	sp
xor	ax,ax
mov	word [bp-0xA],ax
call	_k_getch
mov	word [bp-6],ax
jmp L2
L3:
mov	ax,word [bp-6]
cmp	ax,8
jne 	L4
L5:
mov	ax,word [bp-0xA]
test	ax,ax
jle 	L6
L7:
mov	bx,word [bp-8]
xor	al,al
mov	[bx],al
mov	bx,word [bp-8]
dec	bx
mov	word [bp-8],bx
mov	ax,word [bp-0xA]
dec	ax
mov	word [bp-0xA],ax
push	word [bp-6]
call	_k_putc
inc	sp
inc	sp
L6:
call	_k_getch
mov	word [bp-6],ax
jmp L2
L4:
push	word [bp-6]
call	_k_putc
inc	sp
inc	sp
mov	bx,word [bp-8]
mov	al,byte [bp-6]
mov	[bx],al
mov	bx,word [bp-8]
inc	bx
mov	word [bp-8],bx
mov	ax,word [bp-0xA]
inc	ax
mov	word [bp-0xA],ax
mov	ax,word [bp+6]
dec	ax
cmp	ax,word [bp-0xA]
jg  	L8
L9:
jmp L1
L8:
call	_k_getch
mov	word [bp-6],ax
L2:
mov	ax,word [bp-6]
cmp	ax,0xD
je  	LA
LB:
mov	ax,word [bp-6]
cmp	ax,0xA
jne	L3
LA:
L1:
mov	bx,word [bp-8]
xor	al,al
mov	[bx],al
add	sp,6
pop	si
pop	di
pop	bp
ret
_k_bzero:
push	bp
mov	bp,sp
push	di
push	si
dec	sp
dec	sp
xor	ax,ax
mov	word [bp-6],ax
jmp LE
LF:
mov	ax,word [bp-6]
add	ax,word [bp+4]
mov	bx,ax
xor	al,al
mov	[bx],al
LD:
mov	ax,word [bp-6]
inc	ax
mov	word [bp-6],ax
LE:
mov	ax,word [bp-6]
cmp	ax,word [bp+6]
jl 	LF
L10:
LC:
inc	sp
inc	sp
pop	si
pop	di
pop	bp
ret
_k_init:
push	bp
mov	bp,sp
push	di
push	si
mov	ax,0x200
push	ax
mov	bx,_buffer
push	bx
call	_k_bzero
add	sp,4
pop	si
pop	di
pop	bp
ret
_k_strlen:
push	bp
mov	bp,sp
push	di
push	si
add	sp,-4
mov	bx,word [bp+4]
mov	word [bp-6],bx
xor	ax,ax
mov	word [bp-8],ax
jmp L12
L13:
mov	bx,word [bp-6]
inc	bx
mov	word [bp-6],bx
mov	ax,word [bp-8]
inc	ax
mov	word [bp-8],ax
L12:
mov	bx,word [bp-6]
mov	al,[bx]
test	al,al
jne	L13
L14:
L11:
mov	ax,word [bp-8]
add	sp,4
pop	si
pop	di
pop	bp
ret
_k_printf:
push	bp
mov	bp,sp
push	di
push	si
xor	ax,ax
pop	si
pop	di
pop	bp
ret
_main:
push	bp
mov	bp,sp
push	di
push	si
call	_is_first
test	ax,ax
je  	L15
L16:
mov	bx,L17
push	bx
call	_k_print
inc	sp
inc	sp
mov	bx,L18
push	bx
call	_k_print
inc	sp
inc	sp
mov	bx,L19
push	bx
call	_k_print
inc	sp
inc	sp
mov	bx,L1A
push	bx
call	_k_print
inc	sp
inc	sp
call	_k_init
L15:
L1D:
mov	bx,L1E
push	bx
call	_k_print
inc	sp
inc	sp
call	_k_reset
mov	ax,0x1FF
push	ax
mov	bx,_buffer
push	bx
call	_k_fgets
add	sp,4
mov	bx,L1F
push	bx
call	_k_print
inc	sp
inc	sp
mov	bx,_buffer
push	bx
call	_k_strlen
inc	sp
inc	sp
test	ax,ax
je  	L20
L21:
mov	bx,_buffer
push	bx
call	_sys_exec
inc	sp
inc	sp
L20:
mov	ax,0x200
push	ax
mov	bx,_buffer
push	bx
call	_k_bzero
add	sp,4
L1C:
jmp	L1D
L22:
L1B:
xor	ax,ax
pop	si
pop	di
pop	bp
ret
L1F:
L23:
db 	0xD,0xA
db 	0
L1E:
L24:
db 	"SimpleOS> "
db 	0
L1A:
L25:
db 	0xD,0xA
db 	0
L19:
L26:
db 	"Author: Matthew W. Coan"
db 	0xD,0xA
db 	0
L18:
L27:
db 	"Inventive Implementations"
db 	0xD,0xA
db 	0
L17:
L28:
db 	">>>SimpleOS: Version: 1.0a"
db 	0xD,0xA
db 	0
	_memory:	resb 0x200
	_user_array:	resb 0xEA60
	_user_count:	resb 2
	_file_array:	resb 0x18E70
	_file_count:	resb 2
	_pointer:	resb 2
	_buffer:	resb 0x200
