Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
;
; 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:
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
done: feed_watchdog
STOP
;BRA mainLoop
;**************************************************************
;**************************************************************
;* Interrupt Vectors *
;*******************************ExampleVar*******************************
ORG $FFFA
DC.W spurious ;
DC.W spurious ; SWI
DC.W _Startup ; Reset