You are on page 1of 6

2016

Lab 1
Bubble Sort Algorithm implemented in Assembly
Language
:ARIEL TONATIUH ESPINDOLA PIZANO
Bubble Sort Algorithm implemented in Assembly Language


Objective

The aim of this practice is to get familiar with the developing tool CodeWarrior, the directory
structure and tools, creating a project for a Sorting Algorithm and debugging it.

Project features
- Device MC9S08AW60
- Connections full chip simulator
- Absolutely assembly

Algorithm

The principle is to compare each element to its immediate neighbor moving from the left to
the right.

element neighbor

12 45 22 25 56 38 10 05

If the element is greater than neighbor then SWAP, otherwise go ahead to the next element.

Therefore, 12 is not greater than 45 then go ahead, advance one step and compare again.

element neighbor

12 45 22 25 56 38 10 05

Now 45 is greater than 22 then swap, advance one step and compare again.

element neighbor

12 22 45 25 56 38 10 05

It is applied the same logic in every iteration. Finally, in this case the number 56 will be floated
to the right at the end of the array (thats why bubble).

12 22 25 45 38 10 05 56
" $ % & ' ( )

Where , is each element of the sequence and 0 1
Now is sorted just one single number of the array. The last steps should be repeated for every
single element in the sequence, that means N times where N would be the length of the such
array.

Microcontroller Units Tongji University 2


Algorithm step by step

1. Get the length of the sequence
2. Take the first element and compare it with the immediately neighbor to the right:
, > ,4$
- If true: swap and increment by one.
- If false: increment by one.

3. Repeat step 2, N-1 times.

Pseudo code

While k < N
For i=0 to N-1
If a[i] > a[i+1]
Then
swap()
end
k=k+1
end

Flowchart



Start



no
done K < N


K=K+1
yes

no
i < N-1


yes
no
i=i+1 A[i] > A[i+1]



yes

swap




Microcontroller Units Tongji University 3


Complexity

In each cycle the algorithm compares every couple of consecutive elements and if they are not
ordered, then swap them.

This process is repeated N times by N elements, here it is inferred a complexity of ( )
The optimization would be reached by sensing if there are no swaps to do, if so then the elements
are already sorted and break.


Implementation in C

int* sort(int *x,int N){
int i = 0,j,swaps;
while(i < N){
for (int j = 0; j < N-1; j++){
if (x[j] > x[j+1]){
swaps++;
int t = x[j];
x[j] = x[j+1];
x[j+1] = t;
}
}
i++;
}
printf("swaps: %d\n",swaps);
return x;
}

Implementation in Assembly

; Include derivative-specific definitions


INCLUDE 'derivative.inc'

;
; export symbols
;
XDEF _Startup
ABSENTRY _Startup

;
; variable/data section
;
ORG Z_RAMStart ; Insert your data definition here
Counter: DS.B 1
InnerCnt: DS.B 1
;
; code section
;
ORG ROMStart

_Startup:

Microcontroller Units Tongji University 4


LDHX #RAMEnd+1 ; initialize the stack pointer
TXS
CLI ; enable interrupts
MOV #$12,$80 ; unsorted numbers from $80 - $89
in memory
MOV #$45,$81
MOV #$22,$82
MOV #$25,$83
MOV #$56,$84
MOV #$38,$85
MOV #$10,$86
MOV #$05,$87
MOV #$12,$88
MOV #$02,$89
mainLoop:
LDA #10 ; size of the array (outer
iterations)
STA Counter
CLRA
LDHX #$0000
outerLoop: LDA #9 ; number of inner iterations
STA InnerCnt
CLRA
BSR Sort ; branch to subroutine
DBNZ Counter, outerLoop
BRA done ; end of the program

Sort:
NOP
LDX #$80
Loop:
LDA ,X ; A = X[i]
CMP $1,X ; X[i] ? X[i+1]
BLT round ; if smaller -> round again
BGT swap ; if greater, then -> SWAP
BRA round ; if not -> round again

swap: NOP
PSHA ; store t = X[i] in stack
(temp)
LDA $1,X ; X[i+1]
STA X ; X[i] = X[i+1]
PULA ; t
STA $1,X ; X[i+1] = t

round: AIX #1 ; i++; increment the


index
CLRA
DBNZ InnerCnt,Loop ; decrese counter and back to the
loop
RTS

done: feed_watchdog
STOP
;BRA mainLoop

;**************************************************************

Microcontroller Units Tongji University 5


;* spurious - Spurious Interrupt Service Routine. *
;* (unwanted interrupt) *
;**************************************************************

spurious: ; placed here so that security value


NOP ; does not change all the time.
RTI

;**************************************************************
;* Interrupt Vectors *
;*******************************ExampleVar*******************************

ORG $FFFA

DC.W spurious ;
DC.W spurious ; SWI
DC.W _Startup ; Reset

Microcontroller Units Tongji University 6

You might also like