BITS 16
ORG 0x10000

MOV AX, 1000h
MOV DS, AX

MOV AH, 06h
MOV AL, 00h
MOV BH, 07h
MOV CX, 0x0000
MOV DX, 0x184F
INT 10h

MOV AH, 02h
MOV BH, 00h
MOV DH, 00h
MOV DL, 00h
INT 10h

jmp start

answer:
    times 500 db 0

textwritten:
    times 500 db 0

vercommand:
    db 'version', 0

pookiecommand:
    db 'pookie', 0

pookieanswer:
    db 'hai u just ran the pookie command', 0

veranswer:
    db 'LibertyOS ver 0.0.1', 0
message:
    db 'Welcome to LibertyOS!', 13, 10, 'ver 0.0.1', 13, 10, 10, 0

message2:
    db 'mdr/user - ', 0

cmplabelvars:
    push si
    push ax
    push di

    MOV SI, textwritten

.start11:
    MOV AL, [SI]
    CMP AL, [DI]
    JNE .notequal

    CMP AL, 0
    JE .done12

    INC SI
    INC DI
    JMP .start11

.notequal:
    CMP AH, 1
    JMP .done13

.done12:
    CMP AH, AH
    JMP .done13

.done13:
    pop di
    pop ax
    pop si
    ret

clearout:
    push bx
    xor bx, bx
.start10:
    cmp byte [textwritten + bx], 0
    je .done7
    MOV BYTE [textwritten + bx], 0
    inc bx
    jmp .start10
.done7:
    pop bx
    ret


checklength:
    push si
    XOR CX, CX
    XOR DX, DX
    CLD

.count:
    LODSB
    CMP AL, 0
    JE .done

    INC DX
    JMP .count

.done:
    pop si 
    ret

print:
    call checklength
    push bx
    push ax
    XOR BX, BX
    
.printing:
    CMP BX, DX
    JE .done1
    MOV AH, 0Eh
    MOV AL, [SI + BX]
    INT 10h
    INC BX
    JMP .printing

.done1:
    pop ax
    pop bx
    ret

edit:
    push ax
    push si
    push di

.editing:
    MOV AL, [DI]
    MOV [SI], AL

    CMP AL, 0
    JE .done5

    INC SI
    INC DI
    JMP .editing

.done5:
    pop di
    pop si
    pop ax
    ret


start:
    mov si, message
    call print

    mov si, message2
    call print

    intermediate:
        XOR DI, DI
        jmp waitforkey
        
    waitforkey:
        XOR AX, AX
        MOV SI, AX
        INT 16h

        CMP AH, 01h
        je done

        CMP AH, 0Eh
        je checkifnewline

        CMP AH, 1Ch
        MOV BYTE [textwritten + DI], 0
        je newline

        MOV BYTE [textwritten + DI], AL
        INC DI

        MOV AH, 0Eh
        INT 10h

        jmp waitforkey

    newline:
        MOV DI, vercommand
        call cmplabelvars
        JE .ver
        XOR DI, DI
        MOV DI, pookiecommand
        call cmplabelvars
        JE .pookie
        push si
        push ax
        mov ah, 03h
        mov bh, 0
        int 10h

        XOR SI, SI
        XOR AX, AX
        MOV AL, DL
        MOV SI, AX

        MOV AH, 0Eh
        MOV AL, 0Dh
        INT 10h

        MOV AH, 0Eh
        MOV AL, 0Ah
        INT 10h

        pop ax
        pop si

        XOR DI, DI

        mov si, message2
        call print

        jmp waitforkey

    .ver:
        call clearout
        ; Print line break
        MOV AH, 0Eh
        MOV AL, 0Dh
        INT 10h
        MOV AL, 0Ah
        INT 10h

        mov si, veranswer
        call print

        ; Print prompt on next line
        MOV AH, 0Eh
        MOV AL, 0Dh
        INT 10h
        MOV AL, 0Ah
        INT 10h

        mov si, message2
        call print

        xor di, di
        jmp waitforkey
    .pookie:
        call clearout
        ; Print line break
        MOV AH, 0Eh
        MOV AL, 0Dh
        INT 10h
        MOV AL, 0Ah
        INT 10h

        mov si, pookieanswer
        call print

        ; Print prompt on next line
        MOV AH, 0Eh
        MOV AL, 0Dh
        INT 10h
        MOV AL, 0Ah
        INT 10h

        mov si, message2
        call print

        xor di, di
        jmp waitforkey

    checkifnewline:
        mov ah, 03h
        mov bh, 0
        int 10h     

        cmp dl, 0
        jne backspace

        cmp dh, 0
        je waitforkey

    backspace:
        CMP DI, 0
        JNE .start9
        jmp waitforkey
    .start9:
        MOV AH, 0Eh
        MOV AL, 08h
        INT 10h

        MOV AH, 0Eh
        MOV AL, ' '
        INT 10h

        MOV AH, 0Eh
        MOV AL, 08h
        INT 10h

        DEC DI

        jmp waitforkey

    done:
        cli
        hlt
        jmp done