SlideShare a Scribd company logo
 
     
2016	
  
Lab	
  1	
  
Bubble  Sort  Algorithm  implemented  in  Assembly  
Language	
  
学生姓名:ARIEL	
  TONATIUH	
  ESPINDOLA	
  PIZANO	
  
Microcontroller Units Tongji University 2	
  
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.  
  
  
  
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.  
  
12   45   22   25   56   38   10   05  
  
Now  45  is  greater  than  22  then  swap,  advance  one  step  and  compare  again.  
  
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  (that’s  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.  
  
element	
   neighbor	
  
element	
   neighbor	
  
element	
   neighbor	
  
Microcontroller Units Tongji University 3	
  
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	
  
A[i]	
  >	
  A[i+1]	
  i=i+1	
  
no	
  
yes	
  
swap	
  
K	
  <	
  N	
  
i	
  <	
  N-­‐1	
  
yes	
  
no	
  
yes	
  
K=K+1	
  
done	
  
no	
  
Microcontroller Units Tongji University 4	
  
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: %dn",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 5	
  
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 6	
  
;* 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
  
  
  

More Related Content

What's hot

Coal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly ProgrammingCoal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly ProgrammingMuhammad Taqi Hassan Bukhari
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisRadhika Talaviya
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipeliningMazin Alwaaly
 
8051 experiments1
8051 experiments18051 experiments1
8051 experiments1tt_aljobory
 
Microprocessor 80386
Microprocessor 80386Microprocessor 80386
Microprocessor 80386yash sawarkar
 
Lab 6 microcontroller
Lab 6 microcontrollerLab 6 microcontroller
Lab 6 microcontrollermkazree
 
Micro Processor Mini Project,Electronic Quiz Table
Micro Processor Mini Project,Electronic Quiz TableMicro Processor Mini Project,Electronic Quiz Table
Micro Processor Mini Project,Electronic Quiz TableSubhashini Sundaram
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentationRatul Hasan
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic ProgrammingSahil Kumar
 
Assembly Language Lecture 5
Assembly Language Lecture 5Assembly Language Lecture 5
Assembly Language Lecture 5Motaz Saad
 
memory reference instruction
memory reference instructionmemory reference instruction
memory reference instructionDeepikaT13
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFAkunj desai
 
8086 labmanual
8086 labmanual8086 labmanual
8086 labmanualiravi9
 
NFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA Converted to DFA , Minimization of DFA , Transition DiagramNFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA Converted to DFA , Minimization of DFA , Transition DiagramAbdullah Jan
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannualmaamir farooq
 

What's hot (20)

Coal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly ProgrammingCoal 1 - introduction to assembly programming in Assembly Programming
Coal 1 - introduction to assembly programming in Assembly Programming
 
Part Picking Robot
Part Picking RobotPart Picking Robot
Part Picking Robot
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
Computer architecture pipelining
Computer architecture pipeliningComputer architecture pipelining
Computer architecture pipelining
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
8051 experiments1
8051 experiments18051 experiments1
8051 experiments1
 
Microprocessor 80386
Microprocessor 80386Microprocessor 80386
Microprocessor 80386
 
Flags registers
Flags registersFlags registers
Flags registers
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial Intelligence
 
Lab 6 microcontroller
Lab 6 microcontrollerLab 6 microcontroller
Lab 6 microcontroller
 
Micro Processor Mini Project,Electronic Quiz Table
Micro Processor Mini Project,Electronic Quiz TableMicro Processor Mini Project,Electronic Quiz Table
Micro Processor Mini Project,Electronic Quiz Table
 
Radix sort presentation
Radix sort presentationRadix sort presentation
Radix sort presentation
 
Compiler construction
Compiler constructionCompiler construction
Compiler construction
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Assembly Language Lecture 5
Assembly Language Lecture 5Assembly Language Lecture 5
Assembly Language Lecture 5
 
memory reference instruction
memory reference instructionmemory reference instruction
memory reference instruction
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
8086 labmanual
8086 labmanual8086 labmanual
8086 labmanual
 
NFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA Converted to DFA , Minimization of DFA , Transition DiagramNFA Converted to DFA , Minimization of DFA , Transition Diagram
NFA Converted to DFA , Minimization of DFA , Transition Diagram
 
Selection sort lab mannual
Selection sort lab mannualSelection sort lab mannual
Selection sort lab mannual
 

Similar to Bubble Sort algorithm in Assembly Language

Numerical Methods
Numerical MethodsNumerical Methods
Numerical MethodsESUG
 
Array sorting
Array sortingArray sorting
Array sortingALI RAZA
 
Illustrative Introductory Neural Networks
Illustrative Introductory Neural NetworksIllustrative Introductory Neural Networks
Illustrative Introductory Neural NetworksYasutoTamura1
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesNirmalavenkatachalam
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptxShimoFcis
 
Machine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data DemystifiedMachine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data DemystifiedOmid Vahdaty
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithmsNico Ludwig
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.pptSushantRaj25
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 

Similar to Bubble Sort algorithm in Assembly Language (20)

Numerical Methods
Numerical MethodsNumerical Methods
Numerical Methods
 
Ada notes
Ada notesAda notes
Ada notes
 
Array sorting
Array sortingArray sorting
Array sorting
 
Illustrative Introductory Neural Networks
Illustrative Introductory Neural NetworksIllustrative Introductory Neural Networks
Illustrative Introductory Neural Networks
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Backpropagation - Elisa Sayrol - UPC Barcelona 2018
Backpropagation - Elisa Sayrol - UPC Barcelona 2018Backpropagation - Elisa Sayrol - UPC Barcelona 2018
Backpropagation - Elisa Sayrol - UPC Barcelona 2018
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptx
 
04 Multi-layer Feedforward Networks
04 Multi-layer Feedforward Networks04 Multi-layer Feedforward Networks
04 Multi-layer Feedforward Networks
 
Machine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data DemystifiedMachine Learning Essentials Demystified part2 | Big Data Demystified
Machine Learning Essentials Demystified part2 | Big Data Demystified
 
(1) collections algorithms
(1) collections algorithms(1) collections algorithms
(1) collections algorithms
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 

Recently uploaded

Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industriesMuhammadTufail242431
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Dr.Costas Sachpazis
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdfKamal Acharya
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionjeevanprasad8
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdfAhmedHussein950959
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdfKamal Acharya
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptssuser9bd3ba
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxMd. Shahidul Islam Prodhan
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxViniHema
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxCenterEnamel
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfKamal Acharya
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturingssuser0811ec
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdfKamal Acharya
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Aryaabh.arya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Electivekarthi keyan
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfAbrahamGadissa
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfKamal Acharya
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientistgettygaming1
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-IVigneshvaranMech
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Krakówbim.edu.pl
 

Recently uploaded (20)

Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdfA CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
A CASE STUDY ON ONLINE TICKET BOOKING SYSTEM PROJECT.pdf
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 

Bubble Sort algorithm in Assembly Language

  • 1.       2016   Lab  1   Bubble  Sort  Algorithm  implemented  in  Assembly   Language   学生姓名:ARIEL  TONATIUH  ESPINDOLA  PIZANO  
  • 2. Microcontroller Units Tongji University 2   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.         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.     12   45   22   25   56   38   10   05     Now  45  is  greater  than  22  then  swap,  advance  one  step  and  compare  again.     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  (that’s  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.     element   neighbor   element   neighbor   element   neighbor  
  • 3. Microcontroller Units Tongji University 3   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   A[i]  >  A[i+1]  i=i+1   no   yes   swap   K  <  N   i  <  N-­‐1   yes   no   yes   K=K+1   done   no  
  • 4. Microcontroller Units Tongji University 4   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: %dn",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:
  • 5. Microcontroller Units Tongji University 5   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 ;**************************************************************
  • 6. Microcontroller Units Tongji University 6   ;* 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