0% found this document useful (0 votes)
2 views

Algorithm and flowcharts for Practicals

The document outlines various assembly language programs (ALPs) for performing arithmetic operations (addition, subtraction, multiplication, division), data manipulation (reverse order transfer, block exchange), and counting occurrences (even/odd numbers, specific values, zeros) using an 8-bit architecture. Each program includes a step-by-step instruction set detailing how to manipulate registers and memory locations to achieve the desired outcomes. The document serves as a comprehensive guide for implementing basic algorithms in assembly language.

Uploaded by

themanishawari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm and flowcharts for Practicals

The document outlines various assembly language programs (ALPs) for performing arithmetic operations (addition, subtraction, multiplication, division), data manipulation (reverse order transfer, block exchange), and counting occurrences (even/odd numbers, specific values, zeros) using an 8-bit architecture. Each program includes a step-by-step instruction set detailing how to manipulate registers and memory locations to achieve the desired outcomes. The document serves as a comprehensive guide for implementing basic algorithms in assembly language.

Uploaded by

themanishawari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ALP to find addition of two 8-bit

numbers

1. Start the program.


2. Load the address 4000H into the HL
register pair.
(Instruction: LXI H, 4000)
3. Copy the value from memory at
address 4000H (pointed by HL) into the
accumulator (A).
(Instruction: MOV A, M)
4. Increment the HL register to point to
the next memory location (4001H).
(Instruction: INX H)
5. Add the value at the new memory
location (4001H) to the accumulator
(A).
(Instruction: ADD M)
6. Store the result from the accumulator
(A) into memory address 4002H.
(Instruction: STA 4002)
7. Halt the program.
(Instruction: HLT)
8. End.
ALP to find Absolute Difference of
two 8-bit numbers

1. Start the program.


2. Load the address C030H into the HL
register pair.
Instruction: LXI H, C030H
3. Move the data at memory address
C030H (pointed by HL) into register B.
Instruction: MOV B, M
4. Increment the HL register pair to point
to the next memory address (C031H).
Instruction: INX H
5. Move the data at memory address
C031H (pointed by HL) into the
accumulator (A).
Instruction: MOV A, M
6. Subtract the value in register B from
the value in the accumulator (A).
Instruction: SUB B
7. If the result of subtraction is positive,
jump to the label VP.
Instruction: JP CO0DH
8. If the result is negative, take the 1’s
complement of the value in the
accumulator (A).
Instruction: CMA
9. Add 01H to the accumulator (A) to get
the 2’s complement (absolute value).
Instruction: ADI 01H
10. VP Label: Increment the HL register
pair to point to the next memory
address (C032H).
Instruction: INX H
11. Move the result from the accumulator
(A) into the memory location C032H.
Instruction: MOV M, A
12. Halt the program.
Instruction: HLT
13.End the program.
ALP to find Multiplication of two 8-
bit numbers
1) Start the program.
2) Clear the A register (make it 0).
(This is done to prepare for addition.)
3) Set the B register to 0.
(This will store any carry during
multiplication.)
4) Point the HL register to the starting
address (C030).
5) Load the first number from memory
(C030) into the C register.
6) Move HL to the next address (C031).
7) Repeat the following steps (loop) until
the C register (counter) becomes 0:
 Add the number at the current HL
address (C031) to the A register.
 If there’s a carry (sum exceeds 255):
o Increase the B register by 1.
 Decrease the C register by 1.
8) After the loop ends, store the final
result:
 Store the value in the A register
(lower byte of the result) to memory
(C032).
 Move HL to the next address (C033).
 Store the value in the B register
(upper byte of the result) to memory
(C033).
9) Stop the program.
ALP to find Division of two 8-bit o Instruction: MOV M, B
numbers 10. Increment the HL register to point
to C033 (next memory location for
1. Start the program. the remainder).
2. Clear the A register (set it to 0). o Instruction: INX H

o Instruction: XRA A 11. Store the remainder (value in A


(This is done to reset the A register) into memory location
register to 0.) C033.
3. Set the B register to 0 (to count the o Instruction: MOV M, A

quotient). 12. Stop the program.


o Instruction: MOV B, A o Instruction: HLT

4. Load the address C030 into the HL 13. End.


register pair (to point to the
memory location of the dividend).
o Instruction: LXI H, C030
5. Move the value at C030 (dividend)
into the A register.
o Instruction: MOV A, M
6. Increment the HL register to point
to C031 (divisor location).
o Instruction: INX H
7. Start the loop (Label PP):
o Compare the dividend (A) with
the divisor (M).
 Instruction: CMP M
(If the dividend is smaller than
the divisor, go to step 8.)
o If the dividend is larger or equal
to the divisor:
 Subtract the divisor (M) from
the dividend (A).
Instruction: SUB M
 Increment the B register by 1
(to count the quotient).
Instruction: INR B
 Repeat the loop.
Instruction: JMP C007

8. End the loop (Label VP):


