ASSEMBLY 8086 BUBBLE SORT

🙌 In this blog, we will code the program that sorts numbers from smallest to largest in assembly language. We will use the Bubble Sort algorithm for this.

--

We will use the EMU8086 emulator. So let’s start.🚀

Problem Statement : Write a program in assembly language that places 10 random unsigned numbers of one byte in an area you specify in the memory, sorts them from smallest to largest, and stores them in the same memory area.

WHAT IS BUBBLE SORT ALGORÄ°THM ?

Photo by Alex Alvarez on Unsplash

In this comparison-based algorithm, each element in the list is compared with the next element. If the value of the first element is greater than the value of the second element, swap is made. And this process continues until the whole list is sorted. Look at the example. 👇

Bubble Sort Algorithm Example

If the Bubble Sort order is written manually according to the values I have given below, the following procedures are followed.

unsorted → [55h,32h,54h,11h,47h,33h,75h,01h,5Fh,23h]
step1 → [32h,54h,11h,47h,33h,55h,01h,5Fh,23h,75h]
step2 → [32h,11h,47h,33h,54h,01h,55h,23h,5Fh,75h]
step3 → [11h,32h,33h,47h,01h,54h,23h,55h,5Fh,75h]
step4 → [11h,32h,33h,01h,47h,23h,54h,55h,5Fh,75h]
step5 → [11h,32h,01h,33h,23h,47h,54h,55h,5Fh,75h]
step6 → [11h,01h,32h,23h,33h,47h,54h,55h,5Fh,75h]
step7 → [01h,11h,23h,32h,33h,47h,54h,55h,5Fh,75h]
sorted → [01h,11h,23h,32h,33h,47h,54h,55h,5Fh,75h]

EMU8086 ASSEMBLY CODE

Numbers sorted by Bubble Sort!

When we run the above program in the emulator, the numbers are sorted in ascending order in the same memory regions given.

  MOV BX,0200H
;Let's insert the 8-bit unsigned 10 numbers we want from 0700:0200.
MOV BYTE PTR [bx],55h
MOV BYTE PTR [bx+1],32H
MOV BYTE PTR [bx+2],54H
MOV BYTE PTR [bx+3],11H
MOV BYTE PTR [bx+4],47H
MOV BYTE PTR [bx+5],33H
MOV BYTE PTR [bx+6],75H
MOV BYTE PTR [bx+7],01H
MOV BYTE PTR [bx+8],5FH
MOV BYTE PTR [bx+9],23H


org 100h ;offset addresses of commands


MOV BX,0200H
MOV SI,0 ;The register to be used in the inner loop iteration
MOV DI,0 ;The register to be used in outer loop iteration

innerloop:

MOV DL,[BX+SI] ;register to hold the smallest number
MOV AL,[BX+SI+1] ;Gets the corresponding byte at base address 0700:0200 (indexed with SI+1)

CMP AL,DL ;If the value of the al register is greater than or equal to dl,
JAE @IF ;if al >= dl (cf=0) go to if block
JB @ELSE ;if al < dl (cf=1) go to else block

@IF: ;if block
MOV [BX+SI],DL ;If al is greater than dl, there is no need to swap
MOV [BX+SI+1],AL ; because they are already sorted among themselves.
INC SI ;For iteration, increase the register by 1 (si=si+1)

CMP SI,9 ;Is si equal to 9?
JNZ innerloop ;(zf=0) enter the inner loop

LOOP outerloop ;(zf=1) enter the outerloop

@ELSE: ;else block
MOV [BX+SI],AL ;values are swapped in the same regions in memory
MOV [BX+SI+1],DL
MOV CL,AL ;swap for registers
MOV AL,DL
MOV DL,CL
INC SI ;increment register by 1 for iteration(si=si+1)

CMP SI,9 ;Is si equal to 9?
JNZ innerloop ;(zf=0) enter the inner loop

LOOP outerloop ;(zf=1) enter the outerloop


outerloop:
INC DI ;
MOV SI,0 ;si is reset to be able to reorder
CMP DI,9 ;has the loop reached the end of its step (di=9)
JNZ innerloop ;If di is not equal to 9, enter the inner loop and sort again

HLT ;STOP if di=9

OUTPUT:

[200h] →01h,

[201h] →11h,

[202h] →23h,

[203h] →32h,

[204h] →33h,

[205h] →47h,

[206h] →54h,

[207h] →55h

[208h] →5Fh,

[209h] →75h

Sign up to discover human stories that deepen your understanding of the world.

--

--

Evin AYDÄ°N ÃœLGEN
Evin AYDÄ°N ÃœLGEN

Written by Evin AYDÄ°N ÃœLGEN

Bursa Teknik University / Computer Engineering

No responses yet

Write a response