Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the Bubble Sort program from lecture notes 7, change the following two sec

ID: 3926319 • Letter: U

Question

Using the Bubble Sort program from lecture notes 7, change the following two sections to subroutines.
1. Deallocate local variables

2. Swap positions.

Bubble sort

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. 1 =in ;order, 0 = not

iteration    equ 0    ; distance of local variable iteration from stack top

true equ 1

false equ 0

n equ 30    ; array count (number of elements in the array)

local equ 4        ; number of bytes used by local variables

org $1000

array_x    db    3,29,10,98,54,9,100,104,200,92,87,48,27,22,71

db 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 ;pass array start address in stack

pshx

ldaa    #n      ;pass number of elements in stack

psha

jsr bubble

leas 3,sp       ; deallocate space used by outgoing parameters

Swi          ; break to D-Bug12 monitor

;---------------------Bubble Sort Subroutine--------------------------------;

Setup subroutine part of stack.

bubble    pshd                ; save registers (note did not save A reg)

pshy

pshx

leas        -local,sp    ; allocate space for local variables

ldaa        arcnt,sp    ; get the number of iterations to be performed

deca                   ; "

staa        iteration,sp    ; “

;Main loop. N-1 times or until no swaps

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    ; “

;Walk across and compare/switch loop

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. To zero

staa        in_order,sp    ;    "

;check if finished

looptest    inx

dec        inner,sp

bne        cloop

tst        in_order,sp    ; test if in order

bne        done

dec        iteration,sp    ; finished all iterations?

bne        ploop

; deallocate local variables

done        leas        local,sp

pulx

puly

puld

rts

end

Here is a link where you can see the bubble sort program

Explanation / Answer

A bubble sort test harness to use the subroutine

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote