Skip to content
Official PYQ · 2003

GATE CS 2003 Q48 — Machine Instructions and Addressing Modes

Explain with AI

Consider the following assembly language program for a hypothetical processor A,B,A, B, and CC are 8−8-bit registers. The meanings of various instructions are shown as comments.
MOV B, #0;B←0MOV C, #8;C←8Z:CMP C, #0;compare C with 0JZ X;jump to X if zero flag is setSUB C, #1;C←C−1RRC A, #1;right rotate A through carry by one bit. Thus:;If the initial values of A and the carry flag are a_7…a_0 and;c_0 respectively, their values after the execution of this;instruction will be c_0a_7…a_1 and a_0 respectively.JC Y;jump to Y if carry flag is setJMP Z;jump to ZY:ADD B, #1;B←B+1JMP Z;jump to ZX:;\small \begin{array}{lll} & \text{MOV B, \#0}&& \text{;} & \text{$B \leftarrow 0$} \\& \text{MOV C, \#8} && \text{;}& \text{$C \leftarrow 8$} \\ \text{Z:} & \text{CMP C, \#0} &&\text{;}& \text{compare C with 0} \\ & \text{JZ X} && \text{;}& \text{jump to X if zero flag is set} \\ & \text{SUB C, \#1} && \text{;}& \text{$C \gets C-1$} \\ & \text{RRC A, \#1} && \text{;}& \text{right rotate A through carry by one bit. Thus:} \\ & \text{} && \text{;}& \text{If the initial values of A and the carry flag are $a\_7\ldots a\_0$ and} \\ & \text{} && \text{;}& \text{$c\_0$ respectively, their values after the execution of this} \\ & \text{} && \text{;}& \text{instruction will be $c\_0a\_7\ldots a\_1$ and $a\_0$ respectively.} \\ & \text{JC Y} && \text{;}& \text{jump to Y if carry flag is set} \\ & \text{JMP Z} && \text{;}& \text{jump to Z} \\ \text{Y:} & \text{ADD B, \#1} && \text{;}& \text{$B \gets B+1$} \\ & \text{JMP Z} && \text{;}& \text{jump to Z} \\ \text{X:}& \text{} && \text{;}& \text{} \\ \end{array}
If the initial value of register A is A0 the value of register B after the program execution will be

Choose one option
View worked solution

Answer: B

To determine the final value of register BB, let us analyze the step-by-step execution of the assembly program:

  1. Initialization:
    • MOV B, #0: Register BB is initialized to 00. It acts as a counter.
    • MOV C, #8: Register CC is initialized to 88. It acts as a loop counter.
  2. Loop Condition:
    • Z: CMP C, #0 followed by JZ X: The loop terminates and jumps to label X when C=0C = 0. Since CC starts at 88 and is decremented by 11 in each iteration (SUB C, #1), the loop will execute exactly 88 times.
  3. Rotation and Carry Check:
    • RRC A, #1: This instruction rotates register AA right through the carry flag by 11 bit.
      Specifically, the least significant bit (LSB) of AA is shifted into the Carry Flag.
    • JC Y: If the Carry Flag is 11 (meaning the shifted-out bit of AA was 11), the program jumps to label Y where BB is incremented by 11 (ADD B, #1).
    • If the Carry Flag is 00, the program bypasses the increment and jumps back to the start of the loop at Z.

Since the loop runs exactly 88 times, every bit of the 88-bit register AA (initially containing A0A_0) is shifted into the Carry Flag exactly once. Register BB is incremented if and only if the shifted bit is 11.

Therefore, after the program execution, register BB will contain the number of 11 bits in A0A_0.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Machine Instructions and Addressing Modes questions