include "p16c5x.inc"
include "P16C5X.INC"

startup         code
                org 3

		CLRF FSR      ;clearing FSR
		CLRF INDF     ;clearing INDF
		CLRW          ;clearing W

		MOVLW 0X05    ;accepting the value of N (NO. of inputs)
		MOVWF 0X08    ;08H holds the value of N
		MOVWF 0X09    ;moving the same value to 09H too

		MOVLW 0X0A    ;Starting Address is 0x0A
                MOVWF FSR     ;moving the value 0A to FSR

;VALUES ARE STORED IN MEMORY 0AH,0BH,0CH,0DH,0EH
		
                MOVLW 0XF3   ;moving First Number F3H to W
		MOVWF INDF   ;moving the First Number to 0AH
		INCF FSR,1   ;incrementing FSR (FSR=0B)
		
		DECFSZ 08H,1 ;decrement 08H and checking whether equals 0
FALSE	        GOTO NXT     ;if zero skips this line/ else will execute this line
TRUE	        GOTO SORT    ;if zero skips the above line and executes from here

NXT		MOVLW 0X10   ;moving Second Number 10H to W
		MOVWF INDF   ;moving the second number to 0BH
		INCF FSR,1   ;incrementing FSR (FSR=0C)
		
		DECFSZ 08H,1 ;decrement 08H and checking whether equals 0
		GOTO NXT1    ;if zero skips this line/ else will execute this line
		GOTO SORT    ;if zero skips the above line and executes from here

NXT1	        MOVLW 0XEF   ;moving the Third Number EFH to W
		MOVWF INDF   ;moving the third number to 0CH
		INCF FSR,1   ;incrementing FSR(FSR=0D)
		
		DECFSZ 08H,1 ;decrement 08H and checking whether equals 0
		GOTO NXT2    ;if zero skips this line/else will execute this line
		GOTO SORT    ;if zero skips the above line and executes from here

NXT2	        MOVLW 0X45   ;moving the Fourth Number 45H to W
		MOVWF INDF   ;moving the Fourth Number to 0DH
		INCF FSR,1   ;incrementing FSR(FSR=0E)
		
		DECFSZ 08H,1 ;decrement 08H and checking whether equals 0
		GOTO NXT3    ;if zero skips this line/ else will execute this line
		GOTO SORT    ;if zero skips the above line and executes from here
		
NXT3	        MOVLW 0X02   ;moving the last(fifth number) 02H to W
		MOVWF INDF   ;moving the last(fifth number) 02H to OEH
		INCF FSR,1   ;increment FSR(FSR=0F)
		
		DECFSZ 08H,1 ;decrement 08H and checking whether equals 0
		GOTO NXT3    ;if zero skips this line/ else will execute this line
		GOTO SORT    ;if zero skips the above line and executes from here
		
SORT
		CLRF FSR     ;clearing FSR
		CLRF INDF    ;clearing INDF
		CLRW         ;clearing W

		MOVLW 0X04   ;setting a counter of value 04H
		MOVWF 0X09   ;moving the value to 09H
LOOP
		MOVF 0X09,0  ;moving the value of 09H to W
		MOVWF 0X08   ;moving the value of W to 08H

		MOVLW 0X0A   ;setting the starting address as 0A
		MOVWF FSR    ;keeping the 0A on FSR

;starting comparison
		
CONV	        MOVF INDF,0  ;moving the value stored in 0A to W
		INCF FSR,1   ;incrementing FSR(FSR=0B)
		SUBWF INDF,0 ;subtracting W from value stored in 0B
		BTFSC 03H,0  ;checking the carry flag of STATUS
	
RFALSE	        GOTO SKIP    ;if cleared skips this line. Otherwise execute this line
RTRUE	        MOVF INDF,0  ;if cleared execute this line. Moving content of 0B to W
		MOVWF 0X18   ;moving the value stored in W to 18H(temporary location)
		DECF FSR,1   ;decrement FSR(FSR=0A)
                MOVF INDF,0  ;moving the content of 0A to W
		INCF FSR,1   ;incrementing FSR(FSR=0B)
		MOVWF INDF   ;moving the value of W to 0B
		MOVF 0X18,0  ;moving the value of 18H to W
		DECF FSR,1   ;decrement FSR(0A)		
		MOVWF INDF   ;moving the value of W to 0A
		INCF FSR,1   ;increment FSR(FSR=0B)
SKIP		
		DECFSZ 0X08,1;decrement the value stored in 08H & checks whether its zero
		GOTO CONV    ;if zero skips this line. Else executes this line
		DECFSZ 0X09,1;decrement the value stored in 09H & checks whether its zero
		GOTO LOOP    ;if zero skips this line. Else executes this line          		

;DISPLAYING THE SORTED CONTENTS THROUGH PORTB

		CLRF FSR     ;clearing FSR
		CLRF INDF    ;clearing INDF
		CLRW         ;clearing W
		
		TRIS PORTB   ;setting the PortB in output mode
		
INFI	        MOVLW 0X05   ;moving a value 05 to W
		MOVWF 0X08   ;08H will store the value (as counter)

		MOVLW 0X0A   ;moving starting address 0AH to W
		MOVWF FSR    ;setting FSR with the starting address

DISP
		MOVF INDF,0  ;moving the value of 0AH to W
		MOVWF PORTB  ;outputting  the content of W through PortB
		INCF FSR,1   ;incrementing FSR(FSR=0BH)
		DECFSZ 0X08,1;decrementing the value stored in 08H 		
		GOTO DISP    ;if zero skips this line. Else executes this line
		GOTO INFI    ;if zero executes this line


		END          ; directive 'end of program'
