arr equ 13 ; distance of the variable arr from stack top arcnt equ 12 ; distance of the variable arcnt from stack top buf equ 3 ; distance of local variable buf from stack top in_order equ 2 ; distance of local variable in_order from stack top inner equ 1 ; distance of local variable inner from stack top iteration equ 0 ; distance of local variable iteration from stack top true equ 1 false equ 0 n equ 30 ; array count local equ 4 ; number of bytes used by local variables org $1000 array_x dc.b 3,29,10,98,54,9,100,104,200,92,87,48,27,22,71 dc.b 1,62,67,83,89,101,190,187,167,134,121,20,31,34,54 org $1500 lds #$1500 ; initialize stack pointer ldx #array_x pshx ldaa #n psha jsr bubble leas 3,sp ; deallocate space used by outgoing parameters swi ; break to D-Bug12 monitor bubble pshd pshy pshx leas -local,sp ; allocate space for local variables ldaa arcnt,sp ; compute the number of iterations to be performed deca ; " staa iteration,sp ; " ploop ldaa #true ; set array in_order flag to true before any iteration staa in_order,sp ; " ldx arr,sp ; use index register X as the array pointer ldaa iteration,sp ; initialize inner loop count for each iteration staa inner,sp ; " cloop ldaa 0,x ; compare two adjacent elements cmpa 1,x ; " bls looptest ; the following five instructions swap the two adjacent elements staa buf,sp ; swap two adjacent elements ldaa 1,x ; " staa 0,x ; " ldaa buf,sp ; " staa 1,x ; " ldaa #false ; reset the in-order flag staa in_order,sp ; " looptest inx dec inner,sp bne cloop tst in_order,sp ; test array in_order flag after each iteration bne done dec iteration,sp bne ploop ; the following instruction deallocates local variables done leas local,sp ; deallocate local variables pulx puly puld rts end