Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
191 views40 pages

Code Conversion and Sorting With 8085 Microprocessor

This document provides a comprehensive guide on implementing BCD to Binary conversions, Binary to BCD conversions, and sorting algorithms using the 8085 microprocessor. It covers the architecture of the 8085, the principles of code conversion, and detailed algorithms for sorting, particularly focusing on bubble sort. The document also includes practical examples and code snippets for both conversion processes and sorting techniques.

Uploaded by

arun.sebastian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views40 pages

Code Conversion and Sorting With 8085 Microprocessor

This document provides a comprehensive guide on implementing BCD to Binary conversions, Binary to BCD conversions, and sorting algorithms using the 8085 microprocessor. It covers the architecture of the 8085, the principles of code conversion, and detailed algorithms for sorting, particularly focusing on bubble sort. The document also includes practical examples and code snippets for both conversion processes and sorting techniques.

Uploaded by

arun.sebastian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Code Conversion and Sorting

with 8085 Microprocessor


A comprehensive guide to implementing BCD to Binary conversions, Binary
to BCD conversions, and sorting algorithms using the 8085 microprocessor
architecture.
Agenda
1 2

Introduction Code Conversion


Overview of the 8085 microprocessor architecture and its BCD to Binary and Binary to BCD conversion principles,
programming model algorithms and 8085 implementation

3 4

Sorting Techniques Bubble Sort Implementation


Fundamentals of sorting algorithms and their Detailed walkthrough of bubble sort for both ascending and
implementation in 8085 assembly descending order
The 8085 Microprocessor: Overview
Key Specifications

• 8-bit processor developed by Intel in 1976


• 74 basic instructions in instruction set
• 16-bit address bus (64KB memory addressable)
• Six 8-bit registers: B, C, D, E, H, L
• One accumulator (A) register
• 5 flag bits for condition testing

The 8085 remains popular in education and embedded systems due to its simple
Why Code Conversion?
Interface Requirements Human Readability
Digital systems often need to Humans understand decimal
convert between formats when numbers, but computers
interfacing with different process binary. Conversion
components. Input devices may allows for human-friendly
use BCD while processing display of machine data.
occurs in binary.

Mathematical Operations
Binary format is more efficient for arithmetic operations, while BCD is
better for display purposes.
What is BCD?
Binary-Coded Decimal (BCD) is a way of encoding decimal digits where each
decimal digit is represented by its own 4-bit binary sequence.

Example:

Decimal: 45BCD: 0100 0101 (4) (5)

Applications:
• Digital calculators
• Clock chips and timers
• Display interfaces
• Financial calculations
What is Binary?
Binary is the native number system used by computers, representing all values
using only two digits: 0 and 1.

Example:

Decimal: 45Binary: 00101101 (2^5 + 2^3 + 2^2 + 2^0)


= 32 + 8 + 4 + 1 = 45

Binary is the most efficient format for storage and mathematical operations in
digital systems.
BCD to Binary Conversion Overview

Input BCD Number Separate Digits


The starting point is a BCD number where each decimal digit Extract individual decimal digits from the BCD representation
is represented by 4 bits

Apply Place Values Binary Result


Multiply each digit by its appropriate decimal place value (1, Sum the weighted values to obtain the pure binary representation
10, 100, etc.)

This conversion is essential when performing mathematical operations, as the ALU in the 8085 operates on pure binary values.
Binary to BCD Conversion Overview
Binary to BCD conversion is crucial for displaying computation results to users in human-readable form.

Purpose Method Application


After processing data in binary, The conversion involves repeated Essential for digital readouts, LED
results often need to be presented in division by 10 or an equivalent displays, and any system that needs
decimal format on displays or other process to extract decimal digits from to show numeric output to users.
human interfaces. a binary value.
BCD to Binary – 8085 Algorithm
Load BCD 1
Load the BCD value into the accumulator from memory

2 Separate Digits
Use masking operations (ANI) to separate the tens and
units digits
Weight Tens Digit 3
Shift the tens digit and multiply it by 10 through a
combination of shifts and additions
4 Add Units
Add the units digit to the weighted tens digit to form
the complete binary value
Store Result 5
Store the final binary result back to memory
Sample Input/Output
BCD Input Binary Output Decimal Equivalent

12H 0CH 12

25H 19H 25

47H 2FH 47

72H 48H 72

99H 63H 99

For two-digit BCD numbers, the conversion process involves weight calculations for each digit position. For example, for 72 BCD (0111 0010)

7 × 10 + 2 = 72 (decimal) = 48H (binary)

The 8085's assembly code must implement this mathematical relationship.


