Consider the following assembly language program for a hypothetical processor and are bit registers. The meanings of various instructions are shown as comments.
|
MOV B, #0 |
; |
|
|
|
MOV C, #8 |
; |
|
|
|
Z: |
CMP C, #0 |
; |
compare C with 0 |
|
JZ X |
; |
jump to X if zero flag is set |
|
|
SUB 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 and |
||
|
; |
respectively, their values after the execution of this |
||
|
; |
instruction will be and respectively. |
||
|
JC Y |
; |
jump to Y if carry flag is set |
|
|
JMP Z |
; |
jump to Z |
|
|
Y: |
ADD B, #1 |
; |
|
|
JMP Z |
; |
jump to Z |
|
|
X: |
Which of the following instructions when inserted at location will ensure that the value of the register after program execution is as same as its initial value?
View worked solution
Answer: A
Let the initial value of register be and the initial state of the carry flag be .
The loop runs exactly times (as register decrements from to ). In each iteration, the RRC A, #1 instruction rotates the bits of right through the carry flag by bit:
- Initial state: ,
- After 1st rotation: ,
- After 2nd rotation: ,
- After 8th rotation (at label X): ,
To restore register to its original value , we need to shift the current carry flag () back into the MSB of , and shift all other bits right by one position.
Executing one more RRC A, #1 at location does exactly this:
Thus, inserting RRC A, #1 at location restores the initial value of register .
Free practice · Your answers on this page are not saved.
