0% found this document useful (0 votes)
2 views5 pages

CAO Algorithm

The document outlines various arithmetic and logical operations for 8-bit and 16-bit numbers, including addition, subtraction, multiplication, and division, along with BCD variations. It also describes methods for searching, sorting arrays in ascending and descending order, and finding the largest and smallest elements in an array. Each operation includes step-by-step instructions on how to implement it using registers and memory locations.

Uploaded by

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

CAO Algorithm

The document outlines various arithmetic and logical operations for 8-bit and 16-bit numbers, including addition, subtraction, multiplication, and division, along with BCD variations. It also describes methods for searching, sorting arrays in ascending and descending order, and finding the largest and smallest elements in an array. Each operation includes step-by-step instructions on how to implement it using registers and memory locations.

Uploaded by

Lekshmi M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

8 Bit Addition

Load the first 8-bit number into the accumulator (A).

Load the second 8-bit number into another register (e.g., B).

Add the contents of the register to the accumulator using the ADD instruction.

The accumulator now contains the 8-bit sum.

Check the carry flag for any overflow (if carry is generated).

Store the result (and carry if needed) to memory location.

2.16 bit Addition

Load the first 16-bit number into a register pair (for example, HL pair).

Load the second 16-bit number into another register pair (for example, DE pair).

Move one 16-bit number into HL pair (if not already loaded).

Use the DAD instruction to add the second register pair to HL (HL = HL + DE).

The result of addition is stored in the HL register pair.

Check the Carry flag (CY) for overflow beyond 16 bits.

Store the result from HL pair into memory location if required.

3.8 Bit Substraction

Load the first 8-bit number (minuend) into the Accumulator (A).

Load the second 8-bit number (subtrahend) into another register (e.g., B).

Execute the SUB B instruction (A = A − B).

The result of subtraction is stored in the Accumulator.

Check the Carry flag (CY) to determine borrow (CY = 1 indicates borrow).

Check other flags (Zero, Sign, Parity) if required.

Store the result from the Accumulator into memory location if needed.
[Link] Substraction

Load the BCD minuend (first number) into the Accumulator (A).

Load the BCD subtrahend (second number) into another register (e.g., B).

Subtract the second number from the accumulator using SUB B (A = A − B).

Check the Auxiliary Carry and Carry flags to detect borrow from lower and upper nibble.

If borrow occurs, adjust the result by subtracting 06H from the lower nibble or 60H from the
upper nibble as required (BCD correction).

The corrected BCD result is stored in the Accumulator.

Store the final BCD result into memory location if required.

5.8 Bit Multiplication

Load the first 8-bit number (multiplicand) into a register (e.g., B).

Load the second 8-bit number (multiplier) into another register (e.g., C).

Clear the Accumulator (A = 00H) to store the result.

Add the multiplicand to the Accumulator using ADD B.

Decrement the multiplier register (C) and check if it is zero.

Repeat steps 4 and 5 until the multiplier becomes zero.

The final product is stored in the Accumulator (lower byte) and carry (if any) indicates higher
byte.

6. BCD Multiplication

Load the first BCD number (multiplicand) into a register (e.g., B).

Load the second BCD number (multiplier) into another register (e.g., C).

Clear the Accumulator (A = 00H) to store the BCD product.

Add the multiplicand to the Accumulator using ADD B.


Use DAA (Decimal Adjust Accumulator) instruction to correct the result into valid BCD form.

Decrement the multiplier register (C) and check if it becomes zero.

Repeat steps 4–6 until the multiplier becomes zero.

Store the final BCD result from the Accumulator (and higher byte if any) into memory.

7. 8 Bit Division

Load the 8-bit dividend into the Accumulator (A).

Load the 8-bit divisor into another register (e.g., B).

Clear a register (e.g., C = 00H) to store the quotient.

Subtract the divisor from the Accumulator using SUB B.

If no borrow occurs (Carry flag = 0), increment the quotient register (C) and repeat step 4.

If borrow occurs (Carry flag = 1), stop subtraction.

The quotient is stored in register C and the remainder is stored in the Accumulator.

8. Searching for an element in an array

Store the array elements in consecutive memory locations.

Load the starting address of the array into a register pair (e.g., HL).

Load the element to be searched (key) into the Accumulator (A).

Compare the Accumulator with the current array element using CMP M.

If equal, the element is found; branch to “Element Found” routine.

If not equal, increment the HL register to point to the next array element and repeat step 4.

Continue until the end of the array; if not found, branch to “Element Not Found” routine.

[Link] in Ascending Order


Store the array elements in consecutive memory locations.
Load the starting address of the array into a register pair (e.g., HL).

Compare the current element with the next element using MOV A, M and CMP instructions.

If the current element is greater than the next element, swap them.

Move to the next element and repeat steps 3–4 until the end of the array.

Repeat steps 2–5 for all elements until the entire array is sorted.

The array is now arranged in ascending order in memory.

10. Sorting in Desending Order

Store the array elements in consecutive memory locations.

Load the starting address of the array into a register pair (e.g., HL).

Compare the current element with the next element using MOV A, M and CMP instructions.

If the current element is less than the next element, swap them.

Move to the next element and repeat steps 3–4 until the end of the array.

Repeat steps 2–5 for all elements until the entire array is sorted.

The array is now arranged in descending order in memory.

[Link] the Largest Element of an Array

Store the array elements in consecutive memory locations.

Load the first element of the array into the Accumulator (A) as the initial largest value.

Load the starting address of the next element into a register pair (e.g., HL).

Compare the Accumulator with the current element using CMP M.

If the current element is greater than the value in the Accumulator, move it into the
Accumulator.
Increment the HL register to point to the next element and repeat steps 4–5 until the end of
the array.

The Accumulator now contains the largest element of the array.

[Link] the Smallest Element of an Array

Store the array elements in consecutive memory locations.

Load the first element of the array into the Accumulator (A) as the initial smallest value.

Load the starting address of the next element into a register pair (e.g., HL).

Compare the Accumulator with the current element using CMP M.

If the current element is smaller than the value in the Accumulator, move it into the
Accumulator.

Increment the HL register to point to the next element and repeat steps 4–5 until the end of
the array.

The Accumulator now contains the smallest element of the array

You might also like