Professional Documents
Culture Documents
20L412 - MPMC Ex No 4
20L412 - MPMC Ex No 4
20L412 - MPMC Ex No 4
MICROCONTROLLERS LABORATORY
(AUTONOMOUS INSTITUTION)
PREPARED BY,
SAKTHIVEL R
20L412
EX NO: 4 SORTING THE GIVEN NUMBER USING
AIM:
SOFTWARE REQUIREMENT:
THEORY:
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)
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
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
MOV B, @R1
LOOP: JNC L1
SJMP L2
MOV @R1, A
L2: INC R1
H: SJMP H
OUTER: INC R0
MOV A, R0
MOV R1, A
INC R1
MOV A, R3
MOV R2, A
SJMP COUNT
END
Result:
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
COUNT:MOV A, @R0
MOV B, @R1
LOOP: JC L1
SJMP L2
MOV @R1, A
L2: INC R1
H: SJMP H
OUTER: INC R0
MOV A, R0
MOV R1, A
INC R1
MOV A, R3
MOV R2, A
SJMP COUNT
END
Result:
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.