write an alp to count number of 0's in a given 8 bit number and display result in screen by storing the result in a variable
Answers
Answer:
Explanation:
Algorithm –
Convert the decimal number in Accumulator to its binary equivalent
Rotate the digits of the binary number right without carry
Apply a loop till count is not zero to change the values of D register and count
Copy the value of D register to accumulator and store the result
Program –
MEMORY ADDRESS MNEMONICS COMMENTS
2000 MVI B 75 B ← 75
2002 MVI C 08 C ← 75
2004 MVI D 00 D ← 00
2006 MOV A, B A ← B
2007 RRC Rotate right without carry
2008 JNC 200C Jump if Not Carry
200B INR D D ← D+1
200C DCR C C ← C-1
200D JNZ 2007 Jump if Not Zero
2010 MOV A, D A ← D
2011 STA 3050 A → 3050
2014 HLT Stops execution
Explanation –
MVI B 75 moves 75 decimal number into B register
MVI C 08 moves 08 decimal number into C register, which is taken as counter as the number is of 8 bites
MVI D 00 moves 00 number into d register
MOV A, B moves the contents of B register into A (accumulator) register
RRC rotates the contents of A (which is 75 with binary equivalent 01110101) right
JNC 200C jumps to 200C address and perform the instructions written there if the carry flag is not zero
INR D increases the value of D register by adding one to its contents
DCR C decreases the value of C register by subtracting one from its contents
JNZ 2007 jumps to 2007 address and perform the instructions written there if the zero flag is not zero
MOV A, D moves the contents of B register into A register
STA 3050 store the contents of A at 3050 memory location
HLT stops execution
Answer:
The following python program counts the number of zeroes in a given 8-bit number and displays the result:
count = 0
number = input()
if(len(number)==8):
for i in number:
if(i=='0'):
count+=1
print(count)
else:
print("Invalid number")
Explanation:
- Initially, the count variable's value is zero.
- Then we take the input from the user and check whether the number has 8-bits or not.
- If the number is less than or greater than 8-bit then we print an invalid number message.
- Otherwise, we iterate over the number and check whether the bit of the number is equal to zero or not.
- If the bit is equal to zero then we increase the value of count by 1.
- Lastly, we print the count as our final output.
#SPJ2