8085 program to find the set bit of accumulator - GeeksforGeeks

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:
Without using Carry FlagUsing Carry Flag
Without using Carry Flag
Using Carry Flag
Method 1: Without using Carry Flag
Algorithm –
Initialize a counter in the form of register CLoad a register B with 01 to AND with accumulator and check for set bitAND the content of accumulator with register BIf the result of AND operation is non-zero, then the final result is obtainedIf the result is zero, increment counter CAccumulator content is shifted right using RRC without affecting carry flagThe search for next bit is carried out again
Initialize a counter in the form of register C
Load a register B with 01 to AND with accumulator and check for set bit
AND the content of accumulator with register B
If the result of AND operation is non-zero, then the final result is obtained
If the result is zero, increment counter C
Accumulator content is shifted right using RRC without affecting carry flag
The search for next bit is carried out again
Program –
Starting Address of Program -> 3000H
Explanation –
LXI SP, 5000H is used to initialize the stack pointer at a higher location such that it doesn’t coincide with the program counterMVI C, 01H stores the value of counter that will store the final result at end of executionMVI B, 01H stores 01H to register BANA B Logical AND the contents of register B with right-most bit of accumulator to check for set bitJNZ 3010H jumps to Halt instruction if the AND operation results in non-zero value; it implies the bit is set and final result is obtainedINR C increments the bit counter if AND operation results in zero value; implying search for set bit needs to be continuedRRC is used to right shift the accumulator without affecting the carry flagJMP 3007H jumps unconditionally to check for next bit of accumulatorHLT halts the execution of program flow
LXI SP, 5000H is used to initialize the stack pointer at a higher location such that it doesn’t coincide with the program counter
MVI C, 01H stores the value of counter that will store the final result at end of execution
MVI B, 01H stores 01H to register B
ANA B Logical AND the contents of register B with right-most bit of accumulator to check for set bit
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
INR C increments the bit counter if AND operation results in zero value; implying search for set bit needs to be continued
RRC is used to right shift the accumulator without affecting the carry flag
JMP 3007H jumps unconditionally to check for next bit of accumulator
HLT halts the execution of program flow
Method 2: Using Carry Flag
Algorithm –
Initialize a counter in the form of register CAccumulator content is shifted right through carry flagThe counter is incrementedIf the carry flag (the right-most bit of accumulator) is set, the final result is obtainedIf the carry flag is reset (CY=0), iteration is continued for the next bit
Initialize a counter in the form of register C
Accumulator content is shifted right through carry flag
The counter is incremented
If the carry flag (the right-most bit of accumulator) is set, the final result is obtained
If the carry flag is reset (CY=0), iteration is continued for the next bit
Program –
Starting Address of Program -> 3000H
Explanation –
LXI SP, 5000H is used to initialize the stack pointer at a higher location such that it doesn’t coincide with the program counterMVI C, 00H stores the value of counter that will store the final result at end of executionRAR rotates the accumulator to right through Carry flag such that after the instruction Carry flag contains the right-most bit of accumulatorINR C Increments the counterJNC 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 setHLT halts the execution of program flow
LXI SP, 5000H is used to initialize the stack pointer at a higher location such that it doesn’t coincide with the program counter
MVI C, 00H stores the value of counter that will store the final result at end of execution
RAR rotates the accumulator to right through Carry flag such that after the instruction Carry flag contains the right-most bit of accumulator
INR C Increments the counter
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
HLT halts the execution of program flow
microprocessor
system-programming
Computer Organization & Architecture
microprocessor
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Architecture of 8085 microprocessor
Computer Organization and Architecture | Pipelining | Set 2 (Dependencies and Data Hazard)
Direct Access Media (DMA) Controller in Computer Architecture
Pin diagram of 8086 microprocessor
General purpose registers in 8086 microprocessor
8085 program to add two 16 bit numbers
I2C Communication Protocol
Difference between SRAM and DRAM
Difference between RISC and CISC processor | Set 2
Computer Organization | Different Instruction Cycles