This PIC project shows the sorting of numbers stored in RAM memory in Ascending order.To simulate circuit in this project, initially activate Mixed Mode simulator from the Schematic Editor window. Simulation can be performed by selecting Run Transient analysis from Simulation menu.
The contents of two memory locations are compared. During sorting, the contents of successive memory locations may be exchanged. The number of memory locations to be compared and the number of comparisons to be carried out are specified in the code. For each comparison, number of inputs is decremented and checks whether it became zero. Comparison continues until this count becomes zero. The sorted sequence of numbers will be stored in RAM after the required number of comparisons.
The source code written in Assembly language can be viewed from the code editor window
The program is as shown:
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)
; moving the second number to 0bhincf FSR, 1 ; incre
menting FSR (FSR =0c)
decfsz 08hine/ else will execute this line
goto sort ; if zero skips the above line and executes from here
nxt1 movlw 0xef ; moving the third number EF 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.
rtrue movf INDF, 0 ; if cleared executes 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'
The source code in the code editor window has to be compiled after making any modifications
(editing). Also the code can be debugged during simulation.