o Exit the loop when the dividend
is smaller than the divisor.
9. Store the quotient (value in B
register) into memory location
C032.
ALP to transfer data in Reverse
Order
1) Start the program.
2) Store the block length (0C in hex, 12 in
decimal) into the C register.
Instruction: MVI C, 0C
3) Load the source's last memory location
(C08B) into the HL register pair.
Instruction: LXI H, C08B
4) Load the destination's starting memory
location (C180) into the DE register pair
Instruction: LXI D, C180
5) Start the loop:
Repeat the following steps until the
counter (C) becomes 0:
 Move the data from the memory
location pointed to by HL into the
accumulator.
Instruction: MOV A, M
 Store the data from the accumulator
into the memory location pointed to
by DE.
Instruction: STAX D
 Decrement the HL register to move
to the previous memory location
(source).
Instruction: DCX H
 Increment the DE register to move
to the next memory location
(destination).
Instruction: INX D
 Decrement the counter (C) by 1.
Instruction: DCR C
6) End the loop when the counter (C)
becomes 0.
Instruction: JNZ to loop back if C ≠ 0
7) Stop the program.
Instruction: HLT
8) End.
 Instruction: DCR C
ALP to exchange contents of two 6. End the loop when the counter (C)
becomes 0.
blocks
o Instruction: JNZ to loop back if C ≠ 0

1. Start the program. 7. Stop the program.


o Instruction: HLT
2. Set the length of the block (0B in hex,
11 in decimal) into the C register as a 8. End.
counter.
o Instruction: MVI C, 0B
3. Load the starting address of the first
block (C060) into the HL register pair.
o Instruction: LXI H, C060
4. Load the starting address of the second
block (C100) into the DE register pair.
o Instruction: LXI D, C100
5. Start the loop to exchange data:
o Repeat the following steps until the
counter (C) becomes 0:
1. Load the data from the memory
location pointed to by DE (second
block) into the accumulator (A).
 Instruction: LDAX D
2. Move the data from the memory
location pointed to by HL (first block)
into the B register.
 Instruction: MOV B, M
3. Store the data from the accumulator
(A) into the memory location
pointed to by HL (first block).
 Instruction: MOV M, A
4. Store the data from the B register
into the memory location pointed to
by DE (second block).
 Instruction: MOV A, B
 Instruction: STAX D
5. Increment the HL register to point to
the next memory location in the first
block.
 Instruction: INX H
6. Increment the DE register to point to
the next memory location in the
second block.
 Instruction: INX D
7. Decrement the counter (C) by 1.
ALP to find count of Even as well as Instruction: JNC NEXT (If CY = 0,
Odd number skip to even counter step).
 Increment Odd Counter:
1) Start the Program Instruction: INR D
Initialize the program execution. c. Increment Even Counter (if CY is not
2) Set the Length of the Block set): Instruction: INR E
d. Move to the Next Memory Location
Set the length of the data block (0D in
hex, 13 in decimal) into the C register  Increment HL to point to the next
as a counter. memory location.
Instruction: MVI C, 0D Instruction: INX H
3) Set Counters for Odd and Even e. Update the Counter
Numbers
 Decrement the C register by 1.
Set D register to 00 (Odd counter).
Instruction: DCR C
Set E register to 00 (Even counter).
 If C ≠ 0, go back to the start of the
Instruction: MVI D, 00 loop.
Instruction: MOV E, D Instruction: JNZ VP
4) Load the Starting Address of the Block 6) Store the Results
 Once the loop ends, store the odd
Load the starting address of the block
(C060) into the HL register pair. count (D) and even count (E) in
memory:
Instruction: LXI H, C060
 Store Odd Count at C06D:
5) Start the Loop to Process the Data Instruction: MOV M, D
 Repeat the following steps for all the
numbers in the block until the  Store Even Count at C06E:
counter (C) becomes 0: Instruction: INX H
Instruction: MOV M, E
a. Load the Data
7) Stop the Program
 Load the number at the memory
location pointed to by HL into the 8) End the program execution.
accumulator (A). 9) Instruction: HLT
Instruction: MOV A, M
b. Check if Odd or Even
 Perform a Right Rotate operation
on the accumulator (A).
Instruction: RRC
 If the carry flag (CY) is set,
increment the odd counter (D).
ALP that counts occurrence of given o If equal, increment the counter in C:
number Instruction: INR C

1. Start the Program c. Increment HL to Point to the Next


Memory Location
 Initialize the program execution.
o Move to the next memory location in
2. Set the Initial Counter Value the block by incrementing HL.
o Instruction: INX H
 Set the C register to 00 to store the
count of occurrences of data AD H. d. Decrement the Counter in B
 Instruction: MVI C, 00
o Decrement the value of the B register
3. Load the Data to Be Searched to keep track of the length of the block.
o Instruction: DCR B
 Load the data AD H into the o If the B register is not zero, go back to
accumulator (A). the start of the loop:
 Instruction: MVI A, AD Instruction: JNZ BACK

4. Load the Starting Address of the Block 6. Store the Count of Occurrences

 Load the starting address of the  Move the value in C (count of


memory block (C030) into the HL occurrences) into the accumulator
register pair. (A).
 Instruction: LXI H, C02F Instruction: MOV A, C

