[BITS 16]
[ORG 0x0200]

Start:

 jmp  0x9000:main

%DEFINE TEAL 0x03
%DEFINE RED 0x04
%DEFINE PURPLE 0x05
COL: db 0
ROW:  db 0
%macro Print 2
pusha
	xor ax, ax
	xor dx, dx
	mov dh, BYTE[ROW];puts the row into the dh register
	mov dl, BYTE[COL]
	xor bx, bx
	mov bl, %2
	mov si, %1
	call cPrint
	mov BYTE[COL], dl
 ;saves the rows for the next time we need to print
popa
%endmacro

Print_ln:

pusha   
	mov dh, BYTE[ROW]  
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x00            ;page 00
    inc dh            		;row 00
    mov dl, 0x00            ;col. 00    
	int 0x10
	mov BYTE[ROW], dh
	mov BYTE[COL], 0
	popa


ret
itoa:;number is passed into ax
jmp .beggining
.negate:

neg ax
push ax

mov al, '-'
mov ah, 0xe 
int 0x10
pop ax
jmp .top
.beggining:
xor bx , bx
mov cx, 10;mov into cx 10
cmp ax, 0
jl .negate


.top:
	;divide by 10 and push remainder onto stack 
	xor dx, dx;clear out remainder
	div cx ;divide ax by 10
	push dx;push the remainder onto the stack for later
	inc bx;count the number of digits
	test ax,ax;if ax = 0 then stop
jne .top

.loop:
	pop ax;restore the remainder
	add ax, '0';convert to ASCII
	mov ah, 0xe;print
	int 0x10
	dec bx;get ready for the next digit
	cmp bx, 0;if not zero then jump to .loop	
jne .loop
ret

cPrint:                   ; Routine: output string in SI to screen


 .top:
 	;Paramaters for Input 
    mov ah, 09h             ; Must be 9 to print color
    mov cx, 0x01 			;x position
    lodsb                   ; Get character from string
    test al, al
    je .done                ; If char is zero, end of string
    int 0x10                 ; Otherwise, print it

    mov ah, 0x02			;set cursor position
    mov bh, 0x00			;page
    inc dl 		;column
    int 0x10				;changes the cursor position so the next char can be written at the new location
    jmp .top

 .done:
    ret

;clears the screen and sets the cursor position to the top left 
 clear:
    mov ah, 0x0F            ;get current video mode
    mov al, 0x00            ;reset register
    int 0x10                ;get video mode
    mov ah, 0x00            ;set video mode
    int 0x10                ;reset screen
    mov ah, 0x02            ;set cursor pos
    mov bh, 0x01         ;page 00
    mov dh, 0x00            ;row 00
    mov dl, 0x00            ;col. 00
    int 0x10                ;set pos
	MOV BYTE[ROW], DH
	MOV BYTE[COL], DL
ret





main:		
;first stage of bootloader is loaded at the address 0x07c0:0x0FFFE
	;second stage of bootloader is loaded at address 0x9000:0x0FFFF
	cli
	mov ax, 0x9000	;adjust the segment registers
	mov ds, ax
	mov gs, ax
	mov fs, ax


Create_Stack:
	xor ax, ax
	mov es, ax
	mov ss, ax
	mov sp ,0x0FFFF
	sti
	
	
	Print LOAD_SUCCESS, TEAL
	call Print_ln



	

LOAD_SUCCESS:	db "YOU FINALLY LOADED STAGE 2!!!!!!!!!!!!!!!!",0





