TITLE   BUBBLE SORT
LIST F,W
LINES 70
ORG H#0


; putc "B"
                PUTC    R2,R0,H#0002
; putc "E"
                PUTC    R2,R0,H#0005
; putc "F"
                PUTC    R2,R0,H#0006
; putc "O"
                PUTC    R2,R0,H#000F
; putc "R"
                PUTC    R2,R0,H#0012
; putc "E"
                PUTC    R2,R0,H#0005
; putc " "
                PUTC    R2,R0,H#0020
; putc "S"
                PUTC    R2,R0,H#0013
; putc "O"
                PUTC    R2,R0,H#000F
; putc "R"
                PUTC    R2,R0,H#0012
; putc "T"
                PUTC    R2,R0,H#0014
; putc return
                PUTC    R2,R0,H#0080

;init array to 0
                ADDI    R5,R0,H#0000
;set v[0] = 111
                ADDI    R2,R0,H#006F
                SW      R2,R0,H#0001
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
                ADDI    R5,R5,H#0001
;set v[1] = 102
                ADDI    R2,R0,H#0066
                SW      R2,R0,H#0002
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
                ADDI    R5,R5,H#0001
;set v[2] = 14
                ADDI    R2,R0,H#000E
                SW      R2,R0,H#0003
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
                ADDI    R5,R5,H#0001
;set v[3] = -13
                ADDI    R2,R0,H#FFF3
                SW      R2,R0,H#0004
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
                ADDI    R5,R5,H#0001
;set v[4] = 17
                ADDI    R2,R0,H#0011
                SW      R2,R0,H#0005
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
                ADDI    R5,R5,H#0001
;set v[5] = 6
                ADDI    R2,R0,H#0006
                SW      R2,R0,H#0006
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
                ADDI    R5,R5,H#0001
;set v[6] = 5
                ADDI    R2,R0,H#0005
                SW      R2,R0,H#0007
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
                ADDI    R5,R5,H#0001
;set v[7] = 0
                ADDI    R2,R0,H#0000
                SW      R2,R0,H#0008
; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
                ADDI    R5,R5,H#0001
                ADDI    R4,R0,H#0001                
;
; Set K=8
;              ADDI    R6,R0,H#0008

;Preserve the existing values of the registers
;SORT:          ADDI    R29,R29,H#FFE0
;               ADDI    R30,R30,H#FFFC
;               SW      R15,R29,H#0000
;               SW      R16,R29,H#0004
;               SW      R17,R29,H#0008
;               SW      R18,R29,H#000C
;               SW      R19,R29,H#0010
;               SW      R20,R29,H#0014
;               SW      R24,R29,H#0018
;               SW      R25,R29,H#001C
;               SW      R31,R30,H#0000
; Load value in R4 into R18(v[k])
                ADDI    R18,R4,H#0000
; Load value in R5 into R20(v[k+1])
                ADDI    R20,R5,H#0000
; Init R19 to 0
                ADDI    R19,R0,H#0000
; 
FOR1TST:        SLT     R8,R19,R20
		NOP	
                BEQZ    R8,EXIT1%:
                ADDI    R17,R19,H#FFFF
FOR2TST:        SLTI    R8,R17,H#0000
		NOP
                BNEZ    R8,EXIT2%:
;               SLL     R15,R17
                ADD     R16,R18,R17
                LW      R24,R16,H#0000
                LW      R25,R16,H#0001
                NOP
                SLT     R8,R25,R24
		NOP	
                BEQZ    R8,EXIT2%:
                ADDI    R4,R18,H#0000
                ADDI    R5,R17,H#0000
                JAL     SWAP%:
                ADDI    R17,R17,H#FFFF
                J     FOR2TST%:
EXIT2:          ADDI    R19,R19,H#0001
                J     FOR1TST%:

;SWAP:          ADDI    R30,R30,H#FFF4
;               SW      R2,R30,H#0004
SWAP:           SW      R15,R0,H#0010
                SW      R16,R0,H#0011
		    SW      R31,R0,H#0012
;               SLL     R2,R5
                ADDI    R2,R5,H#0000
                ADD     R2,R4,R2
                LW      R15,R2,H#0000
                LW      R16,R2,H#0001
                NOP
                SW      R16,R2,H#0000
                SW      R15,R2,H#0001
;               LW      R2,R30,H#0000
                LW      R15,R0,H#0010
                LW      R16,R0,H#0011
		    LW      R31,R0,H#0012
;               ADDI    R30,R30,H#000C
                JR      R31

EXIT1:               LW  R2,R0,H#0001
                     LW  R5,R0,H#0002
                     LW  R8,R0,H#0003
                     LW  R17,R0,H#0004
                     LW  R19,R0,H#0005
                     LW  R20,R0,H#0006
                     LW  R24,R0,H#0007
                     LW  R25,R0,H#0008

; putc return
                PUTC    R2,R0,H#0080
; putc "A"
                PUTC    R2,R0,H#0001
; putc "F"
                PUTC    R2,R0,H#0006
; putc "T"
                PUTC    R2,R0,H#0014
; putc "E"
                PUTC    R2,R0,H#0005
; putc "R"
                PUTC    R2,R0,H#0012
; putc " "
                PUTC    R2,R0,H#0020
; putc "S"
                PUTC    R2,R0,H#0013
; putc "O"
                PUTC    R2,R0,H#000F
; putc "R"
                PUTC    R2,R0,H#0012
; putc "T"
                PUTC    R2,R0,H#0014
; putc return
                PUTC    R2,R0,H#0080


; putc reg2
                PUTC    R0,R2,H#0000
                PUTC    R1,R2,H#0000
; putc ,
                PUTC    R2,R0,H#002C
; putc reg5
                PUTC    R0,R5,H#0000
                PUTC    R1,R5,H#0000
; putc ,
                PUTC    R2,R0,H#002C
; putc reg8
                PUTC    R0,R8,H#0000
                PUTC    R1,R8,H#0000
; putc ,
                PUTC    R2,R0,H#002C
; putc reg17
                PUTC    R0,R17,H#0000
                PUTC    R1,R17,H#0000
; putc ,
                PUTC    R2,R0,H#002C
; putc reg19
                PUTC    R0,R19,H#0000
                PUTC    R1,R19,H#0000
; putc ,
                PUTC    R2,R0,H#002C
; putc reg20
                PUTC    R0,R20,H#0000
                PUTC    R1,R20,H#0000
; putc ,
                PUTC    R2,R0,H#002C
; putc reg24
                PUTC    R0,R24,H#0000
                PUTC    R1,R24,H#0000
; putc ,
                PUTC    R2,R0,H#002C
; putc reg25
                PUTC    R0,R25,H#0000
                PUTC    R1,R25,H#0000


FINALE:	     J       FINALE%:

;                LW      R15,R29,H#0000
;                LW      R16,R29,H#0004
;                LW      R17,R29,H#0008
;                LW      R18,R29,H#000C
;                LW      R19,R29,H#0010
;                LW      R20,R29,H#0014
;                LW      R24,R29,H#0018
;                LW      R25,R29,H#001C
;                LW      R31,R30,H#0000
;                ADDI    R29,R29,H#0020
;                ADDI    R30,R30,H#0004
;                JR      R31

END
