	;512 bytes os
	;think I'll call it Jacqueline
	;assemble with nasm

	cpu 386

	bits 16

	org 0x7C00

	;set A20 line
        cli
        xor cx,cx
	mov ds,cx
	mov es,cx
Loop1:
        in al,0x64
        test al,0x02
        loopnz Loop1
        mov al,0xD1
        out 0x64,al
Loop2:
        in al,0x64
        test al,0x02
        loopnz Loop2
        mov al, 0xDF
        out 0x60,al
        mov cx,14h
Loop3:
        out 0xED,ax
        loop Loop3

	;Load GDT
	lgdt [GDT]

	;load IDT
	lidt [IDT]

	;Set the protected mode flag
        mov eax,cr0            ; load the control register in
        or  al,1               ; set bit 1: pmode bit
        mov cr0,eax            ; copy it back to the control register

	;jump to the protected mode code
	jmp Code:ProtectedMode
GDT:
;null descriptor, used as pointer to the GDT
	dw	3*8		;limit
	dd	GDT		;GlobalDescriptorTable
	dw	0		;fill up to 8 bytes
	Code equ $-GDT
;8	;linear code descriptor
	dw	0FFFFh
	dw	0
	db	0
	db	10011010b
	db	11001111b
	db	0
	Data equ $-GDT
;16	;linear data descriptor
	dw	0FFFFh
	dw	0
	db	0
	db	10010010b
	db	11001111b
	db	0

IDT:	dw 34*8
	dd KeyInt-(33*8)

KeyInt:	;keyboard interupt
	dw	IncomingKey
	dw	8
	dw	1000111000000000b
	dw	0

ProtectedMode:
	bits 32

	;First thing is to set up the segment registers
	mov ax,Data
	mov ds,ax
	mov es,ax
	mov ss,ax
	mov esp,0xFFFF

	;After that the hardware interupts are replaced to the interupts 32-47
	mov	al,11h
	out	20h,al
	out	0A0h,al

	mov	al,32		;ofset
	out	21h,al
	mov	al,40		;ofset
	out	0A1h,al

	mov	al,4		;irq 2 conected to pic 2
	out	21h,al
	mov	al,2
	out	0A1h,al

	mov	al,1		;8086 mode
	out	21h,al
	out	0A1h,al
	
	mov	al,10111101b	;keyboard/floppy open
	out	21h,al

	sti

	mov esi,0x7C00
	int 33

Halt:	hlt
	jmp Halt

IncomingKey:
	xor eax,eax
	in al,60h		;read key
	cmp al,0x50
	jz ScrollDown
	cmp al,0x48
	jz ScrollUp
	cmp al,0x4B
	jz Left
	cmp al,0x4D
	jz Right
	cmp al,0x4E
	jz Plus
	cmp al,0x4A
	jz Minus
	cmp al,0x34
	jz Bigger
	cmp al,0x33
	jz Smaller
	cmp al,0x1B
	jz HaakOpen
	cmp al,0x1A
	jz HaakDicht
	cmp al,0x9C
	jz Enter
	jmp Display

ScrollDown:
	add esi,0x10
	jmp Display
ScrollUp:
	sub esi,0x10
	jmp Display
Left:
	dec esi
	jmp Display
Right:
	inc esi
	jmp Display
Plus:
	inc byte [esi]
	jmp Display
Minus:
	dec byte [esi]
	jmp Display
Bigger:
	add byte [esi],0x10
	jmp Display
Smaller:
	sub byte [esi],0x10
	jmp Display
HaakOpen:
	sub esi,0x10000
	jmp Display
HaakDicht:
	add esi,0x10000
	jmp Display
Enter:
	call esi
	;jmp Display

Display:
	;clear the screen
	mov edi,0xB8000
	mov ecx,25*80
.Clear:
	mov word [edi],0x0200
	inc edi
	inc edi
	dec ecx
	cmp ecx,0
	jnz .Clear

;	mov edx,0xB8000+160-4
;	call DisplayNibble
;	shr eax,4
;	inc edx
;	inc edx
;	call DisplayNibble

	;display stuf at the top
	mov edx,0xB8000+16+2
	mov eax,Lijst
Next:
	mov byte [edx],"0"
	inc edx
	inc edx
	mov bl,[eax]
	mov byte [edx],bl
	inc eax
	inc edx
	inc edx
	inc edx
	inc edx
	cmp eax,Lijst+16
	jnz Next

	;display addresses/data
	mov edi,0xB8000+(80*2)
	mov eax,esi
	and eax,0xFFFFFFF0
	mov edx,0xB8000+(80*2)
NextAddres:
	add edi,(80*2)-(16*2)

	mov ecx,8
.Next:
	rol eax,4
	call DisplayNibble
	inc edx
	inc edx
	loop .Next

	mov ecx,0x10
	xchg eax,ebx
.Byte:
	xor eax,eax
	mov al,[ebx]
	mov [edi],al
	inc edi
	inc edi
	inc edx
	inc edx
	ror al,4
	call DisplayNibble
	inc edx
	inc edx
	ror al,4
	call DisplayNibble
	inc edx
	inc edx
	inc ebx
	loop .Byte
	xchg eax,ebx

	add edx,(80*2)-16-(6*0x10)
	cmp edx,0xB8000+(25*80*2)
	jnz NextAddres

	;display pointer
	mov eax,esi
	and eax,0x0000000F
	mov bl,6
	mul bl
	add eax,0xB8000+(80*2)+16+3
	mov byte [eax],0x40
	mov byte [eax+2],0x40

	mov al,20h		;end of interupt
	out 20h,al
	iret

;in:	eax=lower part is the nibble
;	edx=place to put nible
;does:	print a nible
DisplayNibble:
	push eax
	and eax,1111b
	mov al,[Lijst+eax]
	mov [edx],al
	pop eax
	ret

Lijst:	db "0123456789ABCDEF"
;String: db "Jacqueline512"

	times 510-($-$$) db 0
	dw 0xAA55

	;comment the folowing line out for 512 bytes
	;times (80*2*18*512)-512 db 0