.model small
.stack 100h

.data

ERROR1	DB	  10,13,'Nepodarilo sa otvorit(vytvorit) subor$'
ERROR2	DB	  10,13,'Nepodarilo sa nacitat data$'
ERROR3	DB	  10,13,'Nepodarilo sa zatvorit subor$'
ERROR4	DB	  10,13,'cislo mimo rozsahu <-99 ; 99>$'

INFO0	DB	  10,13,'stlacte lubovolnu klavesu$'
NL	DB	  10,13,'$'
text1 	DB	  10,13,10,13,'Prebieha usporiadanie...',10,13,'$'
text2 	DB	  10,13,' Suma zapornych cisel: $'
T_sort	db	10,13,'Sort Ukonceny : $'

FILEI  DB        'IN.TXT',0
FILEO  DB        'OUT.TXT',0

POCET 	DB	0,0

LDATA   DB        255 DUP('$')
ODATA	DB	255 dup(' ') 

to_print DW	0
number 	db 	7,0,7 dup(0)
dlzka	dw	0
pc	db	0
pom	db	0
pom2	dw	0
swap	db	0
data1	db	0
data2	db	0
suma_z  db	0
thand	dw	0
suma_dec	dw	0 
f_min	db	0

.code

start:
	mov	ax,@data
	mov	ds,ax
	call	zmazobr			;procedeura na zmaznie obrazovky

openf:	mov	ah, 3dh			;otvorenie suboru 'in.txt'
	mov	dx, offset FILEI	;v ktorom su ulozene vstupne data
	mov	al, 2
	int 	21h			;
	jnc	loaddata		;ak bola operacia uspesna skoci sa na 'loaddata'
	mov	dx, offset error1	;ak nebola vytlaci sa chybova hlaska a ukonci sa program
	mov	ah,09h			;
	int	21h			;
	call 	koniec			;ukoncenie programu 

loaddata:				;inak je handler v AX ale budem ho potrebovat pri zatvarani suboru
	mov	bx, ax
	mov	ah, 3fh			;pre nacitavanie 'ah = 3fh'
	mov	dx, offset LDATA
	mov	cx, 200			;maximalny pocet znakov
	int	21h
	mov	dlzka,ax		;velkost suboru sa ulozi do premennej dlzka	

	jnc	closef			;ak sa podarilo nacitat data skoci sa na 'closef'
	mov	dx, offset error2	;ak nie vypise sa chybova hlaska
	mov	ah, 09h
	int	21h
	jmp	koniec			;ukoncenie programu

closef:
	mov	ah, 3eh			;zavretie vstupneho suboru
	int	21h			;volanie prerusenia
	mov	dx, offset Ldata
	mov	ah, 09h
	int	21h			
	jnc	convertbin		;
	
	mov	dx, offset ERROR3	;doslo k chybe
	mov	ah,09h			;potom s vytlaci chybova hlaska
	int	21h			
	jmp	koniec			;ukonci sa program
	
convertbin:				;prevod cisla z dec -> bin
	call	rdnumber		;volanie procedury ,kt. prevedie cislo do doplnkoveho kodu
	call	rd_done			;volanie procedury ,kt. vytlaci hlasku o uspesnok prevedeni

sorting:	
	call	sort			;volanie procedury ,kt. usporiada cislav doplnkovom kode 
;	call	sort_done		;volanie procedury ,kt. vytlaci hlasku o uspesnom zosortovani

suma_cisel:
	call 	suma			;volanie procedury ,kt. scita sumu zapornych parnych cisel

	call	print			;vytlacenie zosortovanej postupnosti na obr. a sucesne 
					;aj zapis do suboru avsak uz v desiatkovej sustave
	call	koniec			;ukocenie programu

;------------------PROCEDURY------------------------

;---------------Procedura 'rdnumber'----------------
;-prevod cisla z retazca do cisla v dvojkovom doplnkovom kode-	
rdnumber proc			
				;vstupny v BX, vystup AX, znamienko CH, nacital som cislo CL=1 inak CL=0
	mov	bx, offset LDATA

read_again:
	xor	cx,cx		;nulovanie 'cx','dx','ax'
	xor	dx,dx
	xor	ax,ax

analyze:
	mov	dl,[bx]
	cmp	dl,47		;moze to byt cislo
	ja	analyze1	;ak je skoci sa na 'analyze1'

	jmp	bad		;ak to nieje cislo skace sa na 'bad'

analyze1:
	cmp	dl,57		;ak dl<57 potom je to kladne cislo
	jna	positive	;ak ano potom skok na 'positive'

bad:	
	cmp	cl,1		;presvedcim sa ci som uz spracovcal cele cislo
	jz	endstr
				;v ax ucelene cislo
				;v ch znamienko

	cmp	byte ptr[bx],'-';je to znamienko
	jz	set_sign	
				;nieje to znamienko
	cmp	byte ptr[bx],0	;kontrola konce retazca
	jz	endstr

	cmp	dlzka,0
	jnz	next1
	ret

