edited by
14,995 views
35 votes
35 votes

Consider the following assembly language program for a hypothetical processor $A, B,$ and $C$ are $8-$bit registers. The meanings of various instructions are shown as comments.
$$\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

  1. the number of $0$ bits in $A_0$
  2. the number of $1$ bits in $A_0$
  3. $A_0$
  4. $8$
edited by

3 Answers

Best answer
35 votes
35 votes

All other instructions except CMP are self explanatory.

CMP A, #K

The above instruction does A – #K, where A is a register and #K is a constant value, and if the result is positive, negative or zero, sets the flag accordingly which in turn activates the following “JZ” (Jump on Zero), “JP” (Jump on Positive), “JN” (Jump on Negative) etc. The mnemonic I used here might be different on different architecture but the working remains the same. 

So, the code here is counting the number of 1 bits in $A_0$. When a 1 is moved to carry, B is incremented.

Correct Option: B. 

edited by
7 votes
7 votes

$Let A=7(00000111)$

$|B=0|C=8|carry=0,zero=0|zero\neq1 |C=7|00000011\ \ 1|B=1$

$|carry=0,zero=0|zero\neq1 |C=6|10000001\ \ 1|B=2$

$|carry=0,zero=0|zero\neq1 |C=5|11000000\ \ 1|B=3$

$|carry=0,zero=0|zero\neq1 |C=4|11100000\ \ 0|$

$|carry=0,zero=0|zero\neq1 |C=3|01110000\ \ 0|$

$|carry=0,zero=0|zero\neq1 |C=2|00111000\ \ 0|$

$|carry=0,zero=0|zero\neq1 |C=1|00011100\ \ 0|$

$|carry=0,zero=0|zero\neq1 |C=0|00001110\ \ 0|$

$|carry=0,zero=1|zero=1:Jump\ to\ X|$


$Ans:B$

Answer:

Related questions