Thursday, March 13, 2014

Bubble Sort in Assembly Language

Here's a homework assignment where I had to implement a bubble sort in Assembly Language. My professor gave us specific values to put into the first 8 data memory locations, then we had to sort those values and keep them all in those first 8 spaces in memory.
I love this code. Very simple, clean and best of all, it works!

;------------------------
;FILE: main.asm
;DESC HW9
;Date: 3-10-2014
;Device: PIC18F1220
;-----------------------

                list p=18F1220
                radix hex
                config WDT=OFF, LVP=OFF

#include P18F1220.INC

I equ 0x50                                    ;make I register (somewhere out of the way)
INEXT equ 0x52                                ;make INEXT register (somewhere out of the way)
TEMP equ 0x54                                 ;make TEMP register (somewhere out of the way)

                MOVLW 0X00                    ;put 0 into WREG
                MOVWF FSR0L                   ;inititalize FSR0L to 0
                MOVWF INDF0                   ;initialize INDF0 to 0
                MOVWF I                       ;initialize I to 0
                MOVWF INEXT                   ;initialize INEXT to 0        
               

                                                                                                ;initialize locations 0x00-0x07 with numbers from assignment description
                MOVLW d'10'    
                MOVWF INDF0                    ;put decimal 10 into location 0
               
                INCF FSR0L                     ;FRS0=1. Location of INDF0=1
                MOVLW d'16'    
                MOVWF INDF0                    ;put decimal 16 into location 1

                INCF FSR0L                     ;FRS0=2. Location of INDF0=2
                MOVLW d'1'
                MOVWF INDF0                    ;put decimal 1 into location 2

                INCF FSR0L                     ;FRS0=3. Location of INDF0=3
                MOVLW d'5'
                MOVWF INDF0                    ;put decimal 5 into location 3

                INCF FSR0L                     ;FRS0=4. Location of INDF0=4
                MOVLW d'15'
                MOVWF INDF0                    ;put decimal 15 into location 4

                INCF FSR0L                     ;FRS0=5. Location of INDF0=5
                MOVLW d'11'
                MOVWF INDF0                    ;put decimal 11 into location 5

                INCF FSR0L                     ;FRS0=6. Location of INDF0=6
                MOVLW d'2'
                MOVWF INDF0                    ;put decimal 2 into location 6

                INCF FSR0L                     ;FRS0=7. Location of INDF0=7
                MOVLW d'6'
                MOVWF INDF0                    ;put decimal 6 into location 7

mainloop:

                MOVLW 0X01
                MOVWF FSR0L                    ;FSR0L is set to 1 so that bubble                                                ;loop will start at location 0
                BRA bubble

endloop:
                BRA endloop                    ;will probably never be reached...

switch:                                        ;will be accessed if two numbers                                                  ;need to be switched int he array
                MOVFF I, TEMP                  ;move contents of I into TEMP register
                MOVFF INEXT, I                 ;move contents of INEXT into I
                MOVFF TEMP, INEXT              ;move contents of TEMP into INEXT
                                                                                                                                 ;so now I is INEXT and INEXT is I
                DECF FSR0L                     ;go back one space in memory
                MOVFF I, INDF0                 ;put I into location of FSR0
                INCF FSR0L                     ;go to next location
                MOVFF INEXT, INDF0             ;put INEXT into next location of FSR0
                INCF FSR0L         

                MOVLW d'7'
                SUBWF FSR0L,W                  ;subtract 7 from FSR0L and store in WREG
                BNN mainloop                   ;if FSR0L>7, start new pass 
                                               ;(branch to mainloop

bubble:
                DECF FSR0L                     ;take FSR0 back one spot
                MOVFF INDF0, I                 ;move 10 into I register
                INCF FSR0L                     ;FSR0L is now 1. So looking at                                                    ;location 1 where 16 sits
                MOVFF INDF0, INEXT             ;move 16 into INEXT register
                                                                                                                                 ;compare I and INEXT
                MOVF I, W                      ;put value of I into WREG
                SUBWF INEXT, W                 ;subtract WREG(I) from INEXT
                BN switch                      ;if I>INEXT, branch to switch loop
                INCF FSR0L                     ;increment FSR0 so that the next                                                  ;loop will be looking at the next                                                ;location
                BRA bubble                     

                end

No comments:

Post a Comment