BCD to Binary: Key Steps
Detailed Algorithm:

1. Load the BCD value into the accumulator (A register)


2. Make a backup copy of the BCD value in another register (B)
3. Isolate the tens digit using a masking operation (ANI F0H)
4. Shift the tens digit to the right position (RRC or RLC operations)
5. Multiply the tens digit by 10 (using shifts and additions)
6. Store this partial result in register C
7. Restore the original BCD value from register B
8. Isolate the units digit using ANI 0FH
9. Add the weighted tens digit to the units digit
10. Store the final binary result in memory
BCD to Binary: Example 8085 Code Start
; BCD to Binary Conversion; Input: BCD value at memory location 201FH; Output: Binary value at memory
location 2020H LDA 201FH ; Load BCD value into accumulator MOV B, A ; Copy to B
register for safekeeping ANI F0H ; Mask to isolate tens digit RRC ; Rotate
right RRC ; Rotate right RRC ; Rotate right RRC ;
Rotate right (tens digit now in proper position)

This segment initializes the conversion process by loading the BCD value and isolating the tens digit through masking and bit
rotation operations.
BCD to Binary: Continue Example Code
; Continuing from previous code segment MOV D, A ; Save tens digit in D MOV A, D ;
Move tens digit back to A RLC ; Multiply by 2 RLC ; Multiply by 4
ADD D ; Add original (×1) to get ×5 RLC ; Multiply by 2 again to get ×10
MOV C, A ; Store tens×10 in register C MOV A, B ; Recall original BCD value
ANI 0FH ; Mask to isolate units digit ADD C ; Add (tens×10 + units) STA 2020H
; Store final binary result HLT ; End program

This segment completes the conversion by calculating tens×10, adding the units digit, and storing the final binary result.
BCD to Binary: Code Notes
Register Usage Key Instructions

• A (Accumulator): Primary working register • ANI: Bitwise AND for masking digits
• B: Temporary storage for original BCD • RLC/RRC: Rotate operations for shifting
• C: Holds tens digit after multiplication by 10 • ADD: Addition for combining values
• D: Additional temporary storage for calculations • MOV: Register-to-register data transfer
• LDA/STA: Memory access operations

The code minimizes memory access by keeping intermediate values in registers, improving execution speed. The multiplication by
10 is achieved through shifts and additions rather than actual multiplication instructions, which are not available in the basic 8085
instruction set.
Binary to BCD Conversion Algorithm
Load Binary Value
Retrieve the binary value to be converted from memory into the accumulator

Initialize Counters
Set up registers to track the tens and units digits during conversion

Repeated Subtraction
Repeatedly subtract 10 from the binary value, incrementing tens counter each time

Handle Remainder
When subtraction results in a value less than 10, that becomes the units digit

Form BCD Result


Combine tens and units digits into BCD format (tens in upper nibble, units in lower)
Binary to BCD: 8085 Steps
Algorithm Details

1. Load binary value into accumulator

2. Initialize a counter register (e.g., B) to zero for tracking tens

3. Enter loop: Subtract 10 (0AH) from accumulator

4. If subtraction doesn't cause borrow (carry flag = 0), increment tens counter and
repeat step 3

5. If borrow occurs (carry flag = 1), add back 10 to get units digit

6. Shift tens digit to upper nibble position

7. Combine tens and units digits to form BCD result


Binary to BCD: Code Skeleton
; Binary to BCD Conversion; Input: Binary value at memory location 2030H; Output: BCD value at memory
location 2031H LDA 2030H ; Load binary value MVI B, 00H ; Initialize tens counter to
0 LOOP: SUI 0AH ; Subtract 10 JC DONE ; If carry, we've gone too far
INR B ; Increment tens counter JMP LOOP ; Continue subtracting DONE: ADI 0AH
; Add back 10 to get units digit ; Continue with digit packing...

This code skeleton implements the repeated subtraction method, using the carry flag to detect when we've subtracted 10 too
many times. Register B tracks the tens digit count.
Binary to BCD: Final Packing
Digit Packing Process
; Continuing from previous segment MOV C,
A ; Save units digit in C MOV A, B After obtaining the tens and units digits:
; Get tens digit RLC ; Shift
1. Store the units digit temporarily in register C
left RLC ; Shift left RLC
; Shift left RLC ; 2. Move the tens digit to the accumulator
Tens digit now in upper nibble ORA 3. Shift the tens digit left four times (into the upper nibble position)
C ; Combine with units digit STA 4. Use bitwise OR to combine the shifted tens digit with the
2031H ; Store BCD result HLT units digit
; End program
5. Store the resulting packed BCD value to memory

