The Gateway to Computer Science Excellence
+18 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.


MOV B, #0


$B \leftarrow 0$


MOV C, #8


$C \leftarrow 8$


CMP C, #0


compare C with 0




jump to X if zero flag is set


SUB C, #1


$C \gets C-1$


RRC 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.




jump to Y if carry flag is set




jump to Z


ADD B, #1


$B \gets B+1$




jump to Z



 Which of the following instructions when inserted at location X will ensure that the value of the register A after program execution is as same as its initial value?

  1. RRC A, #1
  2. NOP  ; no operation
  3. LRC A, #1;   left rotate A through carry flag by one bit
  4. ADD A, #1
in CO and Architecture by Veteran (104k points)
retagged by | 2.7k views

2 Answers

+31 votes
Best answer
Option $(A)$ RRC a, #1.  As the 8 bit register is rotated via carry 8 times.

$c_0a_7a_6a_5a_4a_3a_2a_1$, now $a_0$ is the new carry. So, after next rotation,

So, after 8 rotations,

$a_6a_5a_4a_3a_2a_1a_0c_0$ and carry is $a_7$.

Now, one more rotation will restore the original value of $A_0$.
by Veteran (422k points)
edited by
sir.. here condition JC Y is when checked? as we do not know the value in register A. Do we have to ignore the instruction through out?
same doubt! @ arjun sir
@Arjun Sir i have one doubt, I think CMP will reset the zero and carry flags when !=0( C>0)

So every time RRC is performed it should insert 0 at MSB every time right???

So how can the contents be made same by using RRC A only one time that too at the end.
It doesn't matter here as we need to Jump on Z again in both the cases whether it is a 0 or 1 in the carry. It is used just to complete the task of given assembly language program. The main concern is RRC on A and to get A again after the complete execution.
Sir can you please explain with the example?
I have a doubt, all arithmatic operation update carry flag so ADD B,#1 will also update it so by rotating right if we get carry flag =1 then we jump to Y

And in Y we perform addition (ADD b,#1) as there is no carry in this operation so carry flag will become 0 thus after execution of complete code A will be 00000000 so for part B we cannot perform any operation so that A will be equal to its initial value

where am i thinking wrong?
take example: place 0000 0101 in A then run the program, you wil get it

@ArjunSir Then Where  SUB C#1 in this program is used?


@Surya Pratap Singh S if we take your example then after first 1 execution will jump to Y and carry bit is 1 and then we are performing an arithmetic operation i.e we are adding 1 to B so it is arithmetic operation so this will modify carry bit to 0 so the first 1 became 0 because of this

+5 votes
If A has n bits with 1 carry bit ... then number of right rotations through carry after which we will get the same A is (n+1) .. Here this code is doing right rotation for n times .. So at the last step of program we do one more rotation ...
by Loyal (7.8k points)

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
50,666 questions
56,167 answers
94,005 users