Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Embedded Systems

Sort numbers in ascending order in an array | 8086 Microprocessor



Here, we are going to write an assembly language program in 8086 microprocessor to sort numbers in ascending order in an array?
Submitted by Ayush Sharma, on November 08, 2018

Problem: Write a program in 8086 microprocessor to sort numbers in ascending order in an array of n numbers, where size n is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501.

Algorithm:

  1. Load data from offset 500 to register CL (for count).
  2. Travel from starting memory location to last and compare two numbers if first number is greater than second number then swap them.
  3. First pass fix the position for last number.
  4. Decrease the count by 1.
  5. Again travel from starting memory location to (last-1, by help of count) and compare two numbers if first number is greater than second number then swap them.
  6. Second pass fix the position for last two numbers.
  7. Repeated.

Program:

ADDRESSMNEMONICSCOMMENTS
400MOV SI, 500SI ← 500
403MOV CL, [SI]CL ← [SI]
405DEC CLCL ← CL-1
407MOV SI, 500SI ← 500
40AMOV CH, [SI]CH ← [SI]
40CDEC CHCH ← CH-1
40EINC SISI ← SI+1
40FMOV AL, [SI]AL ← [SI]
411INC SISI ← SI+1
412CMP AL, [SI]AL-[SI]
414JC 41CJUMP TO 41C IF CY=1
416XCHG AL, [SI]SWAP AL AND [SI]
418DEC SISI ← SI-1
419XCHG AL, [SI]SWAP AL AND [SI]
41BINC SISI ← SI+1
41CDEC CHCH ← CH-1
41EJNZ 40FJUMP TO 40F IF ZF=0
420DEC CLCL ← CL-1
422JNZ 407JUMP TO 407 IF ZF=0
424HLTEND

Explanation:

  1. MOV SI, 500: set the value of SI to 500.
  2. MOV CL, [SI]: load data from offset SI to register CL.
  3. DEC CL: decrease value of register CL BY 1.
  4. MOV SI, 500: set the value of SI to 500.
  5. MOV CH, [SI]: load data from offset SI to register CH.
  6. DEC CH: decrease value of register CH BY 1.
  7. INC SI: increase value of SI BY 1.
  8. MOV AL, [SI]: load value from offset SI to register AL.
  9. INC SI: increase value of SI BY 1.
  10. CMP AL, [SI]: compares value of register AL and [SI] (AL-[SI]).
  11. JC 41C: jump to address 41C if carry generated.
  12. XCHG AL, [SI]: exchange the contents of register AL and SI.
  13. DEC SI: decrease value of SI by 1.
  14. XCHG AL, [SI]: exchange the contents of register AL and SI.
  15. INC SI: increase value of SI by 1.
  16. DEC CH: decrease value of register CH by 1.
  17. JNZ 40F: jump to address 40F if zero flat reset.
  18. DEC CL: decrease value of register CL by 1.
  19. JNZ 407: jump to address 407 if zero flat reset.
  20. HLT: stop.





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates



© https://www.includehelp.com (2015-2018), Some rights reserved.