Write a short assembly subroutine to count the number of one
Solution
A)
Lebel Mnemonics Comment
MVI NUM_ONES, 00H ; Initialize NUME_ONES register with all zeros
MOV A, TEMP1 ; Copy the data from TEMP1 to accumulator A.
RRC ;Rotate accumulator right
JC NEXT1 ; IF carry is 1 then jump to leble NEXT1
NEXT1 INR NUM_ONES ; Increment data of NUM_ONES by one
RRC ;Rotate accumulator right
JC NEXT2 ; IF carry is 1 then jump to leble NEXT2
NEXT2 INR NUM_ONES ; Increment data of NUM_ONES by one
RRC ; Rotate accumulator right
JC NEXT3 ; IF carry is 1 then jump to leble NEXT3
NEXT3 INR NUM_ONES ; Increment data of NUM_ONES by one
RRC ;Rotate accumulator right
JC NEXT4 ; IF carry is 1 then jump to leble NEXT4
NEXT4 INR NUM_ONES ; Add data of NUM_ONES to carry bit
HLT ; Halt the execution of subroutine
NOTE: This program can even be smaller if there is another register given for loop operation.
Acumulator and Carry register are assumed to be given because for ALU operation these are necessary register.
Mnemonics commands are choosen on the basis of 8085 microprocessor command.
B)
In question it is not given for which processor we have to update program counter.
The answer is written here for 8085 micro processor.
Objective: First load the PC with ABCD H, then load it again with 00 H.
It is very simple to load PC using a simple command PCHL in 8085, as given below...
Mnemonics Comment
LXI H, 0xABCD H ; Load the HL register pair direct with 0xADCD H
PCHL ; Copy the content of HL register pair to program counter (PC)
LXI H, 00H ; Load HL register pair with all zeros
PCHL ;Copy the content of HL register pair to program counter (PC)
HLT

