8085 program to find the set bit of accumulator Last Updated : 13 May, 2019 Comments Improve Suggest changes 3 Likes Like Report 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 Flag Using Carry Flag Method 1: Without using Carry Flag Algorithm - 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 MEMORY ADDRESS MNEMONICS COMMENT 3000 LXI SP, 5000H Initialize Stack Pointer 3003 MVI C, 01H C <- 01H 3005 MVI B, 01H B <- 01H 3007 ANA B AND A with B 3008 JNZ 3010H Jump at Zero Flag reset (Z=1) to location 3010H 300B INR C C <- C+1 300C RRC Rotate accumulator right without Carry 300D JMP 3007H Unconditional jump to location 3007H 3010 HLT Halt Execution Explanation - 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 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 MEMORY ADDRESS MNEMONICS COMMENT 3000 LXI SP, 5000H Initialize Stack Pointer 3003 MVI C, 00H C <- 00H 3005 RAR Rotate accumulator right through Carry 3006 INR C C <- C+1 3007 JNC 3005H Jump to location 3005H if CY=0 300A HLT Halt Execution Explanation - 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 Create Quiz Comment P PratikBasu Follow 3 Improve P PratikBasu Follow 3 Improve Article Tags : Computer Organization & Architecture system-programming microprocessor Explore Basic Computer InstructionsWhat is a Computer? 6 min read Issues in Computer Design 1 min read Difference between assembly language and high level language 2 min read Addressing Modes in 8086 7 min read Difference between Memory based and Register based Addressing Modes 4 min read Von Neumann Architecture 5 min read Harvard Architecture 3 min read Interaction of a Program with Hardware 3 min read Simplified Instructional Computer (SIC) 4 min read Instruction Set used in simplified instructional Computer (SIC) 1 min read Instruction Set used in SIC/XE 2 min read RISC vs CISC 3 min read Vector processor classification 5 min read Essential Registers for Instruction Execution 3 min read Single Accumulator Based CPU Organization 3 min read Stack based CPU Organization 3 min read Machine Control Instructions in Microprocessor 4 min read Very Long Instruction Word (VLIW) Architecture 3 min read Input and Output SystemsPrimary Instruction Cycles 4 min read Machine Instructions 5 min read Instruction Formats 6 min read Difference between 2-address instruction and 1-address instructions 4 min read Difference between 3-address instruction and 0-address instruction 4 min read Register content and Flag status after Instructions 3 min read Debugging a machine level program 3 min read Vector Instruction Format in Vector Processors 7 min read Vector Instruction Types 4 min read Instruction Design and FormatALU Functions and Bus Organization 5 min read Computer Arithmetic | Set - 1 5 min read Computer Arithmetic | Set - 2 4 min read 1's Complement Representation vs 2's Complement Representation 4 min read Restoring Division Algorithm For Unsigned Integer 4 min read Non-Restoring Division For Unsigned Integer 3 min read Booth's Algorithm 4 min read How the Negative Numbers are Stored in Memory? 2 min read Microprogrammed ControlMicro-Operation 3 min read Instruction Set Architecture and Microarchitecture 3 min read Program Control Instructions 4 min read Difference between CALL and JUMP instructions 5 min read Hardwired and Micro-programmed Control Unit 3 min read Implementation of Micro Instructions Sequencer 4 min read Performance of Computer in Computer Organization 5 min read Introduction to Control Unit and its Design 5 min read Computer Organization | Amdahl's law and its proof 2 min read Subroutine: Nesting and Stack memory 3 min read Different Types of RAM (Random Access Memory ) 8 min read Random Access Memory (RAM) and Read Only Memory (ROM) 8 min read 2D and 2.5D Memory organization 4 min read Input and Output OrganizationPriority Interrupts 5 min read I/O Interface (Interrupt and DMA Mode) 4 min read DMA Controller 8257/8237 2 min read Computer Organization | Asynchronous input output synchronization 7 min read Programmable peripheral interface 8255 4 min read Synchronous Data Transfer in Computer Organization 4 min read Introduction of Input-Output Processor 5 min read MPU Communication in Computer Organization 4 min read Memory Mapped I/O and Isolated I/O 5 min read Memory OrganizationIntroduction to memory and memory units 2 min read Memory Hierarchy Design and its Characteristics 6 min read Register Allocations in Code Generation 6 min read Cache Memory 4 min read Cache Organization | Set 1 (Introduction) 3 min read Multilevel Cache Organisation 6 min read Difference between RAM and ROM 7 min read Difference Between CPU Cache and TLB 4 min read Introduction to Solid-State Drive (SSD) 4 min read Read and Write operations in Memory 3 min read PipeliningInstruction Level Parallelism 5 min read Execution and Throughput 5 min read Pipelining Types and Stalling 3 min read Computer Organization and Architecture | Pipelining | Set 2 (Dependencies and Data Hazard) 6 min read Last Minute Notes Computer Organization 15+ min read Like