0% found this document useful (0 votes)
11 views7 pages

002 Mpmctermwork

Uploaded by

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

002 Mpmctermwork

Uploaded by

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

8.

Efficient Use of Resources:

Assembly language provides very low-level control over the CPU and memory, which allows the
program to:

• Work directly with individual bytes in memory.


• Efficiently swap and compare bytes using registers.
• Handle loops with minimal overhead.

Step-by-Step Breakdown:
1. Concept:
o You’ll input a string of bytes.
o The program will sort the bytes using a simple algorithm like bubble sort.
o The sorted result will be displayed in both ascending and descending order.
2. Registers Used:
o SI (Source Index) and DI (Destination Index) will point to memory locations.
o CX will be used as a loop counter.
o AX, BX for temporary data storage.
o AL, BL for byte-wise comparison.
3. Assumptions:
o The string is stored in memory starting at a certain address.
o The string length is known and will be stored in a register.

Key Features of the Assembly Language Program


for Sorting a String
1. 8086 Microprocessor-Based Program:
o The program is designed for the 8086 microprocessor, which uses a segmented
memory architecture, allowing for efficient memory management and organization
of code, data, and stack segments.
2. Memory Segmentation:
o The program operates within the small memory model, which segments memory
into code, data, and stack.
o Memory segments are explicitly initialized and controlled, which is critical in low-
level programming like assembly.
3. String Sorting:
o The program takes an input string and sorts it into ascending and descending orders
based on ASCII values of the characters.
4. Bubble Sort Algorithm:
o Uses the bubble sort algorithm, which is a simple comparison-based sorting
method.
o The algorithm compares adjacent characters and swaps them if they are out of order
(in both ascending and descending sorting).
5. Manipulation of ASCII Values:
o The program sorts the string by comparing characters’ ASCII values (e.g., 'A' = 65, 'B'
= 66).
o Sorting in ascending order arranges characters from the smallest to the largest ASCII
values, while descending order arranges them from the largest to the smallest.
6. Use of 8086 Registers:
o Registers like AX, SI, DI, and CX are used to:
▪ Store temporary data (like characters during swapping).
▪ Point to memory locations (the string and sorting buffers).
▪ Control loops (like string copying and sorting).
o Efficient use of registers minimizes memory access and improves performance.
7. String Copying:
o Before sorting, the input string is copied into buffers (sortedAsc and sortedDesc)
using a loop.
o This preserves the original string while sorting is performed on the copied data.
8. Two Sorting Orders:
o Ascending Order Sorting: The program first sorts the string in ascending order,
comparing adjacent characters and arranging them from the smallest to the largest
ASCII value.
o Descending Order Sorting: After the ascending sort, the program reverses the order
of the characters to achieve descending order.
9. Loop and Conditional Control:
o The program uses looping mechanisms (such as LOOP and CX counter) for repeated
operations like copying the string and sorting.
o Conditional jumps (like JBE and JAE) are used to control the sorting logic based on
comparisons of character values.
10. Program Termination:
o The program terminates gracefully using the DOS interrupt INT 21H, which is
standard for 8086 assembly programs running in DOS environments.
11. Low-Level Hardware Control:
o The program demonstrates direct control over hardware through assembly language
instructions, providing efficiency and flexibility in handling operations like memory
access, data comparison, and sorting.

DEVELOP AND EXECUTE ALP TO ARRANGE BYTES


OF STRING IN ASCENDING AND DESCENDING
ORDER BY USING EMULATOR 80806
To design and execute an assembly language program for sorting a string of bytes in both ascending
and descending order using the 8086 microprocessor, you can utilize the emulator "EMU8086" or
any similar assembler.

.MODEL SMALL
.STACK 100H
.DATA
string DB 'HELLO' ; Input string to be sorted
length DW $ - string ; Calculate the length of the string
sortedAsc DB 5 DUP(0) ; To store the ascending sorted result
sortedDesc DB 5 DUP(0) ; To store the descending sorted result

.CODE
MAIN PROC
; Set up the data segment
MOV AX, @DATA
MOV DS, AX

; Copy the original string to the sorting array (ascending)