The final packing operation creates a properly formatted BCD value with the tens digit in the upper nibble and the units digit in the lower nib
Use Cases: Code Conversion

Digital Displays Calculators


Converting binary computational results to BCD for display Converting between BCD input from keypads and binary for
on seven-segment LEDs, LCD panels, and other digital internal calculation, then back to BCD for display of results.
readouts that require decimal representation.

Sensor Systems Data Processing


Converting binary sensor readings to BCD for human- Converting between formats when transferring data
readable displays in measurement equipment, control between different systems or when interfacing with legacy
systems, and monitoring devices. equipment that uses different number representations.
What is Sorting?
Sorting is the process of arranging data elements in a specific order, typically
ascending or descending based on key values.

Applications in Computing:

• Database operations and queries


• Search algorithms (binary search requires sorted data)
• Data analysis and statistics
• User interface displays
• Priority scheduling in operating systems

Sorting is a fundamental operation in computer science, serving as a building block


Types of Sorting Algorithms
Selection Sort

Bubble Sort Finds the minimum element and places


it at the beginning, then repeats for the
Repeatedly steps through the list,
remainder of the list. Performs well on
compares adjacent elements, and swaps
small lists but inefficient on large ones.
them if they're in the wrong order.
Simple to implement but inefficient for
Insertion Sort
large datasets.
Builds the sorted array one item at a
time by comparing each with the items
before it. Efficient for small datasets
and nearly-sorted data.
Quick Sort
Uses a divide-and-conquer strategy with Merge Sort
a pivot element. Very efficient for large
Divides the array into halves, sorts each
datasets but has poor worst-case
half, then merges them. More efficient
performance.
but requires additional memory and
more complex code.

For 8085 implementation, simpler algorithms like bubble sort are preferred due to memory constraints and the limited instruction set.
Bubble Sort Overview
Algorithm Characteristics:

• Simple comparison-based sorting algorithm


• Named for the way smaller elements "bubble" to the top of the list
• Performs repeated passes through the array
• On each pass, adjacent elements are compared and swapped if out of order
• Each pass guarantees that at least one element moves to its final position
• Time complexity: O(n²) in worst and average cases
• Space complexity: O(1) - requires only a constant amount of additional memory
• Stable sort: preserves the relative order of equal elements

Despite its inefficiency for large datasets, bubble sort is ideal for assembly
language implementation due to its simplicity.
Bubble Sort: Practical Examples
Initial Array: [5, 1, 4, 2, 8]

First Pass:

• Compare 5 & 1: Swap → [1, 5, 4, 2, 8]


• Compare 5 & 4: Swap → [1, 4, 5, 2, 8]
• Compare 5 & 2: Swap → [1, 4, 2, 5, 8]
• Compare 5 & 8: No swap → [1, 4, 2, 5, 8]

Second Pass:

• Compare 1 & 4: No swap → [1, 4, 2, 5, 8]


• Compare 4 & 2: Swap → [1, 2, 4, 5, 8]
• Compare 4 & 5: No swap → [1, 2, 4, 5, 8]
• Compare 5 & 8: No swap → [1, 2, 4, 5, 8]

The algorithm makes at most n-1 passes through the array, where n is the number of
Ascending Sort: Definition
Smallest First
Elements arranged from lowest to highest value

Natural Order
For numbers: 1, 2, 3, 4, 5...

For alphabet: A, B, C, D, E...

Default Sort
Most commonly used ordering in computing applications

Implementation
In 8085: Compare adjacent elements and swap if first > second

Ascending sort is the most common ordering requirement in data processing tasks, providing a foundation for binary search and
other algorithms.
Descending Sort: Definition
Largest First
Elements arranged from highest to lowest value

Reverse Order
For numbers: 5, 4, 3, 2, 1...

For alphabet: Z, Y, X, W, V...

Special Applications
Used for ranking, priority queues, and "top N" displays

Implementation
In 8085: Compare adjacent elements and swap if first < second

Descending sort is particularly useful for displaying rankings, high scores, or prioritizing critical items in a list.
8085 Sorting Challenges
Limited Register Set

• Only seven 8-bit registers available (A, B, C, D, E, H, L)


• Requires careful register allocation for algorithm implementation

No Direct Array Support

• Must use memory addressing with pointer registers (HL)


• No high-level array indexing or data structures

Limited Instruction Set

• No built-in swap operation


• Limited comparison options

Memory Management
Bubble Sort on 8085: Outline
Initialization 1
Set up registers and pointers to the beginning of the array

Initialize counter for the number of elements


