Related Articles

# 8085 program to find the set bit of accumulator

• Last Updated : 13 May, 2019

Problem – All bits of an accumulator are 0 except a single bit which is 1. Write an assembly language program using 8085 to determine which bit of the accumulator is 1. The result should be a decimal number from 1 to 8 and is required to be stored in register C.

Examples –

```Example 1 : Accumulator Content is 10H (Hex)

Accumulator Content -> 0 0 0 1 0 0 0 0
Result after execution -> Register C : 5

Example 2 : Accumulator content is 02H (Hex)

Accumulator Content -> 0 0 0 0 0 0 1 0
Result after execution -> Register C : 2

```

Solution –
The problem can be solved through multiple methods. Here, two approaches have been discussed:

1. Without using Carry Flag
2. Using Carry Flag

Method 1: Without using Carry Flag

Algorithm –

1. Initialize a counter in the form of register C
2. Load a register B with 01 to AND with accumulator and check for set bit
3. AND the content of accumulator with register B
4. If the result of AND operation is non-zero, then the final result is obtained
5. If the result is zero, increment counter C
6. Accumulator content is shifted right using RRC without affecting carry flag
7. The search for next bit is carried out again

Program –

Starting Address of Program -> 3000H

3000LXI SP, 5000HInitialize Stack Pointer
3003MVI C, 01HC <- 01H
3005MVI B, 01HB <- 01H
3007ANA BAND A with B
3008JNZ 3010HJump at Zero Flag reset (Z=1) to location 3010H
300BINR CC <- C+1
300CRRCRotate accumulator right without Carry
3010HLTHalt Execution

Explanation –

1. LXI SP, 5000H is used to initialize the stack pointer at a higher location such that it doesn’t coincide with the program counter
2. MVI C, 01H stores the value of counter that will store the final result at end of execution
3. MVI B, 01H stores 01H to register B
4. ANA B Logical AND the contents of register B with right-most bit of accumulator to check for set bit
5. JNZ 3010H jumps to Halt instruction if the AND operation results in non-zero value; it implies the bit is set and final result is obtained
6. INR C increments the bit counter if AND operation results in zero value; implying search for set bit needs to be continued
7. RRC is used to right shift the accumulator without affecting the carry flag
8. JMP 3007H jumps unconditionally to check for next bit of accumulator
9. HLT halts the execution of program flow

Method 2: Using Carry Flag

Algorithm –

1. Initialize a counter in the form of register C
2. Accumulator content is shifted right through carry flag
3. The counter is incremented
4. If the carry flag (the right-most bit of accumulator) is set, the final result is obtained
5. If the carry flag is reset (CY=0), iteration is continued for the next bit

Program –

Starting Address of Program -> 3000H

3000LXI SP, 5000HInitialize Stack Pointer
3003MVI C, 00HC <- 00H
3005RARRotate accumulator right through Carry
3006INR CC <- C+1
300AHLTHalt Execution

Explanation –

1. LXI SP, 5000H is used to initialize the stack pointer at a higher location such that it doesn’t coincide with the program counter
2. MVI C, 00H stores the value of counter that will store the final result at end of execution
3. RAR rotates the accumulator to right through Carry flag such that after the instruction Carry flag contains the right-most bit of accumulator
4. INR C Increments the counter
5. JNC 3005H jump to location 3005H if Carry flag is not set, that is the right-most bit of accumulator is 0. It doesn’t execute if the Carry flag is set
6. HLT halts the execution of program flow

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up