in CO and Architecture edited by
1 flag 9,808 views
29 votes
29 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$

Z:

CMP C, #0

;

compare C with 0

 

JZ X

;

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.

 

JC Y

;

jump to Y if carry flag is set

 

JMP Z

;

jump to Z

Y:

ADD B, #1

;

$B \gets B+1$

 

JMP Z

;

jump to Z

X:

     

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. $\text{RRC A}, \#1$
  2. $\text{NOP} ;$ no operation
  3. $\text{LRC A,} \#1; $  left rotate $A$ through carry flag by one bit
  4. $\text{ADD A,} \#1$
  • 🚩 Edit necessary | 👮 Arjun
in CO and Architecture edited by
1 flag
9.8k views

3 Comments

Can anyone explain why CMP C, #0 is not resetting the carry flag? 

1
1
edited by
0
0
edited by

For the given question, NO option is correct. Detailed Video Solution with Complete Analysis of this question is HERE.

$ADD,SUB, CMP$ instructions affect all the status flags, like carry flag, zero flag etc.

$SUB$ instruction sets carry flag when there is need of borrow into the MSB i.e. when we subtract bigger number from smaller number. $ADD$ instruction sets carry flag when there is carry out of MSB. $CMP$ instruction subtracts second operand from first for flags only. If first operand is $\geq$ second operand, then carry flag is reset, else set. 

NOTE that just before $RRC$ instruction, the $SUB$ instruction will also affect the Carry Flag, & in each iteration, since there is no requirement of borrow for the subtraction operation, Carry Flag will be reset just before the $RRC$ instruction. 

Hence, WHATEVER be the value of $A$ in the beginning, the final value of $A$ will ALWAYS be $0.$ 

NOTE: In question, “SUB C, #1” should be replaced with “DEC C” to prevent modification of carry flag. Also, “ADD B, #1” should be replaced with “INC B” to prevent modification of carry flag. Also, it should be mentioned that $CMP$ instruction, somehow, doesn't affect carry flag. With these 3 changes, answer becomes Option A.

Detailed Video Solution with Complete Analysis: https://youtu.be/5aC2H2u_VgI 

Both Question 48 & Q 49 are analyzed in the above lecture

1
1

3 Answers

52 votes
52 votes

Option $(A) \text{RRC}\; a, \#1.$  As the 8 bit register is rotated via carry $8$ times.

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

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

PS: In question, “ADD B, #1” should be replaced with “INC B” to prevent modification of carry flag. 

edited by
by

4 Comments

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

0
0

@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

1
1
not only add B,#1. isn't there other instructions also which might change the carry flag thus loosing the data bit in it, such as sub c,#1?
1
1
10 votes
10 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 ...
6 votes
6 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|$

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?

$If\ X:RRC \ A,\#1$

$|00000111\ \ 0|$


$Ans:A$

4 Comments

  1. “zero” is denoted as zero flag here. So, when you write zero=0, it means value of zero flag is zero and  zero != 1 shows only that value of zero flag is still zero, it just shows that we don’t have to go to “X” now. Values in zero flag is either 0 or 1.
  1. In Assembly language, CMP A,B instruction means processor compares the two arguments $A$ and $B$ and when they are equal then zero flag is set which means value of the zero flag register is 1 i.e. zero=1. Here, at the end, $C=0$ and so, when processor goes to CMP C, #0 instruction, it finds that both arguments are equal and so, it sets the zero flag i.e zero=1.
  1. Carry flag is set means value of the carry flag is 1. 
1
1

@ankitgupta.1729 

Thank you 

one last doubt , you have written :->

 “Here, at the end, C=0 and so, when processor goes to CMP C, #0 instruction, it finds that both arguments are equal and so, it sets the zero flag i.e zero=1.”

It is not mentioned to make zero =1 in the program , does it automatically happen after the condition became true.

 

Thank you very much I understand the solution today(after 3 day😄).

 

0
0
Yes, it is not mentioned here because the person who made this question might be expecting to know the functionality of “CMP” instruction from the candidates for this exam.
1
1
Answer:

Related questions