GATE / CS ARCHIVE

GATE CSE 2003 / QUESTION 48

Counting set bits with carry rotations

Computer Organization & Architecture · Instructions & Addressing Modes · Full GATE CSE 2003 paper

GATE 2003, Q48 2 marks · MCQ
Report issue

Instructions & Addressing Modes

Question 48, GATE 2003: A, B and C are 8-bit registers in a hypothetical processor. The program is:
MOV B,#0       ; B=0
MOV C,#8       ; C=8
Z: CMP C,#0    ; compare C with zero
JZ X          ; jump if zero flag is set
SUB C,#1      ; C=C-1
RRC A,#1      ; rotate A right through carry once
JC Y          ; jump if carry is set
JMP Z
Y: ADD B,#1   ; B=B+1
JMP Z
X:
RRC transforms A=a7...a0 and carry=c into A=ca7...a1 and carry=a0. Use the instruction effects listed for this hypothetical machine.
If A initially equals A0, what is B after execution?
A. Number of 0 bits in A0
B. Number of 1 bits in A0
C. A0
D. 8
Question 48, GATE 2003: A, B and C are 8-bit registers in a hypothetical processor. The program is:
MOV B,#0       ; B=0
MOV C,#8       ; C=8
Z: CMP C,#0    ; compare C with zero
JZ X          ; jump if zero flag is set
SUB C,#1      ; C=C-1
RRC A,#1      ; rotate A right through carry once
JC Y          ; jump if carry is set
JMP Z
Y: ADD B,#1   ; B=B+1
JMP Z
X:
RRC transforms A=a7...a0 and carry=c into A=ca7...a1 and carry=a0. Use the instruction effects listed for this hypothetical machine.
If A initially equals A0, what is B after execution?
A. Number of 0 bits in A0
B. Number of 1 bits in A0
C. A0
D. 8
Question 48, GATE 2003: A, B and C are 8-bit registers in a hypothetical processor. The program is:
MOV B,#0       ; B=0
MOV C,#8       ; C=8
Z: CMP C,#0    ; compare C with zero
JZ X          ; jump if zero flag is set
SUB C,#1      ; C=C-1
RRC A,#1      ; rotate A right through carry once
JC Y          ; jump if carry is set
JMP Z
Y: ADD B,#1   ; B=B+1
JMP Z
X:
RRC transforms A=a7...a0 and carry=c into A=ca7...a1 and carry=a0. Use the instruction effects listed for this hypothetical machine.
If A initially equals A0, what is B after execution?
A. Number of 0 bits in A0
B. Number of 1 bits in A0
C. A0
D. 8
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. The C test is before the decrement: C=8,7,...,1 each enters the body, while C=0 exits. There are exactly eight rotations.
2. Write the original bits as a7...a0. The successive carry outputs are a0,a1,...,a7. Any incoming carry sits at the top and cannot emerge until a ninth rotation.
3. Only JC Y reaches ADD B,#1. Thus B increases once for every outgoing 1 and never for an outgoing 0. Starting from zero it ends at a0+...+a7, the population count: B.

Solution: The counter C starts at eight and the loop performs exactly eight one-bit right rotations through carry. The carry tested immediately after the kth rotation is the kth original low-to-high bit of A. Newly inserted carry bits cannot reach the outgoing low position during these eight iterations. B starts at zero and is incremented exactly when the outgoing bit is one, so B is the population count of the original A, option B.

Original paper, page 7