LEA SI, string
LEA DI, sortedAsc
MOV CX, length
COPY_STRING:
MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
LOOP COPY_STRING

; Sorting in Ascending order


LEA SI, sortedAsc
MOV CX, length
SORT_ASC:
MOV DI, SI
INC DI
MOV CX, length
DEC CX
COMPARE_ASC:
MOV AL, [SI]
MOV BL, [DI]
CMP AL, BL
JBE NEXT_COMPARE_ASC ; If AL <= BL, no need to swap
; Swap AL and BL
MOV [SI], BL
MOV [DI], AL
NEXT_COMPARE_ASC:
INC DI
DEC CX
JNZ COMPARE_ASC
INC SI
MOV CX, length
DEC CX
JNZ SORT_ASC

; Display sorted result in ascending order (optional display routine)


; [Your display routine can be implemented here]

; Now copy the ascending sorted result to sortedDesc for descending sorting
LEA SI, sortedAsc
LEA DI, sortedDesc
MOV CX, length
COPY_DESC:
MOV AL, [SI]
MOV [DI], AL
INC SI
INC DI
LOOP COPY_DESC

; Reverse the sorted array for descending order


LEA SI, sortedDesc
MOV CX, length
DEC CX
SORT_DESC:
MOV DI, SI
INC DI
MOV CX, length
DEC CX
COMPARE_DESC:
MOV AL, [SI]
MOV BL, [DI]
CMP AL, BL
JAE NEXT_COMPARE_DESC ; If AL >= BL, no need to swap
; Swap AL and BL
MOV [SI], BL
MOV [DI], AL
NEXT_COMPARE_DESC:
INC DI
DEC CX
JNZ COMPARE_DESC
INC SI
MOV CX, length
DEC CX
JNZ SORT_DESC

; Display sorted result in descending order (optional display routine)


; [Your display routine can be implemented here]

; End of the program


MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN

EXPLANATION OF THE PROGRAM:


String Copying:

• We first copy the input string string to a buffer sortedAsc for ascending sorting.

Bubble Sort:

• We use two loops: the outer loop to control the number of passes and the inner loop to
compare and swap the elements.
• CMP is used to compare two adjacent bytes. If they are in the wrong order, we swap them.

Descending Order:

• After sorting in ascending order, the array is reversed to get the descending order.

Summary of Key Operations:


• String Copying: Moves data from the input string to the sorting buffers.
• Bubble Sort Algorithm: Used to sort the string in both ascending and descending orders.
• Data Movement: Registers like SI, DI, and CX are used for indexed addressing, loops, and
comparisons.
• Termination: The program ends with the DOS INT 21H interrupt.

This is a simple and efficient way to implement sorting in assembly for the 8086 microprocessor.
ABSTRACT
The Intel 8086 microprocessor, introduced in 1978, marked a significant advancement
in computer architecture. As a 16-bit microprocessor, it was capable of addressing up
to 1 MB of memory through its unique segmented memory model, which divides
memory into segments (code, data, stack, and extra). This segmentation allows
programs to utilize memory more efficiently and simplifies the management of large
codebases.
The 8086 features a comprehensive instruction set that supports arithmetic, logical,
and control flow operations, enabling complex computations and robust programming
capabilities. It operates with a maximum clock speed of 10 MHz and employs a
multiprocessing architecture, which allows multiple processors to operate
concurrently, thereby enhancing computational power and efficiency
This assembly language program for the 8086 microprocessor sorts a string of
characters in both ascending and descending order using the bubble sort algorithm.
The program leverages the 8086's segmented memory model to organize the data and
code, efficiently manipulating memory and registers. The string is first copied into a
buffer, preserving the original, and then sorted based on the ASCII values of the
characters. Key 8086 instructions such as LOOP, CMP, and conditional jumps control
the sorting process, while the SI and DI registers are used to index memory for
comparison and swapping.
After sorting the string in ascending order, the program reverses the result for
descending order. The program terminates using a DOS interrupt, showcasing low-level
CPU control and memory management typical in assembly language. This project
demonstrates fundamental concepts of assembly programming, including register
manipulation, memory segmentation, string handling, and sorting algorithms,
providing a practical example of working with low-level system instructions.

You might also like