2 Outer Loop
Control the number of passes through the array (n-1 passes required)

3 Reset pointer to array start for each pass


Inner Loop
Compare adjacent elements in the current pass

Perform swaps when elements are out of order 4 Completion


Array is sorted after all passes are complete

Final result remains in the original memory location


Bubble Sort: Key 8085 Instructions
; Key instructions for bubble sort implementationLXI H, 2000H ; Load HL register pair with start address
of arrayMOV A, M ; Move value at memory location [HL] to accumulatorINX H ; Increment HL
pointer to next array elementCMP M ; Compare accumulator with value at current [HL]JC NOSWAP
; Jump if carry flag set (A < M, already in order) ; Otherwise, perform swap operations; For
swappingMOV B, A ; Temporarily store A in register BMOV A, M ; Load second element into ADCX
H ; Decrement HL to point back to first elementMOV M, A ; Store second element in first
positionINX H ; Increment HL to point to second positionMOV M, B ; Store first element in
second position

These core instructions form the building blocks of the bubble sort implementation in 8085 assembly.
Bubble Sort: Ascending Code Concept
Pseudocode Logic 8085 Implementation Approach

• Use B register to control outer loop (passes)


for i = 0 to n-2 for j = 0 to n-2-i if
• Use C register to control inner loop (comparisons)
array[j] > array[j+1] swap(array[j],
array[j+1]) • Use HL register pair as pointer to current element
• Compare adjacent elements using CMP instruction
• Swap elements when out of order (A > M)
• Use JC instruction to skip swap when elements are already
The outer loop (i) controls the number of passes through the
in order
array, while the inner loop (j) performs the comparisons and
swaps.

The key comparison for ascending order is checking if the current element is greater than the next element. If so, a swap is needed.
Bubble Sort: Descending Code Concept
Pseudocode Logic 8085 Implementation Approach

• Same loop structure as ascending sort


for i = 0 to n-2 for j = 0 to n-2-i if
• Same register usage (B, C, HL)
array[j] < array[j+1] swap(array[j],
array[j+1]) • Key difference: swap when current element is less than
next element
• Use JNC instruction to skip swap when elements are already
in order (for descending)
The structure is identical to ascending sort, with only the
comparison condition reversed.

Converting between ascending and descending sort implementations requires only changing the comparison condition and the
conditional jump instruction.
Example: Bubble Sort 8085 Code Start
; Bubble Sort (Ascending Order); Sorts array starting at memory location 2000H; Array length: 8
bytesSTART: LXI H, 2000H ; Initialize HL to start of array MVI B, 07H ; B = n-1 (7 for
8 elements), outer loop counter OUTER: MOV C, B ; Initialize inner loop counter
LXI H, 2000H ; Reset HL to start of array for each pass INNER: MOV A, M ; Get
current element INX H ; Point to next element CMP M ; Compare
current with next JC NOSWAP ; Skip swap if current < next (already in order)

This code segment initializes the bubble sort process and sets up the nested loop structure. Register B controls the outer loop
(number of passes), while register C controls the inner loop (comparisons within each pass).
Bubble Sort: Comparing Elements
Ascending Sort Comparison Descending Sort Comparison

; For ascending order (smallest to largest)MOV A, ; For descending order (largest to smallest)MOV
M ; Get first elementINX H ; A, M ; Get first elementINX H
Point to next elementCMP M ; Compare ; Point to next elementCMP M ;
A with MJC NOSWAP ; Skip swap if A < M Compare A with MJNC NOSWAP ; Skip swap if
(carry set) ; If A >= M, A >= M (carry not set) ; If A <
continue to swap M, continue to swap

The CMP instruction subtracts M from A and sets flags based on the result. The carry flag (CF) is set if A < M. This flag-based result
is used with conditional jumps (JC/JNC) to determine whether a swap is needed.
Bubble Sort: Swapping
; Swap routine for bubble sort; At this point, A contains first element, HL points to second elementSWAP:
MOV B, A ; Save first element temporarily in B MOV A, M ; Get second element
into A DCX H ; Point back to first element MOV M, A ; Store second
element in first position INX H ; Point to second position again MOV M, B
; Store first element in second positionNOSWAP: DCR C ; Decrement inner loop counter
JNZ INNER ; Continue inner loop if not zero

The swap operation requires a temporary register (B) to hold one value while the other is moved. Since 8085 lacks a direct swap
instruction, this sequence of moves accomplishes the exchange of values.
Bubble Sort: Looping
Completing the Inner Loop Completing the Outer Loop

NOSWAP: DCR C ; Decrement inner loop DCR B ; Decrement outer loop


