20L412 - MPMC Ex No 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

19L511 MICROPROCESSORS AND

MICROCONTROLLERS LABORATORY

DEPT. OF ELECTRONICS AND COMMUNICATION ENGINEERING,

PSG COLLEGE OF TECHNOLOGY,

(AUTONOMOUS INSTITUTION)

PEELAMEDU, COIMBATORE - 641004

PREPARED BY,

SAKTHIVEL R

20L412
EX NO: 4 SORTING THE GIVEN NUMBER USING

DATE: 26/09/2021 SORT ALGORITHM

AIM:

To become familiar with the conditional and unconditional Jump


Instructions and to write an Assembly Language program to sort a given array of
numbers using the Bubble Sort Algorithm

SOFTWARE REQUIREMENT:

Keil µVision IDE.

THEORY:

Bubble sort works on the repeatedly swapping of adjacent elements until


they are not in the intended order. It is called bubble sort because the movement
of array elements is just like the movement of air bubbles in the water. Bubbles
in water rise up to the surface; similarly, the array elements in bubble sort move
to the end in each iteration.
Although it is simple to use, it is primarily used as an educational tool
because the performance of bubble sort is poor in the real world. It is not suitable
for large data sets. The average and worst-case complexity of Bubble sort
is O(n2), where n is a number of items.

 Best Case Complexity - It occurs when there is no sorting required, i.e. the
array is already sorted. The best-case time complexity of bubble sort is O(n).
 Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of bubble sort is O(n2).
 Worst Case Complexity - It occurs when the array elements are required to
be sorted in reverse order. That means suppose you have to sort the array
elements in ascending order, but its elements are in descending order. The
worst-case time complexity of bubble sort is O(n2).
Algorithm:
begin BubbleSort(list)

for all elements of list

if list[i] > list[i+1]

swap(list[i], list[i+1])

end if

end for

return list

end BubbleSort

SORTING OF NUMBERS USING THE BUBBLE SORT ALGORITHM:

Ascending order:

Algorithm:
 Initialize the Registers Rn with the pointers of Memory Locations and
initialize the registers Rn with the Inner and Outer loop Counters.
 Move the Contents of Memory Location pointed by the Registers to the
Registers Accumulator A and B.
 Compare the Accumulator A and Register B.
 If the Accumulator is greater than / lesser than / equal to the Register B,
 The values are swapped / left as such / left as such respectively.
 The Inner Loop Counter is Decremented and the pointer moves to the
next value and it is compared with the current lowest value and the
above procedure is repeated again.
 The above process is repeated until the Inner Loop Counter reaches zero.
 After this the Outer Loop Counter is decremented and decremented value
is moved to the Inner Loop Counter and the above procedure is repeated
again.
Program:

Memory Machine
Label: Mnemonic: Operands: Comments:
Address: Code:
0x0000 7830 MOV R0,#0x30 Move the
Memory
Location
pointed by
the Pointer
to the
Register R0

0x0002 7931 MOV R1,#0x31 Move the


Memory
Location
pointed by
the Pointer
to the
Register R1

0x0004 7A04 MOV R2,#0x04 Move the