5. Start the Loop to Search for Data  Store the value of A into memory
location D000.
 Repeat the following steps until the Instruction: STA D000
counter in the B register becomes 0:
7. Stop the Program
a. Load the Data from Memory
 End the program execution.
o Move the memory data at the location Instruction: HLT
pointed by HL into the B register.
o Instruction: MOV B, M

b. Compare with AD H

o Compare the contents of the


accumulator (A) with the memory data
(B).
o Instruction: CMP M
o If not equal (zero flag not set), skip to
the next memory location:
Instruction: JNZ NEXT
ALP to find smallest as well as o Compare the contents of the
greatest number using linear search accumulator (A) with the smallest
value in the E register.
1. Start the Program o Instruction: CMP E
o Begin program execution. o If A < E, move the accumulator
2. Initialize the Counter contents to E.
o Store the length of the block (0B) in o Instruction: MOV E, A
the C register. 8. Continue the Loop
o Instruction: MVI C, 0B o Decrement the counter in C and
3. Load Starting Address of Block check if it is zero.
o Load the starting address of the o Instruction: DCR C
block (C030) into the HL register o If not zero, jump back to the start of
pair. the loop.
o Instruction: LXI H, C030 o Instruction: JNZ VP
4. Assume Initial Values for Greatest and 9. Store the Results
Smallest o Store the greatest value (D) into
o Move the first memory data to the D memory after the block.
register (greatest). o Instruction: MOV M, D
o Instruction: MOV D, M o Increment HL to point to the next
o Move the first memory data to the E memory location.
register (smallest). o Instruction: INX H
o Instruction: MOV E, M o Store the smallest value (E) into
5. Start the Loop to Compare Data memory after the block.
o Decrement the counter in C to o Instruction: MOV M, E
reduce the length. 10. Stop the Program
o Instruction: DCR C
o Increment HL to point to the next
 End the program execution.
memory location.  Instruction: HLT
o Instruction: INX H
o Move the next memory content into
the accumulator (A).
o Instruction: MOV A, M
6. Compare and Update Greatest
o Compare the contents of the
accumulator (A) with the greatest
value in the D register.
o Instruction: CMP D
o If A > D, move the accumulator
contents to D.
o Instruction: JC SML
o Label: SML
7. Compare and Update Smallest
ALP to count the zero in given  If the counter (C) is not zero, repeat
number the loop to process the next bit.
 Instruction: JNZ BACK
1. Start the Program
7. Store the Result
 Begin execution of the program.
 Copy the zero count from the B
2. Set Counter to 8 register into the accumulator to
store it in memory.
 Load the value 08H into the C  Instruction: MOV A, B
register because we need to check  Store the result (zero count) into
all 8 bits of the number. memory location C500H.
 Instruction: MVI C, 08H  Instruction: STA C500H

3. Initialize Zero Counter 8. Stop the Program

 Set the B register to 00H to store  Halt program execution.


the count of zeros found in the  Instruction: HLT
number.
 Instruction: MVI B, 00H

4. Load 8-Bit Number into Accumulator


 Load the 8-bit number from
memory location C400H into the
accumulator to process it bit by bit.
 Instruction: LDA C400H

5. Start the Loop to Count Zeros


 Step 1: Rotate the number in the
accumulator right (RRC) to check its
least significant bit (LSB).
o Instruction: RRC
 Step 2: If the LSB is 1, skip the
counting step and move to the next
bit.
o Instruction: JC NEXT
 Step 3: If the LSB is 0, increment the
zero counter in the B register.
o Instruction: INR B

6. Decrement the Bit Counter


 Decrease the counter in the C
register by 1 because one bit is
processed.
 Instruction: DCR C
 Instruction: DCR B

3. Inner Loop Setup

 Move data from memory into B


register to start comparing values.
 Instruction: MOV B, M
 Move the next data into the C register
for comparison.
 Instruction: MOV C, M

4. Compare Values

 Compare the contents of the


accumulator (A) and register C.
 Instruction: CMP C
 If data in A is larger than C, swap the
values.

5. Swap Data (if needed)

 If the carry flag is set (data in A is


larger), store the smaller value in
memory and move the larger value to
the next position.
 Instructions:
o MOV D, M
o MOV M, A

6. Decrement Length Counter


ALP to arrange data in ascending  The length counter (C) is
order decremented for each pass through
the inner loop.
1. Initialize Memory Pointer  Instruction: DCR C
 HL register pair is loaded with the 7. Repeat the Inner Loop
starting address of the block (CO2F) to
point to the memory location for  Check if the length counter is zero; if
sorting. not, go back to the inner loop start.
 Instruction: LXI H, CO2F  Instruction: JNZ C008

2. Decrement the Length Counter 8. Repeat the Outer Loop

 The B register, which stores the length  Decrement B (outer loop counter) and
of the block, is decremented by 1 for repeat the sorting process for the
loop control. next pass.
 Instruction: DCR B

9. End of Sorting

 Once all values are sorted, the HL


pointer is reset to CO2F, and the
sorted values remain in the same
memory locations.

10. Stop Execution

 Program ends with the HLT


instruction.

You might also like