counter JNZ INNER ; Continue inner counter JNZ OUTER ; Continue outer
loop if not zero loop if not zero HLT
; End program when sorting complete
After each comparison (and possible swap), the inner loop
counter is decremented. The loop continues until all relevant
comparisons for the current pass are complete. After each complete pass through the array, the outer loop
counter is decremented. The sorting process is complete when
all passes are finished.

Each pass through the array places at least one element in its final position. After n-1 passes (where n is the number of elements),
the array is guaranteed to be fully sorted.
Sorting: 8085 Memory Structure
Array Layout in Memory

Memory Address | Value-----------------|--------2000H | 25H2001H


| 10H2002H | 57H2003H | 32H2004H | 83H2005H
| 45H2006H | 91H2007H | 68H

In 8085 programming, arrays are simply consecutive memory locations. The programmer must track
the starting address and length.

Memory Pointer Operations


Sorting in Practice: Ascending vs Descending
Ascending Sort (Key Lines) Descending Sort (Key Lines)

; Compare and conditional jump for ascendingCMP M ; Compare and conditional jump for descendingCMP
; Compare A with MJC NOSWAP ; M ; Compare A with MJNC NOSWAP
Skip swap if A < M ; (elements ; Skip swap if A >= M ;
already in order) (elements already in order)

In ascending sort, elements are arranged from smallest to In descending sort, elements are arranged from largest to
largest. We swap when the current element is greater than the smallest. We swap when the current element is less than the
next element. next element.

The core sorting algorithm remains identical for both ascending and descending orders. Only the comparison condition changes,
which affects the conditional jump instruction used after comparison.
Program: Full Sorting Example
Bubble Sort of 8-byte Array

; Bubble Sort Program (Ascending Order); Sorts array at memory locations 2010H-2017H LXI H, 2010H ; Point to start of array MVI B, 07H ; B = n-1 (7 for 8 elements)
OUTER: MVI C, 07H ; C = n-1 (reset for inner loop) LXI H, 2010H ; Reset to start of array INNER: MOV A, M ; Get current element INX H ; Point to next element CMP M ;
Compare current with next JC NOSWAP ; Skip swap if current < next MOV D, M ; Save next element in D MOV M, A ; Store current in next position DCX H ; Point back to
current position MOV M, D ; Store next in current position INX H ; Point to next position again NOSWAP: DCR C ; Decrement inner counter JNZ INNER ; Continue inner loop
if not done DCR B ; Decrement outer counter JNZ OUTER ; Continue outer loop if not done HLT ; End program
Debugging Tips and Common Pitfalls
Off-by-One Errors Pointer Initialization
Initialize counters carefully (n-1 for outer loop). Watch array Reset HL pointer to array start for each outer loop iteration.
boundaries to avoid accessing invalid memory locations. Failing to do this will cause the algorithm to operate on
incorrect memory locations.

Loop Control Register Usage


Ensure counter registers (B, C) are properly decremented Keep track of which registers hold which values. Using a
and tested. Incorrect loop termination conditions can lead register for multiple purposes without proper
to infinite loops or incomplete sorting. saving/restoring can cause data corruption.

Swap Operations Comparison Logic


The swap operation requires careful sequencing of moves Double-check the conditional jump after comparison. JC for
and pointer adjustments. Test thoroughly with different ascending and JNC for descending are common sources of
input data to verify correctness. errors.
Summary & Key Takeaways
Code Conversion Sorting Algorithms

• •
BCD to Binary conversion is essential for mathematical operations Bubble sort is simple to implement in 8085 assembly
• Binary to BCD conversion is crucial for human-readable output • Ascending and descending sorts differ only in comparison logic
• Both conversions can be efficiently implemented on the 8085 • 8085's limited instruction set requires creative approaches
• Code conversion bridges the gap between human and • Careful register and memory management is essential
machine number systems

The 8085 microprocessor, despite its limitations, can effectively implement both code conversion and sorting algorithms through
careful programming and efficient use of its instruction set.
Questions & Further Reading
Recommended Resources:

• Gaonkar: "Microprocessor Architecture, Programming and Applications with the 8085"


• Mathur: "Introduction to Microprocessors"
• Brey: "The Intel Microprocessors: 8086/8088, 80186/80188, 80286, 80386, 80486, Pentium, and Pentium Pro Processor"

Next Steps:

• Experiment with online 8085 simulators to test your own code


• Implement more advanced sorting algorithms like insertion sort or selection sort
• Explore multi-digit BCD conversions beyond two digits
• Combine sorting and conversion in practical applications

You might also like