intermediat
e constant
(#0x04)
which is the
inner loop
counter to
the Register
R2.

0x0006 7B04 MOV R3,#0x04 Move the


intermediat
e constant
(#0x04)
which is the
outer loop
counter to
the Register
R3.

0x0008 E6 REPEAT: MOV A,@R0 Move the


content of
the memory
location
pointed by
the Register
R0 to the
Accumulato
r A.

0x0009 87F0 MOV B(0xF0),@R1 Move the


content of
the memory
location
pointed by
the Register
R1 to the
Register B.

0x000B BSF000 CJNE A,B(0xF0),LOO Compare


P(C:000E) the
Accumulato
r A and the
Register B. If
it is not
equal jump
to the Label
LOOP.

0x000E 5002 LOOP: JNC L1(C:0012) Jump to the


address
Labeled as
L1, if the
carry is not
generated.

0x0010 8003 SJMP L2(C:0015) Jump to the


address
Labeled as
L2.

0x0012 A6F0 L1: MOV @R0,B(0xF0) Move the


contents of
the register
B to the
Memory
Location
Pointed by
the Register
R0.

0x0014 F7 MOV @R1,A Move the


contents of
the register
A to the
Memory
Location
Pointed by
the Register
R1.

0x0015 09 L2: INC R1 Increment


the register
R1.

0x0016 DAF0 DJNZ R2,REPEAT(C:0 Decrement


008) the Inner
Loop
counter R2.

0x0018 DB02 DJNZ R3,OUTER(C:0 Decrement


01C) the Outer
Loop
counter R3.

0x001A 80FE H: SJMP H(C:001A) Halting the


Program.

0x001C 08 OUTER: INC R0 Incrementin


g the
Register R0.

0x001D E8 MOV A,R0 Moving the


Content of
the Register
R0 to the
Accumulato
r A.

0x001E F9 MOV R1,A Moving the


content of
the
Accumulato
r A to the
Register R1.

0x001F 09 INC R1 Increment


the Register
R1.

0x0020 EB MOV A,R3 Moving the


Content of
the Register
R3 to the
Accumulato
r A. The
value of
Outer loop
counter is
moved to
the
Accumulato
r.

0x0021 FA MOV R2,A Moving the


content of
the
Accumulato
r A to the
Register R2.

0x0022 80E4 SJMP REPEAT(C:000 Jump to the


8) Address
with the
Label
REPEAT.

Assembly Language Program:


ORG 0000H

MOV R0, #30H

MOV R1, #31H

MOV R2, #0X04

MOV R3, #0X04


COUNT:MOV A, @R0

MOV B, @R1

CJNE A,B, LOOP

LOOP: JNC L1

SJMP L2

L1: MOV @R0,B

MOV @R1, A

L2: INC R1

DJNZ R2, COUNT

DJNZ R3, OUTER

H: SJMP H

OUTER: INC R0

MOV A, R0

MOV R1, A

INC R1

MOV A, R3

MOV R2, A

SJMP COUNT

END

Result:

Input Details: Result:


Memory Memory
Expected Obtained
Location/ Data: Location/
Output: Output:
Registers: Registers:

0x30 0x02 0x30 0x01 0x01

0x31 0x05 0x31 0x02 0x02


0x32 0x04 0x32 0x03 0x03

0x33 0x01 0x33 0x04 0x04

0x34 0x03 0x34 0x05 0x05

Program Output:

Descending order:

Algorithm:
 Initialize the Registers Rn with the pointers of Memory Locations and
initialize the registers Rn with the Inner and Outer loop Counters.
 Move the Contents of Memory Location pointed by the Registers to the
Registers Accumulator A and B.
 Compare the Accumulator A and Register B.
 If the Accumulator is lesser than / greater than / equal to the Register B,
 The values are swapped / left as such / left as such respectively.
 The Inner Loop Counter is Decremented and the pointer moves to the
next value and it is compared with the current highest value and the
above procedure is repeated again.
 The above process is repeated until the Inner Loop Counter reaches zero.
 After this the Outer Loop Counter is decremented and decremented value
is moved to the Inner Loop Counter and the above procedure is repeated
again.

Program:
Memory Machine
Label: Mnemonic: Operands: Comments:
Address: Code:
0x0000 7830 MOV R0,#0x30 Move the
Memory
Location
pointed by
the Pointer
to the
Register R0

0x0002 7931 MOV R1,#0x31 Move the


Memory
Location
pointed by
the Pointer
to the
Register R1

0x0004 7A04 MOV R2,#0x04 Move the


intermediat
e constant
(#0x04)
which is the
inner loop
counter to
the Register
R2.

0x0006 7B04 MOV R3,#0x04 Move the


intermediat
e constant
(#0x04)
which is the
outer loop
counter to
the Register
R3.

0x0008 E6 COUNT: MOV A,@R0 Move the


content of
the memory
location
pointed by
the Register
R0 to the
Accumulato
r A.

0x0009 87F0 MOV B(0xF0),@R1 Move the


content of
the memory
location
pointed by
the Register
R1 to the
Register B.

0x000B BSF000 CJNE A,B(0xF0),LOO Compare


P(C:000E) the
Accumulato
r A and the
Register B. If
it is not
equal jump
to the Label
LOOP.

0x000E 4002 LOOP: JC L1(C:0012) Jump to the


address
Labeled as
L1, if the
carry is
generated.

0x0010 8003 SJMP L2(C:0015) Jump to the


address
Labeled as
L2.

0x0012 A6F0 L1: MOV @R0,B(0xF0) Move the


contents of
the register
B to the
Memory
Location
Pointed by
the Register
R0.

0x0014 F7 MOV @R1,A Move the


contents of
the register
A to the
Memory
Location
Pointed by
the Register
R1.

0x0015 09 L2: INC R1 Increment


the register
R1.

0x0016 DAF0 DJNZ R2,COUNT Decrement


(C:0008) the Inner
Loop
counter R2.

0x0018 DB02 DJNZ R3,OUTER(C:0 Decrement


01C) the Outer
Loop
counter R3.

0x001A 80FE H: SJMP H(C:001A) Halting the


Program.

0x001C 08 OUTER: INC R0 Incrementin


g the
Register R0.
0x001D E8 MOV A,R0 Moving the
Content of
the Register
R0 to the
Accumulato
r A.

0x001E F9 MOV R1,A Moving the


content of
the
Accumulato
r A to the
Register R1.

0x001F 09 INC R1 Increment


the Register
R1.

0x0020 EB MOV A,R3 Moving the


Content of
the Register
R3 to the
Accumulato
r A. The
value of
Outer loop
counter is
moved to
the
Accumulato
r.

0x0021 FA MOV R2,A Moving the


content of
the
Accumulato
r A to the
Register R2.

0x0022 80E4 SJMP COUNT(C:0008 Jump to the


) Address
with the
Label
COUNT.
Assembly Language Program:
ORG 0000H

MOV R0, #30H

MOV R1, #31H

MOV R2, #0X04

MOV R3, #0X04

COUNT:MOV A, @R0

MOV B, @R1

CJNE A,B, LOOP

LOOP: JC L1

SJMP L2

L1: MOV @R0,B

MOV @R1, A

L2: INC R1

DJNZ R2, COUNT

DJNZ R3, OUTER

H: SJMP H

OUTER: INC R0

MOV A, R0

MOV R1, A

INC R1

MOV A, R3

MOV R2, A

SJMP COUNT

END
Result:

Input Details: Result:


Memory Memory
Expected Obtained
Location/ Data: Location/
Output: Output:
Registers: Registers:

0x30 0x02 0x30 0x05 0x05

0x31 0x05 0x31 0x04 0x04

0x32 0x01 0x32 0x03 0x03

0x33 0x03 0x33 0x02 0x02

0x34 0x04 0x34 0x01 0x01

Program Output:
INFERENCE:

 Here we have used the Bubble Sorting algorithm for sorting. If we have
more no. of numbers to be sorted then it will take lot of time because, for
each inner and outer loop iteration it has to compare one number with other
numbers.
 Here we have used the JNC and JC instructions for Ascending and
Descending order respectively. In which a carry will not be generated if the
first no. is larger than the next no. and will be generated if the first no. is
smaller than the next no.
 These jump instructions are basically based on the arithmetic operation of
Subtraction.

RESULT:

Thus we have done the Sorting of numbers for both Ascending as well as
Descending order, using the Assembly language.

You might also like