next1:	xor	cx,cx
	inc 	bx
	dec	dlzka
	jmp	analyze

set_sign:
	mov	ch,1		;ak '-' => cl <- 1
	inc	bx		;posun
	dec	dlzka
	jmp	analyze		;pokracovat v retazci
	
positive:
	mov	cl,1
	mov	dl,10		;10-tkou sa bude nasobit
	mul	dx		;AX <= AX * 10
	mov	dl,[bx]		;nacitanie znaku
	sub	dl,30h		;z ASCII znaku sa dostane cislo
	add	ax,dx		;pripocitanie
	inc	bx		;posun
	dec	dlzka
	jmp	analyze		;pokracovanie v analyze 

endstr:
	cmp	cl,1		;je to ucelene cislo ?
	jz	range		;ak ano pokracuj dalej
	ret
range:	cmp	ch,1		;ak je cislo zaporne
	jz	mns
	cmp	ax,99		;max. klad. cislo
	ja	err_r		;nespravny rozsah => koniec

	jmp	save_num
mns:	
	cmp	ax,99		;max. zapor. cislo
	ja	err_r		;nespravny rozsah => koniec

save_num:
	push 	bx		; bx -> stack
	
	mov	bx,offset POCET
	mov	dh,[bx]	
	inc	bx
	mov	dl,[bx]
	mov	bx,offset LDATA
	add	bx,dx
	cmp	ch,1		;je to zaporne cislo
	jnz	pos_num		;ak je tak do doplnku
	not	ax		;negacia
	inc	ax		;ax = ax+1
pos_num:
	mov	[bx],al		;*bx<- al 
	add	dx,1		;
	mov	bx,offset pocet
	mov	[bx],dh
	inc	bx
	mov	[bx],dl
	pop	bx		;bx <- stack
	inc	pc		;pocet cisel
	jmp	read_again
err_r:
	mov	dx, offset ERROR4
	mov	ah,9
	int	21h
	jmp 	koniec		;koniec programu
rdnumber 	endp
;Koniec procedury konverzie cisiel 

;---------zisti sumu parnych zapornych cisel-----------
suma	proc
	xor	ax,ax
	xor	dx,dx			;na 'dh' bude suma zapornych parnych cisel(max -99)
	xor	cx,cx			;'cx' bude pocitadlo
	mov	si,offset pocet+1	;
	mov	cl,[si]			;cl <= pocet spracovavanych cisel(max. 50)
	mov	si,offset LDATA		;'si' ukazujem priamo na data
	dec	cl
loopka:
	mov	ah,[si]
	mov	bh,ah
	inc	si
	and	bh,80h			;-10 = 1111 1000 ,&, 1000 0000 
	cmp	bh,80h
	jne	preskoc
	add	dh,ah
preskoc:loop	loopka
	mov	suma_z,dh
	dec	suma_z
	not	suma_z

	ret
suma	endp
		

;---------Procedura print----------------------------
print  	proc 
	mov	pom2,1
	mov	dx,offset FILEO
	mov	thand,0
	mov	cx,0
	mov	ah,3Ch
	mov	al,0
	int	21h
	jnc	zn2
	call	koniec
zn2:	mov	thand,ax
	
	xor 	cx,cx
;	mov 	cl,pocet+1			;vypis cisla TAK ZE SA KONVERTUJE NA RETAZEC 
zn1:	mov 	bx,offset number+7		;nastavenie na koniec retazca 
	mov 	[bx],'$'			;ukoncenie vypisovania 
	dec 	bx				;ideme s5 po retazci a preto dekrementujeme
	xor 	ax,ax				;ax <= 0
	mov 	si,offset ldata
	add 	si,cx
	mov 	al,[si]
	mov	di,offset odata+3

	mov	[di],2020h
	mov	[di+2],2020h
	cmp	al,80h
	jb	convert
	dec	al
	not	al
	mov	f_min ,0FFh
	
convert:
	inc	pom
	mov 	dx,10				;samotny konvert cisla do retazca
	div 	dl
	add 	ah,30h
	mov 	[bx],ah
	dec 	bx
	mov	[di],ah
	dec	di	;-------------
	mov 	ah,00
	cmp 	ax,00
	jnz 	convert
	
	cmp	f_min,0FFh
	jne	zn3
	dec	bx
	mov	[bx],2D20h
	dec	di
	mov	[di],2D20h
	mov	f_min,0

zn3:
	dec 	bx
	dec	di	;-------------
	mov 	[bx],' '

	mov 	dx,bx

	call 	Fwrite
	mov 	ah,09h				;vypise retazca (vysledku poctu cislic)
	int 	21h
	inc 	cl
	cmp 	cl,pocet+1
	jne 	zn1

	mov	dx, offset text2
	mov	ah,09h
	int	21h

	call	print_s
	ret
