	format PE console
	entry start

	include 'macro\import32.inc'

ROWS = 20
COLS = 70

	section '.code' code readable executable
start:
	mov ebx, 10
	push 0
	call [time]
	add esp, 4
	push eax
	call [srand]
	add esp, 4
.insert_items:
	call [rand]
	mov ecx, 26
	xor edx, edx
	div ecx
	add edx, 'A'
	push edx
	push tree
	call insert
	dec ebx
	jnz .insert_items

	mov ecx, ROWS*COLS
	mov edi, fbuff
	mov al, ' '
	rep stosb

	push 1
	push COLS/2 - 1
	push [tree]
	call fill_tree
	xor ebx, ebx
	mov esi, fbuff
.prin:
	mov byte [esi+COLS-1], 0
	push esi
	call [puts]
	add esp, 4
	add esi, COLS
	inc ebx
	cmp ebx, ROWS
	jb .prin


;        push dword [tree]
;        call inorder

	call [getchar]

	push 0
	call [exit]


fill_tree:
 root equ ebp+8
 x equ ebp+12
 y equ ebp+16

	enter 0, 0
	push ebx

	mov ebx, [root]
	cmp ebx, 0
	jz .return
	cmp dword [x], 1
	jb .return
	cmp dword [x], COLS-1
	jae .return

	cmp dword [y], 1
	jb .return
	cmp dword [y], ROWS-1
	jae .return

	mov ecx, [x]
	mov edx, [y]
	add edx, 2
	sub ecx, 2
	push edx
	push ecx
	push dword [ebx+4]
	call fill_tree

	imul eax, [y], COLS
	add eax, [x]
	mov cl, [ebx]
	mov [fbuff+eax], cl	    ; fbuff[y][x] = root->item;

	add eax, COLS
	mov byte [fbuff+eax-1], '/'  ; fbuff[y+1][x-1] = '/';
	mov byte [fbuff+eax+1], '\'  ; fbuff[y+1][x+1] = '\';

	mov ecx, [x]
	mov edx, [y]
	add ecx, 2
	add edx, 2
	push edx
	push ecx
	push dword [ebx+8]
	call fill_tree			; fill_tree(root->right, x+2, y+2);
.return:
	pop ebx
	leave
	ret 12

; ----------------------------------------------------------------
; Insert the item given in the Tree pointed by the address passed.
; Insert works in such a way that the  inorder traversal will give
; us results in non-decreasing sorted order.
; ----------------------------------------------------------------
insert:
 root equ ebp+8
 item equ ebp+12
	enter 0,0
	mov eax, [root]
	cmp dword [eax], 0
	jz .new_node
	mov ecx, [eax]
	mov edx, [item]
	cmp [ecx], edx
	je .return
	jl .insert_right
; Otherwise insert left.
	add ecx, 4
.recursion:
	push edx
	push ecx
	call insert
	jmp .return
.insert_right:
	add ecx, 8
	jmp .recursion
.new_node:
	push 12
	call dword [malloc]
	add esp, 4
	mov ecx, [root]
	mov [ecx], eax
	mov edx, [item]
	mov [eax], edx
	mov dword [eax+4], 0
	mov dword [eax+8], 0
.return:
	leave
	ret 8


inorder:
root equ ebp+8

	enter 0,0
	cmp dword [root], 0
	jz .return

	mov eax, [root]
	add eax, 4
	push dword [eax]
	call inorder

	mov eax, [root]
	push dword [eax]
	call [putchar]
	add esp, 4

	mov eax, [root]
	add eax, 8
	push dword [eax]
	call inorder
.return:
	leave
	ret 4


	section '.data' data readable writable
fbuff db ROWS*COLS dup 0
tree dd 0


	section '.idata' import data readable

library msvcrt,'msvcrt.dll'

import	msvcrt,\
	exit,'exit',\
	rand,'rand',\
	malloc,'malloc',\
	printf,'printf',\
	getchar,'getchar',\
	putchar,'putchar',\
	puts,'puts',\
	time,'time',\
	srand,'srand'


