We are given three addressing modes involving a register $X$ used as a stack pointer:
- $EA = (X)+$ → effective address is contents of location $X$, then $X$ is incremented by one word length after calculation.
- $EA = -(X)$ → $X$ is decremented by one word length before calculating EA; EA is contents of new $X$.
- $EA = (X)-$ → EA is contents of location $X$, then $X$ is decremented by one word length after calculation.
Instruction format:
$\texttt{ADD source, destination}$ → means $\texttt{destination} \leftarrow \texttt{source op destination}$
Using $X$ as a stack pointer, which instruction pops the top two elements from the stack, adds them, and pushes the result back
Step 1: Initial Stack State
Assume:
- Stack grows downward in memory.
- Word size = 4 bytes.
- $X$ points to the top of stack.
Initial state (stack with 2 elements):
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & S \\
\hline
\end{array}
\quad
X = 0x1008
$$
We want to:
- Pop $T$ → use as first operand
- Pop $S$ → use as second operand
- Add them → $T + S$
- Push result → store at new top, update $X$
Step 2: Analyze Each Option
Option A: $\texttt{ADD (X)-, (X)}$
- First operand: $(X)-$ → reads $T$ from $0x1008$, then $X$ decrements to $0x1004$
- Second operand: $(X)$ → reads $S$ from $0x1004$
- Operation: $(X) \leftarrow (X)- + (X)$ → writes $T + S$ into $0x1004$
- Final $X = 0x1004$
Stack Before:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & S \\
\hline
\end{array}
\quad
X = 0x1008
$$
Stack After:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & T + S \\
\hline
\end{array}
\quad
X = 0x1004
$$
We popped $T$ and $S$, added them, and pushed $T+S$ back. The stack now has one element: the sum. This is correct.
Option B: $\texttt{ADD (X), (X)-}$
- First operand: $(X)$ → reads $T$ from $0x1008$
- Second operand: $(X)-$ → reads $S$ from $0x1008$, then $X$ decrements to $0x1004$
- Operation: $(X)- \leftarrow (X) + (X)-$ → writes $T + S$ into $0x1008$ (before decrement), then $X = 0x1004$
Stack Before:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & S \\
\hline
\end{array}
\quad
X = 0x1008
$$
Stack After:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T + S \\
\hline
0x1004 & S \\
\hline
\end{array}
\quad
X = 0x1004
$$
We did not remove $S$. It remains on stack. We only replaced $T$ with the sum. So this is not "pop two, add, push".
Option C: $\texttt{ADD -(X), (X)+}$
- First operand: $-(X)$ → $X$ decrements to $0x1004$, reads $S$ from $0x1004$
- Second operand: $(X)+$ → reads $S$ again from $0x1004$, then $X$ increments to $0x1008$
- Operation: $(X)+ \leftarrow -(X) + (X)+$ → writes $S + S$ into $0x1004$, then $X = 0x1008$
Stack Before:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & S \\
\hline
\end{array}
\quad
X = 0x1008
$$
Stack After:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & S + S \\
\hline
\end{array}
\quad
X = 0x1008
$$
We never accessed $T$. Both operands were $S$. Incorrect.
Option D: $\texttt{ADD -(X), (X)}$
- First operand: $-(X)$ → $X$ decrements to $0x1004$, reads $S$
- Second operand: $(X)$ → reads $S$ again from $0x1004$
- Operation: $(X) \leftarrow -(X) + (X)$ → writes $S + S$ into $0x1004$
- $X = 0x1004$
Stack Before:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & S \\
\hline
\end{array}
\quad
X = 0x1008
$$
Stack After:
$$
\begin{array}{|c|c|}
\hline
\textbf{Address} & \textbf{Content} \\
\hline
0x1008 & T \\
\hline
0x1004 & S + S \\
\hline
\end{array}
\quad
X = 0x1004
$$
Again, we read $S$ twice, never touched $T$. Incorrect.
$$
\color{skyblue} \boxed{\text{A. ADD (X)-, (X)}}
$$