print	endp	
;-------------Procedura print_s------------
;Procedura print
print_s	proc 
	xor cx,cx			;vypis cisla TAK ZE SA KONTARTUJE NA RETAZEC 
	mov bx,offset number+7		;nastavenie na koniec retazca 
	mov [bx],'$'			;ukoncenie vypisovania 
	dec bx				;ideme s5 po retazci a preto dekrementujeme
	xor ax,ax			;ax <= 0
	mov al,suma_z
conv2:	mov dx,10			;samotny konvert cisla do retazca
	div dl
	add ah,48
	mov [bx],ah
	dec bx
	mov ah,00
	cmp ax,00
	jnz conv2
	dec bx				;posun kvoli znamienku
	mov [bx],2D00h			;znamienko '-'
	mov dx,bx
	inc dx
	mov suma_dec, dx
	mov ah,09h			;vypise retazca (vysledku poctu cislic)
	int 21h

	call Fwrite_s
	ret
print_s	endp	
;------------------------------------------


;-------------Procedura Fwrite-------------
;-procedura zapise do suboru vysledny retazec usporiadanych znakov-
Fwrite	proc
	push 	ax
	push 	bx
	push 	cx
	push	dx
	xor	cx,cx
	mov	di, offset odata
	mov	ah,40h
	mov	bx,thand
	mov	cl,4
	mov	dx, di
	int 	21h
	pop	dx
	pop	cx
	pop	bx
	pop	ax
	ret
Fwrite	endp
;----------Koniec zapisu do suboru----------


;-------------Procedura Fwrite_s-------------
;-procedura zapise do suboru vyslednu sumu zapornych cisel-
Fwrite_s	proc
	push 	ax
	push 	bx
	push 	cx
	push	dx

	xor	cx,cx
	
;	mov	dx, offset NL+2
;	mov	ah,40h
;	mov	bx,thand
;	mov	cl,4
;	int 	21h
	
	mov	dx, offset text2+2
	mov	ah,40h
	mov	bx,thand
	mov	cl,23
;	mov	dx, di
	int 	21h
	
	mov	dx, suma_dec
	mov	ah,40h
	mov	bx,thand
	mov	cl,3
	int 	21h

	pop	dx
	pop	cx
	pop	bx
	pop	ax
	ret
Fwrite_s	endp
;----------Koniec zapisu do suboru----------

rd_done proc
	mov	ah,09
	mov	dx,offset text2
	int	21h
	ret
rd_done	endp


;--------Triedenie vzostupne cisel v doplnkovom kode----
;-Zotriedi 
sort	proc	
	xor	ax,ax
	xor	bx,bx
	xor	cx,cx
	xor	dx,dx
	mov	al,pc			;pc - Pocet Cisel
	mov	cl,pc
	dec	al
	dec	cl
vn2:	dec	cl
vn1:	mov	dl,pc
	mov	si,offset ldata
	add	si,cx
	mov	bx,[si]
	mov	dx,bx
	and	dx,8080h
	cmp	dx,8080h
	jz	zaporne
	cmp	dx,0
	jz	kladne
	cmp	dx,8000h
	jz	horne_zap
	cmp	dx,80h
	jz	dolne_zap

zaporne:cmp	bh,bl
	jnb	dalej
	mov	swap,bh
	mov	bh,bl
	mov	bl,swap
	jmp 	dalej
kladne: cmp	bh,bl
	jnb	dalej
	mov	swap,bh
	mov	bh,bl
	mov	bl,swap
	jmp	dalej
horne_zap:
	mov	swap,bh
	mov	bh,bl
	mov	bl,swap
dolne_zap:

dalej:	mov	[si],bl
	inc	si
	mov	[si],bh
;	sub	ldata,cl
	
	cmp	cx,0
	jnz	vn2
	mov	cl,pc
	dec	cl

	dec	ax
	cmp	ax,0
	jnz	vn2

	mov	ah,09h
	mov	dx,offset T_sort
	int	21h


	ret
sort	endp

;--------------- zmazanie obrazovky-----------------
zmazobr proc			 			
	mov ah,6			; nastavenie registrov
	mov ch,0
	mov cl,0
	mov dh,25
	mov dl,80
	mov al,0
	mov bh,07h			;farba (0-pozadie; 2-text) 
	int 10h			;zmazanie obrazovky
	mov ah,2			;nastavenie kurzoru
	mov dh,0
	mov dl,0
	mov bh,0
	int 10h			;kurzor na (0,0)
	ret
zmazobr	endp
;----




;koniec
koniec	proc
	mov	ax,4C00h	;ukocenie programu
	int	21h
koniec	endp	
	end	start
	


