You are using the following program to calculate the factorial of a number using an 8085...
Texas Instruments aptitude question, verified with a worked answer. Free to practise - no sign-up.
You are using the following program to calculate the factorial of a number using an 8085 microprocessor:
LXI H, 2000H
MOV B, M
MVI D, 01H
FACTORIAL: CALL MULTIPLY
DCR B
JNZ FACTORIAL
INX H
MOV M, D
HLT
MULTIPLY: MOV E, B
MVI A, 00H
MULTIPLYLOOP: ADD D
XXX
MOV D, A
RET
What instruction sequence should replace XXX to complete the multiplication loop?
Show answer & explanation
The MULTIPLY subroutine implements multiplication through repeated addition. To multiply the current product (D) by the current multiplier (B), the value of B is loaded into register E to serve as a loop counter, and the loop must decrement E and jump back until E reaches zero.
Step-by-step Derivation:
Step 1: Analyze the MULTIPLY subroutine logic. The goal is to calculate D = D * B.
Step 2: The code MOV E, B initializes register E as the counter for the number of times the current value of D should be added to the accumulator.
Step 3: MVI A, 00H clears the accumulator to start the summation.
Step 4: MULTIPLYLOOP: ADD D adds the value of D to the accumulator.
Step 5: To ensure the addition happens exactly B times, we need a loop control mechanism. This requires decrementing the counter (E) and checking if it has reached zero.
Step 6: DCR E decrements the counter. JNZ MULTIPLYLOOP (Jump if Not Zero) creates the loop that continues until E = 0.
Step 7: After the loop, MOV D, A stores the result back into D for the next iteration of the factorial process.
Step 8: Comparing this to the options: Option A provides exactly this sequence (DCR E and JNZ MULTIPLYLOOP), whereas Option B increments (causing an infinite loop), Option C decrements the wrong register and uses the wrong jump condition, and Option D terminates the program prematurely.