The Gateway to Computer Science Excellence
+24 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.
$$\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$
in CO and Architecture by Veteran
edited by | 5.3k views
can anyone plz explain this ques. solution with example.
Content of A0 is copied to A, we are rotating register to right with Carry, every 1 present in the register A carry will be set to 1, and everytime carry flag is set to 1, B will be incremented.

A contains the content of A0, it means when this program terminates, B will contain the number of 1's in the A (or) A0. there will be 8 iterations of the loop as value of C is initially 8 and with every iteration it is decremented by 1.
and because its an 8 bit register the max ones can only be 8 so 8 iterations always suffice

2 Answers

+27 votes
Best answer
Option $(B)$. The code is counting the number of 1 bits in $A_0$. When a 1 is moved to carry, B is incremented.
by Veteran
edited by

Dear Arjun sir

I have a very stupid doubt here. Please pardon my ignorance.

First time the value of C will be 8 ==> 1000

Now , after SUB C,#1 , value of C will be 7 ==> 0111

Now , will the RRC be 1011 ? so , B will be 0+1 = 1

After this C will be 7 ( CMP C,#0 will be zero flag false ) , now C <- C-1 , so C is 6 , 0110 ,

Now , RRC will be 0011 , so B will not be incremented.

Will this program flow happen in this way ?

@ Arjun sir, In 49 , why rotation will happen 8 times ?
@coolcoder are you rotating C? Question is rotating A and decrementing C.

@Amsar Because C is initialized to 8.

Arjun sir,

yes yes , sorry ... 


@Arjun SIR 
Can u pls explain what is happening in line

RRC A, #1

Carry Flad =0 Regiser A = 10010
After RRC  010010
When does carry flag set? 

when $a_0=1$. This is mentioned in question.
Sir what will be contents of A after this code. Will it be 00000000, bcoz of CMP imstruction, as it will reset carry and zero flag when C !=0.
please provide the steps and iterated solution with one example.....
Sir what is the initial value of the register A.....??  Not mentioned anywhere!!

Preety singhal 

   you can assume any initial value of register content A  (i.e: the content of Ao )

like...suppose A = 00101101  or whatever you want can assume.and at last when we execute this program the register content of B will be the total number of 1's which are present in register A.( in this case at last content of register B will be 4, because 4,  1's are present in register A )

And one more thing u can also assume initial carry either 0 or 1.

8 bit register along with 1 carry bit->9 bits are rotated right 8 times.

So, In Each rotation $i$, bit $a_{i-1}$ comes into carry flag and depending on whether it's 1 or 0, CY flag is set or reset.

8 times rotation, bits $a_0-a_7$ each time come in CY flag.

After $8^{th}$ rotation, register would contain $a_6a_5a_4a_3a_2a_1a_0c_0$ and CY flag $a_7$
@Arjun sir, where a0=1 is given?
0 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|$


by Loyal

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
52,223 questions
59,812 answers
118,087 users