Mp lab manual
Mp lab manual
1
Ramaiah University of Applied Sciences
Private University Established in Karnataka State by Act No. 15 of 2013
List of Experiments
1. Data transfer operations
2. Arithmetic operations
3. Logical operations
5. String manipulation
2
7. Sorting an array
8. Interfacing
9. Interfacing
2 Arithmetic operations
3 Logical operations
5 String manipulation
6 Searching an element in an
array
7 Sorting an array
3
8 Interfacing
9 Interfacing
4
Laboratory 1
Title of the Laboratory Exercise: Data transfer operations
Aim
To develop assembly language program to perform data transfer operations on different data.
Objectives
3) Experimental Procedure
1. Write algorithm to solve the given problem
2. Translate the algorithm to assembly language code
3. Run the assembly code in GNU assembler
4. Create a laboratory report documenting the work
4) Questions
1. Perform the following data transfer operations
1. 32 bit integer data to a General Purpose register
Segment Register
Memory
5
2. 16 bit integer data to a General Purpose register
Segment Register
Memory
5) Calculations/Computations/Algorithms
1,2,3. 32/16/8 bit integer data to a General Purpose register/ Segment Register/ Memory
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: movl/w/b $num, % (General Purpose register/ Segment Register/ Memory)
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: check info registers
6
Step 12: n
Step 13: info registers
Step 14: end
7
Step 11: run
Step 12: check info registers
Step 13: n
Step 14: info registers
Step 15: end
6) Presentation of Results:
7)
Input :
Output:
Output:
8
(c) Memory
Input :
Output:
9
Output:
Output:
(c) Memory
Input :
10
Output:
Input :
Output:
11
(b) Segment Register Input:
Output:
(c) Memory
Input :
Output:
12
4. 1. 32 bit general purpose data to a
(a) General Purpose register Input :
Output:
13
Output:
(c) Memory
Input :
14
Output:
15
Output:
Output:
16
c. Memory
Input :
Output:
17
3. 8 bit general purpose to a
a. General Purpose register
Input :
Output:
18
Output:
c. Memory
Input :
Output:
19
(a) General Purpose register Input
:
Output:
Output:
(c) Memory
Input :
20
Output:
Output:
21
Output:
(c) Memory
Input :
Output:
22
Output:
Output:
(c) Memory
Input :
23
Output:
Output:
In assembly programming, data transfer operations are used to move data between registers and
memory locations, or to transfer data between registers
24
MOV: The MOV instruction is used to move data from a source operand to a destination operand. The
source operand can be a register or a memory location, and the destination operand can be a register
or a memory location.
One important thing to note about the MOV instruction is that it does not support direct memory-
tomemory transfers. If you want to move data from one memory location to another, you will need to
use an intermediate register as a buffer.
One important thing to note about the MOV instruction is that it does not support direct memory-
tomemory transfers. If you want to move data from one memory location to another, you will need to
use an intermediate register as a buffer.
8. Conclusions
In conclusion, data transfer operations play a crucial role in modern computing environments,
enabling the movement of data between various systems, devices, and platforms. Through this
process, organizations can achieve several benefits such as improved collaboration, data
accessibility, system integration, and enhanced decision-making capabilities. However, effective data
transfer operations require careful planning, implementation, and management to ensure data
security, integrity, and compliance with regulations.
9. Comments
Data transfer operations involve the movement of data from one location to another, typically within a
computer system or between different systems. These operations are fundamental to modern
computing and are crucial for tasks such as file sharing, database synchronization, network
communication, and system backups. Data transfer can occur via various methods such as direct
memory access (DMA), input/output operations, network protocols (like TCP/IP), and storage mediums
(such as disks or cloud storage). Efficient data transfer operations are essential for ensuring data
integrity, minimizing latency, optimizing system performance, and supporting real-time applications
across diverse computing environments.
25
Laboratory 2
Title of the Laboratory Exercise: Arithmetic Operations
Aim
To develop assembly language program to perform all arithmetic operations.
Objectives
– Identify the appropriate assembly language instruction for the given arithmetic operations
– Perform all arithmetic operations using assembly language instructions
– Understand different data types and memory used
– Get familiar with assembly language program by developing simple programs
3. Experimental Procedure
1. Write algorithm to solve the given problem
2. Translate the algorithm to assembly language code
3. Run the assembly code in GNU assembler
4. Create a laboratory report documenting the work
4. Questions
1. Consider the following source code fragment
Int a,b,c,d;
a= (b + c)-d + (b*c) / d;
26
• Assume that b, c, d are in registers. Develop an assembly language program to perform this
assignment statements.
Value of b= 10
Value of c= 12
Value of d=2
5. Calculations/Computations/Algorithms:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: move the b,c,d to registers
Step 6: preform the arithmetic operations
Step 7: compile
Step 8: link
Step 9: gdb run
Step 10: break point
Step 11: run
Step 12: check info registers
Step 13: n
Step 14: info registers
Step 15: end
27
1.2 b is in registers and c, d in memory:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: move the b to registers
Step 6: preform the arithmetic operations
Step 7: compile
Step 8: link
Step 9: gdb run
Step 10: break point
Step 11: run
Step 12: check info registers
Step 13: n
Step 14: info registers
Step 15: end
2. Multiplication and division by shift operations:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: move the b,c,d to registers
Step 6: preform the sall for multiplication, sarl for division
Step 7: compile
Step 8: link
Step 9: gdb run
Step 10: break point
Step 11: run
Step 12: check info registers
Step 13: n
28
Step 14: info registers
Step 15: end
6. Presentation of Results:
Output:
29
1.2 b is in registers and c, d in memory:
30
Input:
Output :
31
2. Multiplication and division by shift operations:
Input:
Output:
32
7. Analysis and Discussions:
The Assembly Language instructions correspond to machine code and how they are executed by
the processor. This includes understanding how the values are stored in registers, how the values
are manipulated, and how the results are stored. Additionally, it's important to understand how
the arithmetic operations affect the processor's flags, such as the carry flag, which can affect the
results of subsequent operations.
8. Conclusions
Arithmetic operations form the foundational pillars of mathematics, providing essential tools for
solving numerical problems across various domains. Through addition, subtraction,
multiplication, and division, we manipulate numbers to perform calculations, analyze data, and
make informed decisions. Arithmetic operations are fundamental in everyday life, from simple
tasks like counting money to complex scientific calculations. They are crucial for understanding
mathematical concepts and developing problem-solving skills. While calculators and computers
33
can perform arithmetic operations swiftly, understanding the underlying principles remains
crucial for verifying results, interpreting data, and developing algorithms.
9. Comments
Arithmetic operations are taught early in education to develop numerical fluency and
mathematical reasoning. They form the basis for more complex mathematical concepts students
encounter later in their academic journey. Engaging with arithmetic operations promotes critical
thinking by requiring logical reasoning and systematic approaches to problem-solving. It fosters
analytical skills and enhances decision-making abilities.
34
Laboratory 3
Title of the Laboratory Exercise: Logical operations
Aim
Objectives
– Identify the appropriate assembly language instruction for the given logical operations
– Perform all logical operations using assembly language instructions
– Get familiar with assembly language program by developing simple programs
3. Experimental Procedure
35
Perform multiplication and division by shift operations
5. Calculations/Computations/Algorithms:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: move integers to registers and perform logical opperations
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: check info registers
Step 12: n
Step 13: info registers
Step 14: end
2. Multiplication and division by shift operations:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: move the b,c,d to registers
Step 6: preform the sall for multiplication, sarl for division
Step 7: compile
Step 8: link
Step 9: gdb run
Step 10: break point
Step 11: run
Step 12: check info registers
Step 13: n
36
Step 14: info registers
Step 15: end
6. Presentation of Results:
7.
7.
1. a= (b AND c) XOR d:
1. b, c, d are in registers
Input
Output:
37
2. b, c are in registers and d in memory
input:
Output:
38
2. a=(b XOR c) OR d 1. b, c,
d are in registers
input :
Output :
39
2. b, c are in registers and d in memory
input:
Output:
40
2. Multiplication and division by shift operations:
Input:
Output:
41
8. Analysis and Discussions:
In assembly programming, logical operations are used to perform bitwise operations on data
stored in registers or memory locations. These operations include AND, OR, XOR, and NOT. AND:
The AND operation compares each bit of the first operand to the corresponding bit of the second
operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set
to 0.
OR: The OR operation compares each bit of the first operand to the corresponding bit of the
second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the result bit
is set to 0.
XOR: The XOR operation compares each bit of the first operand to the corresponding bit of the
second operand. If the bits are different, the corresponding result bit is set to 1. Otherwise, the
result bit is set to 0.
42
NOT: The NOT operation inverts all the bits of the operand. If a bit is 0, it is set to 1. If a bit is 1, it
is set to 0.
Logical operations are frequently used in assembly programming for tasks such as masking and bit
shifting. They are also often used in conjunction with conditional jump instructions to implement
control flow in a program.
9. Conclusions
Logical operations play a fundamental role in computer science, mathematics, philosophy, and
various other fields where precise reasoning and decision-making are required. Logical operations
form the basis of digital computing systems. Boolean logic, with its AND, OR, NOT, and XOR
operations, is at the core of designing circuits, programming languages, and algorithms in
computer science. Understanding logical operations enhances critical thinking and problem-
solving skills. By applying logical rules and principles, individuals can analyze arguments, evaluate
propositions, and draw valid conclusions. Logical operations intersect with mathematics,
particularly in set theory and predicate logic. Set operations like union, intersection, and
complement can be viewed through logical lenses, aiding in mathematical proofs and problem-
solving.
10. Comments
Logical operations are crucial in data analysis and manipulation. Filtering data based on logical
conditions (e.g., filtering a database for specific criteria) and performing logical comparisons
(e.g., checking for equality or inequality) are common tasks in data science and database
management. Logical operations are extensively used in programming for decision-making and
controlling program flow. Conditional statements (if-else, switch-case) and loops rely on logical
conditions to execute specific code blocks based on criteria. In artificial intelligence (AI) and
decision-making systems, logical operations play a vital role. Boolean logic and logical reasoning
algorithms are used to model decision trees, expert systems, and rule-based systems for
automated decision-making.
43
Signature and date Marks
44
Laboratory 4
Title of the Laboratory Exercise: Array manipulation
Students will be able to perform control flow operations using conditional instructions
Aim
To develop assembly language program to access the elements in an array or to access a particular
element in an array using indexed addressing mode.
Objectives
3. Questions
a. Assume an array of 8 integers. Translate this C statement/assignment using the indexed
form: x and y are memory variable
x = array [7] + y
b. Create an array of 10 elements and initialize the array elements with random values.
i. Assign array[5]=0 and array[8]=0 (array is the name of the array, you can choose
any other name instead of a name array)
45
ii. Perform: (value is a memory variable) value =array [3] +array [6] array [7]
=value+array[7]
iii. Compare array[5] with array[8], show whether both the values are equal or not
iv. Read the 7th element of the array and check whether it’s an odd or even
number
46
2.1. Assign array[5]=0 and array[8]=0
Step 1: create an array with 10 integers
Step 2: move 5,8 into registers
Step 3: perform move operation and replace the array index places as “0”
Step 4: continue the step 6 from general
Step 5:print the array by print(int[10])array
2.2. value =array [3] +array [6] ; array [7] =value+array[7]
Step 1: create an array with 10 integers
Step 2: define value as memory
Step 3: move 3,6,7 into registers
Step 4: perform move operatation and preform arithmetic operatation
Step 5: move the registers to memory
Step 5: continue the step 6 from general
Step 6:print the array by print(int[10])array
2.3. Compare array[5] with array[8]:
Step 1: create an array with 10 integers
Step 2: define value as memory
Step 3: move 5,8 into registers
Step 4: perform move operatation by accessing an array
Step 5: compare the registers by cmpl
Step 6: check equal or not
Step 7: jump if equal to a loop
Step 8: if not following with procedure
Step 9: continue the basic operatations from step 6
2.4. The 7th element of the array and check whether it’s an odd or even number:
Step 1: create an array with 10 integers
Step 2: define value as memory
Step 3: move 7 into registers
Step 4: perform move operatation by accessing an array
Step 5: use the arithmetic operatation of division
47
Step 6: compare the %edx register with $0
Step 7: jump if equal to a loop
Step 8: if not following with procedure
Step 9: continue the basic operatations from step 6
3. program to generate factorial:
Step 1: assign a value in the memory
Step 2: move the value to eax register
Step 3: move $1 to some register
Step 4: loop : mull registers
jmp comp
Step 5: comp : compare $1 with register
je dec
movl $0,%eax
jmp exit
Step 6:dec : subl $1,register
Jmp loop
Step 7: exit : exit codes of three lines
Step 8: continue the basic operatations from step 6
5 .Presentation of Results:
a) x = array [7] + y :
Input:
48
Output:
49
Output:
50
Output:
51
b.3) Compare array[5] with array[8]:
Input:
Output:
52
b.4) the 7th element of the array and check whether it’s an odd or even number:
input:
Output:
53
c) factorial of any given number:
Input:
Output :
54
5. Analysis and Discussions:
In assembly language, arrays are typically implemented using memory addresses. Each element in
the array is stored at a specific memory location, and the programmer must keep track of the
starting address of the array and the size of each element in order to access and manipulate the
elements.
Loops are also implemented using assembly language instructions for branching and conditional
testing. The programmer must manually increment or decrement a counter and test it against a
set limit in order to control the number of iterations in the loop.
6. Conclusions
Array manipulation is a powerful concept in computer science and programming that involves
organizing, transforming, and accessing data stored in arrays. Array manipulation is a powerful
concept in computer science and programming that involves organizing, transforming, and
accessing data stored in arrays. Arrays can be one-dimensional, multidimensional, or even
nested within other arrays, providing flexibility in representing complex data structures. This
versatility enables programmers to model real-world scenarios more accurately and solve
diverse problems effectively.
7. Comments
Array manipulation involves a range of operations such as inserting, deleting, updating, and
rearranging elements within arrays. These operations are fundamental in data processing,
algorithm design, and software development tasks. Many algorithms and data processing
techniques heavily rely on array manipulation. Efficient sorting algorithms like merge sort,
55
quicksort, and efficient searching algorithms such as binary search leverage array manipulation
principles to achieve optimal performance. Dynamic arrays and resizable arrays are
implementations that allow arrays to dynamically grow or shrink in size as needed. This dynamic
memory management capability is crucial for handling varying data loads efficiently.
56
Laboratory 5
Title of the Laboratory Exercise: Controlling execution flow using conditional instructions
Aim
To develop assembly language program to perform control flow operations using conditional
instructions.
Objectives
– Identify the appropriate assembly language instruction for the given conditional
operations
– Perform all conditional operations using assembly language instructions
– Get familiar with assembly language program by developing simple programs
3. Experimental Procedure
1. Write algorithm to solve the given problem
2. Translate the algorithm to assembly language code
3. Run the assembly code in GNU assembler
4. Create a laboratory report documenting the work
4. Questions
Develop an assembly language program to perform the following
1. Create an uninitialized array and print ‘n’ natural numbers in the created array.
2. Develop an assembly language program to insert a given element in an array at the
specified position.
Input Array: 10, 20,30,40,50
57
Element to be inserted: 60
Position: 3
Output: 10, 20, 30,60,40,50
3. Compute GCD for the given two natural numbers
4. Compute LCM for the given two natural numbers
5. Develop an assembly language program to generate the first n numbers in Fibonacci
series.
5. Calculations/Computations/Algorithms:
1. Create an uninitialized array and print ‘n’ natural numbers in the created array:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: adding +1 to the position and the value and moving the value to array
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
58
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
4. Fibonacci series:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: adding the previous value to current number and saving into array
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
59
Step 11: print
Step 12: end
Presentation of Results:
1. Create an uninitialized array and print ‘n’ natural numbers in the created array:
INPUT:
OUTPUT:
60
2. Insert a given element in an array at the specified position:
INPUT:
OUTPUT:
61
3. Compute GCD for the given two natural numbers:
INPUT:
OUTPUT:
62
4. Compute LCM for the given two natural numbers:
INPUT:
OUTPUT:
5. Fibonacci series:
63
INPUT:
OUTPUT:
64
location in memory based on a condition. There are also conditional move instructions that allow
the program to move data between registers or memory locations based on a condition. While
these instructions are essential in assembly language programming, they can make the code more
complex and harder to read and maintain. Therefore, it is important for developers to use them
judiciously and with care.
7. Conclusions :
Conditional instructions are an important feature of assembly programming language that allow
programs to make decisions based on the current state of the program. Jump instructions enable
the program to change the execution flow by jumping to a different location in memory based on
a condition, while conditional move instructions allow data to be moved between registers or
memory locations based on a condition. While these instructions are powerful, they can also make
the code more complex and harder to maintain, so they should be used judiciously.
8. Comments
65
Laboratory 6
Title of the Laboratory Exercise: String manipulation
Aim
To develop assembly language program to perform all string operations like inserting a byte,
deleting a byte and copying a string as a sub-string
Objectives
3. Experimental Procedure
66
3.
4. Develop an assembly language program to compare two strings and print a message
“Equal” if they are equal, “Not Equal” if they are not equal.
5. Calculations/Computations/Algorithms:
1. Copy the contents of MSG1 to MSG2:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: saving the string by travelling from first to last
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
2. Copy the contents of MSG1 to MSG2 in reverse order:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: saving the string by travelling from first to last and saving from last to first
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
67
3. Copy the contents of MSG3 to MSG4 after ‘n’ th character in MSG4:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: copying the data from one string and attaching with another string
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
4. compare two strings and print equal or not:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: comparing the strings and printing equal or not
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
68
5.
6. Presentation of Results:
Input:
Output:
69
Copy the contents of MSG1 to MSG2 in reverse order:
Input:
Output :
70
7.
Input:
Output:
71
compare two strings and print equal or not:
Input:
Output:
72
7. Analysis and Discussions:
String manipulation in assembly language involves the manipulation of strings, which are
sequences of characters, typically stored in memory.
Memory addressing modes: Memory addressing modes are used to access memory locations
where strings are stored. Assembly language provides different addressing modes, such as
immediate, direct, indirect, and indexed addressing modes.
Data movement instructions: Data movement instructions are used to transfer data between
memory and registers. These instructions are crucial when manipulating strings in assembly
language because they allow the programmer to load, store, and move characters within the string.
Character encoding: It is essential to know the character encoding used in the string manipulation.
Common character encodings include ASCII, Unicode, and EBCDIC.
String manipulation in assembly language requires careful attention to detail, as errors can result
in unexpected behavior. Assembler directives, such as DB (define byte) and DW (define word), can
be used to declare and initialize strings.
8. Conclusions :
73
In conclusion, string manipulation in assembly language is a fundamental task that requires
knowledge of memory addressing modes, data movement instructions, and string processing
instructions. With the right set of instructions and careful attention to detail, programmers can
manipulate strings to perform various operations such as loading, storing, comparing, searching,
and concatenating. Assembler directives, such as DB and DW, can be used to declare and initialize
strings. It is essential to understand the character encoding used in the string manipulation, such
as ASCII or Unicode. Overall, string manipulation is a powerful tool that enables assembly language
programmers to create efficient and optimized code for various applications.
9. Comments
String manipulation allows developers to transform text data in various ways, including
concatenation, splitting, substitution, and formatting. These operations are essential for data
cleaning, normalization, and preparation in applications ranging from data analysis to natural
language processing. String manipulation techniques such as tokenization, parsing, and pattern
matching enable developers to extract meaningful information from unstructured text data. This
is crucial for tasks like parsing log files, extracting data from web pages, processing user input,
and performing linguistic analysis.
74
Laboratory 7
Title of the Laboratory Exercise: Searching an element in an array
3. Experimental Procedure
1. Write algorithm to solve the given problem
2. Translate the algorithm to assembly language code
3. Run the assembly code in GNU assembler
4. Create a laboratory report documenting the work
4. Questions
Develop an assembly language program to perform the following:
1. Searching an element in an array of ‘n’ numbers
2. Read a sentence with at least one special character and search for the special character
and print it. E.g., consider the input {youremailid@msruas.ac.in }
Output: @, .
3. To count the number of vowels and consonants in the given string
75
5. Calculations/Computations/Algorithms:
1. Searching an element in an array of ‘n’ numbers:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: creating a array and checking the element is found or not
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
2. Search for the special character and print:
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: checking with a to z alphabets and numbers remining will be into new array
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
3. count the number of vowels and consonants:
Step 1: start
Step 2 : create directory
Step 3: change directory
76
Step 4: to get program window
Step 5: checking aeiou and incr if not incr with consonants
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
6. Presentation of Results:
Input:
Output:
77
2. Search for the special character and print:
Input:
Output:
78
3. count the number of vowels and consonants:
Input:
Output:
79
7. Analysis and Discussions:
The assembly code provided above searches for an element in an array by iterating through each
element of the array and comparing it with the element being searched for. Here's a brief analysis
of the code
1. The length of the array is loaded into the ECX register, and the address of the array is loaded into
the ESI register.
2. A loop is started that will iterate through each element of the array until the entire array is
searched.
3. Inside the loop, the array length is decremented and the next element in the array is loaded into
the EAX register using the LODSD instruction.
4. The loaded element is then compared with the search element using the CMP instruction.
5. If the elements match, the program jumps to the element_found label and executes the code
there.
6. If the entire array has been searched and the element has not been found, the program jumps
to the element_not_found label and executes the code there.
Overall, this is a simple and straightforward implementation of a linear search.
One limitation of this implementation is that it can be slow for large arrays since it has to iterate
through each element one by one. To optimize this implementation, we can consider using more
advanced algorithms like binary search, which can significantly reduce the search time for large
arrays.
80
Additionally, the implementation assumes that the array is stored contiguously in memory. If the
array is stored in a more complex data structure, such as a linked list, a different approach may be
necessary.
8. Conclusions:
In conclusion, searching for an element in an array using assembly language involves iterating
through each element of the array and comparing it with the element being searched for. This can
be accomplished using a simple loop and the CMP instruction to compare elements. The code
provided above is a basic implementation of a linear search and can be modified and optimized for
specific use cases. Assembly language provides a low-level approach to programming and can be
useful in performance-critical applications where every cycle counts.
81
Laboratory 8
Title of the Laboratory Exercise: Sorting
Aim
To develop assembly language program to perform sorting using nested loop structures Objectives
3. Experimental Procedure
1. Write algorithm to solve the given problem
2. Translate the algorithm to assembly language code
3. Run the assembly code in GNU assembler
4. Create laboratory report documenting the work
4. Questions
Develop an assembly language program to perform the following
1. Arrange an array of ’n’ numbers in ascending order
5. Calculations/Computations/Algorithms:
1. Arrange an array of ’n’ numbers in ascending order
82
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: checking the array and arranging in ascending order
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: checking the array and arranging in descending order
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
83
3. Determine the second smallest number in an array
Step 1: start
Step 2 : create directory
Step 3: change directory
Step 4: to get program window
Step 5: checking the array and arranging in descending order and
getting second value to print
Step 6: compile
Step 7: link
Step 8: gdb run
Step 9: break point
Step 10: run
Step 11: print
Step 12: end
6. Presentation of Results:
1. Arrange an array of ’n’ numbers in ascending order
Input:
84
Output:
85
Input:
Output:
86
Input:
Output:
87
Sorting an array in assembly language involves iterating through the array and comparing each
element with the subsequent elements. The sorting algorithm used depends on the specific
implementation, but a common approach is the bubble sort algorithm.
For sorting in ascending order, the bubble sort algorithm works by comparing adjacent elements
and swapping them if they are in the wrong order. This process is repeated until the entire array
is sorted.
For sorting in descending order, a similar approach is used, but the comparison and swapping
logic is reversed, i.e., elements are swapped if they are in the wrong order for descending sorting.
The implementation of the sorting algorithm in assembly language involves using registers to hold
the array elements and a loop counter to iterate through the array. The sorting logic is typically
implemented using conditional jump instructions such as JZ, JNZ, JG, JL, etc., along with register-
based arithmetic operations.
Overall, sorting an array in assembly language requires a good understanding of the specific
architecture and instruction set of the processor being used. The implementation can be complex
and time-consuming, but it can also be highly optimized for performance.
8. Conclusions :
Sorting an array in assembly language involves using a sorting algorithm, such as bubble sort, and
implementing it using register-based arithmetic operations and conditional jump instructions. The
implementation can be complex, but it can also be highly optimized for performance. Sorting in
ascending and descending order requires a similar approach but with reversed comparison and
swapping logic. A good understanding of the processor architecture and instruction set is essential
for efficient implementation.
88