[BITS 16]
[org 0]
[section .text]

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

	mov ah,03
	mov bh,0
	int 10h

	call _main

	jmp 0x1000:0

global _k_fgets
global _k_bzero
global _k_init
global _k_strlen
global _k_printf
global _load_program
global _main
global _k_print
global _k_getch
global _k_putc
global _k_strcmp
global _readcmd
global _search
global _is_first
global _sys_open
global _sys_read
global _sys_write
global _sys_close
global _sys_malloc
global _sys_free
global _sys_exec
global _sys_exit
global _sys_getpid

[segment .text]

%include "io.asm"

struc __File
   ._sector resw 1
   ._name resb 100
endstruc

struc __User
   ._name resb 30
   ._pass resb 30
endstruc


%define _i -2
%define _buf1 4
%define _sz 6

_k_bzero:
	push bp
	mov bp,sp
	sub sp,word 2
	mov [bp+_i],word 0
	push ax
.L19:	
	push word [bp+_i]
	mov bx,word [bp+_sz]
	pop dx
	cmp dx,bx
	jl .L20
	mov ax,word 0
	jmp .L21
.L20:	
	mov ax,word 1
.L21:	
	cmp ax,word 0
	je .L18
	push word 0
	mov bx,word [bp+_i]
	pop dx
	add bx,dx
	push bx
	mov bx,[bp+_buf1]
	pop dx
	push bx
	add bx,dx
	push bx
	mov dx,word 0
	pop di
	mov [di],dx
	mov ax,dx
	inc word [bp+_i]
	mov ax,word [bp+_i]
	jmp .L19
.L18:	
	leave
	ret

%undef _i
%undef _buf1
%undef _sz

%define _ch -2
%define _p -4
%define _count -6
%define _buf 4
%define _size 6

_k_fgets:
	push bp
	mov bp,sp
	sub sp,word 6
	mov ax,word [bp+_buf]
	mov [bp+_p],ax
	mov [bp+_count],word 0
	call _k_getch
	mov [bp+_ch],ax
.L1:	
	push word [bp+_ch]
	mov bx,word 13
	pop dx
	cmp dx,bx
	jne .L2
	push 0
	jmp .L3
.L2:	
	push 1
.L3:	
	push word [bp+_ch]
	mov bx,word 10
	pop dx
	cmp dx,bx
	jne .L4
	push 0
	jmp .L5
.L4:	
	push 1
.L5:	
	pop bx
	pop ax
	and ax,bx
	cmp ax,word 0
	je .L0
	push word [bp+_ch]
	mov bx,word 8
	pop dx
	cmp dx,bx
	je .L7
	mov ax,word 0
	jmp .L8
.L7:	
	mov ax,word 1
.L8:	
	cmp ax,word 0
	je .L9
	push word [bp+_count]
	mov bx,word 0
	pop dx
	cmp dx,bx
	jg .L11
	mov ax,word 0
	jmp .L12
.L11:	
	mov ax,word 1
.L12:	
	cmp ax,word 0
	je .L13
	mov di,[bp+_p]
	mov [di],word 0
	dec word [bp+_p]
	mov ax,word [bp+_p]
	dec word [bp+_count]
	mov ax,word [bp+_count]
	push word [bp+_ch]
	call _k_putc
	add sp,word 2
	jmp .L10
.L13:	
.L10:	
	call _k_getch
	mov [bp+_ch],ax
	jmp .L1
	jmp .L6
.L9:	
.L6:	
	push word [bp+_ch]
	call _k_putc
	add sp,word 2
	mov ax,word [bp+_ch]
	mov di,[bp+_p]
	mov [di],ax
	inc word [bp+_p]
	mov ax,word [bp+_p]
	inc word [bp+_count]
	mov ax,word [bp+_count]
	push word [bp+_count]
	push word [bp+_size]
	mov bx,word 1
	pop dx
	sub dx,bx
	mov bx,dx
	pop dx
	cmp dx,bx
	jge .L15
	mov ax,word 0
	jmp .L16
.L15:	
	mov ax,word 1
.L16:	
	cmp ax,word 0
	je .L17
	jmp .L0
	jmp .L14
.L17:	
.L14:	
	call _k_getch
	mov [bp+_ch],ax
	jmp .L1
.L0:	
	mov di,[bp+_p]
	mov [di],word 0
	leave
	ret

%undef _ch
%undef _p
%undef _count
%undef _buf
%undef _size


_k_init:
	push bp
	mov bp,sp
	push 512
	lea ax,[_buffer]
	push ax
	call _k_bzero
	add sp,word 4
	leave
	ret


%define _fmt 4

_k_printf:
	push bp
	mov bp,sp
	mov ax,word 0
	leave
	ret
%undef _fmt

%define _p -2
%define _c -4
%define _buf 4

_k_strlen:
	push bp
	mov bp,sp
	sub sp,word 4
	mov ax,word [bp+_buf]
	mov [bp+_p],ax
	mov [bp+_c],word 0
.L23:	
	mov di,[bp+_p]
	mov al,[di]
	cpush al
	mov bx,word 0
	pop dx
	cmp dx,bx
	jne .L24
	mov ax,word 0
	jmp .L25
.L24:	
	mov ax,word 1
.L25:	
	cmp ax,word 0
	je .L22
	inc word [bp+_p]
	mov ax,word [bp+_p]
	inc word [bp+_c]
	mov ax,word [bp+_c]
	jmp .L23
.L22:	
	mov ax,word [bp+_c]
	leave
	ret
%undef _p
%undef _c
%undef _buf

%define _code 4
%define _cmd 6

_load_program:
	push bp
	mov bp,sp
	nop
	leave
	ret

%undef _code
%undef _cmd


_main:
	push bp
	mov bp,sp
	call _is_first
	cmp ax,word 0
	je .L27
	push C0
	call _k_print
	add sp,word 2
	push C1
	call _k_print
	add sp,word 2
	push C2
	call _k_print
	add sp,word 2
	push C3
	call _k_print
	add sp,word 2
	call _k_init
	jmp .L26
.L27:	
.L26:	
.L29:	
	mov ax,word 1
	cmp ax,word 0
	je .L28
	push C4
	call _k_print
	add sp,word 2
	push 512
	mov bx,word 1
	pop dx
	sub dx,bx
	push dx
	lea ax,[_buffer]
	push ax
	call _k_fgets
	add sp,word 4
	push C3
	call _k_print
	add sp,word 2
	lea ax,[_buffer]
	push ax
	call _k_strlen
	add sp,word 2
	cmp ax,word 0
	je .L31
	lea ax,[_buffer]
	push ax
	call _sys_exec
	add sp,word 2
	jmp .L30
.L31:	
.L30:	
	push 512
	lea ax,[_buffer]
	push ax
	call _k_bzero
	add sp,word 4
	jmp .L29
.L28:	
	mov ax,word 0
	leave
	ret



[section .data]

C0: db '>>>SimpleOS: Version: 1.0a',13,10,0
C1: db 'Inventive Implementations',13,10,0
C2: db 'Author: Matthew W. Coan',13,10,0
C3: db 13,10,0
C4: db 'SimpleOS> ',0

[section .bss]

answer: resb 20
_buffer: resb 1
