GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPUTER ORGANIZATION & ARCHITECTURE

Trace shift-and-test population-count loops

3 questions solved the same way, newest first. Back to Computer Organization & Architecture

How these are solved

Track the low bit through shift/test or rotate-through-carry, increment the accumulator on a one, and follow loop termination. 1999 asks a concrete trace from 5 and a symbolic function of n;

2 marks · MCQ
Report issue

Counting set bits with carry rotations

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

2 marks · Descriptive
Report issue

Trace the population-count loop

Instructions & Addressing Modes

Question 17(a), GATE 1999: For the assembly below with initial (R1,R2,R3)=(5,0,0), find R1 and R3 on reaching Z:
X: CMP R1,0
   JZ Z
   MOV R2,R1
   SHR R1
   SHL R1
   CMP R2,R1
   JZ Y
   INC R3
Y: SHR R1
   JMP X
Z: ...
CMP sets comparison flags; JZ jumps on equality; MOV copies its second operand into its first; SHR/SHL shift by one bit.
Question 17(a), GATE 1999: For the assembly below with initial (R1,R2,R3)=(5,0,0), find R1 and R3 on reaching Z:
X: CMP R1,0
   JZ Z
   MOV R2,R1
   SHR R1
   SHL R1
   CMP R2,R1
   JZ Y
   INC R3
Y: SHR R1
   JMP X
Z: ...
CMP sets comparison flags; JZ jumps on equality; MOV copies its second operand into its first; SHR/SHL shift by one bit.
Question 17(a), GATE 1999: For the assembly below with initial (R1,R2,R3)=(5,0,0), find R1 and R3 on reaching Z:
X: CMP R1,0
   JZ Z
   MOV R2,R1
   SHR R1
   SHL R1
   CMP R2,R1
   JZ Y
   INC R3
Y: SHR R1
   JMP X
Z: ...
CMP sets comparison flags; JZ jumps on equality; MOV copies its second operand into its first; SHR/SHL shift by one bit.
Tap to enlarge
Answer & solution

Answer: R1=0, R3=2 (no official key was published)

The SHR followed by SHL clears the least significant bit. Comparing the result against saved R2 detects whether that bit was 1. If so, increment R3. The SHR at Y then advances to the next bit. With nonnegative logical shifts the trace is:

Entry R1 | R2 after MOV | after SHR,SHL | R3 after test | R1 after Y
5 (101)  | 5            | 4 (100)       | 1             | 2
2 (010)  | 2            | 2 (010)       | 1             | 1
1 (001)  | 1            | 0 (000)       | 2             | 0

At the next X, R1=0 makes JZ Z branch before another iteration. Hence final R1=0 and R3=2. R2 happens to remain 1, though it is not requested. The two right shifts in the body do not skip two input bits because the intervening left shift restores the higher-bit positions.

Original paper, page 8

3 marks · Descriptive
Report issue

Prove population-count behavior

Instructions & Addressing Modes

Question 17(b), GATE 1999: For the same assembly with initial (R1,R2,R3)=(n,0,0), determine R3 on reaching Z:
X: CMP R1,0
   JZ Z
   MOV R2,R1
   SHR R1
   SHL R1
   CMP R2,R1
   JZ Y
   INC R3
Y: SHR R1
   JMP X
Z: ...
CMP sets comparison flags; JZ jumps on equality; MOV copies its second operand into its first; SHR/SHL shift by one bit.
Question 17(b), GATE 1999: For the same assembly with initial (R1,R2,R3)=(n,0,0), determine R3 on reaching Z:
X: CMP R1,0
   JZ Z
   MOV R2,R1
   SHR R1
   SHL R1
   CMP R2,R1
   JZ Y
   INC R3
Y: SHR R1
   JMP X
Z: ...
CMP sets comparison flags; JZ jumps on equality; MOV copies its second operand into its first; SHR/SHL shift by one bit.
Question 17(b), GATE 1999: For the same assembly with initial (R1,R2,R3)=(n,0,0), determine R3 on reaching Z:
X: CMP R1,0
   JZ Z
   MOV R2,R1
   SHR R1
   SHL R1
   CMP R2,R1
   JZ Y
   INC R3
Y: SHR R1
   JMP X
Z: ...
CMP sets comparison flags; JZ jumps on equality; MOV copies its second operand into its first; SHR/SHL shift by one bit.
Tap to enlarge
Answer & solution

Answer: R3=popcount(n), the number of 1 bits in n. (no official key was published)

For nonnegative integer n (or an unsigned fixed-width register with logical SHR), write current R1=2k+b, where b∈{0,1}. SHR then SHL yields 2k, so CMP detects inequality exactly when b=1; R3 therefore increases by b. The SHR at Y sets R1=k. After t iterations, R1=floor(n/2t) and R3 equals the number of ones in the t low-order bits removed from n. This invariant holds initially and is preserved by the decomposition 2k+b. Termination occurs when R1=0, after all significant bits have been examined, so R3 is the binary population count of n. For n=0 the initial branch exits immediately and R3=0. For negative signed inputs the outcome requires a specified word width and right-shift convention; arithmetic SHR is not covered by this nonnegative-integer proof.

Original paper, page 8