• edited by
28,374 views
79 79 votes

Consider a processor with byte-addressable memory. Assume that all registers, including program counter (PC) and Program Status Word (PSW), are size of two bytes. A stack in the main memory is implemented from memory location $(0100)_{16}$ and it grows upward. The stack pointer (SP) points to the top element of the stack. The current value of SP is $(016E)_{16}$. The CALL instruction is of two words, the first word is the op-code and the second word is the starting address of the subroutine (one word = 2 bytes). The CALL instruction is implemented as follows:

  • Store the current value of PC in the stack
  • Store the value of PSW register in the stack
  • Load the statring address of the subroutine in PC

The content of PC just before the fetch of a CALL instruction is $(5FA0)_{16}$. After execution of the CALL instruction, the value of the stack pointer is:

  1. $(016A)_{16}$
  2. $(016C)_{16}$
  3. $(0170)_{16}$
  4. $(0172)_{16}$

5 Answers

Best answer
106 106 votes

First we have to consider here memory is byte-addressable 

The CALL instruction is implemented as follows:

  • Store the current value of PC in the stack

    PC is $2$ bytes it means when we store pc in stack it will increase by $2$  
    So current value of SP is $(016E)_{16} +2$
     
  • Store the value of PSW register in the stack
    PSW is $2$ byte it means when we store psw in stack it will increase by $2$  
    So current value of SP is $(016E)_{16}+2+2 =(0172)_{16}$

Correct Answer: $D$

• edited by
5 5 votes

1. The Question: The question asks for the final value of the stack pointer (SP) after the CALL instruction executes.

2. What the CALL Instruction Does (to the Stack)

The problem explicitly tells us the CALL implementation:

  1. Store the current value of PC in the stack.

  2. Store the value of PSW register in the stack.

  3. (Load the new address into PC - this does not affect the stack).

So, the only thing that happens to the stack is that two items are pushed onto it: first the PC, then the PSW.

 

3. Key Information to Solve the Problem

  1. Current SP: The stack pointer starts at $(016E)_{16}$.

  2. Item Size:

    • Size of PC = 2 bytes (given)

    • Size of PSW = 2 bytes (given)

  3. Stack Behavior (The Most Important Part):

    • "A stack... grows upward."

 

4. Step-by-Step Calculation

This is where we trace the SP.

  • What "Grows Upward" Means: Most stacks in computers grow downward (pushing decreases the address). This stack is different. "Grows upward" means that every time you push an item, the stack pointer's address increases.

  • What "SP points to the top element" Means: The SP's current value, $(016E)_{16}$, is the address of the last item currently on the stack.

Let's trace the operation:

  1. Initial State:

    • SP = $(016E)_{16}$

  2. Operation 1: Store PC (2 bytes)

    • We need to push 2 bytes onto the stack.

    • Because the stack grows upward, we add the size to the SP.

    • New SP = (Old SP) + (Size of PC)

    • New SP = $(016E)_{16} + 2$

  0 1 6 E   (in hex)
+       2   (in hex)
----------

 

  • In hexadecimal, the letter E represents the decimal number 14.

  • So, this is $14 + 2 = 16$.

  • How do we write 16 in hexadecimal? It's 10. (It's one "16" and zero "1s").

  • This means we write down 0 and carry over the 1 to the next column.

      1
  0 1 6 E
+       2
----------
        0

      1
  0 1 6 E
+       2
----------
      7 0

  0 1 6 E
+       2
----------
  0 1 7 0
    • New SP = $(0170)_{16}$

    • (At this point, the 2 bytes of the PC are stored at addresses $(016F)_{16}$ and $(0170)_{16}$, and the SP points to the new top at $(0170)_{16}$).

  1. Operation 2: Store PSW (2 bytes)

    • Next, we push the 2-byte PSW onto the stack.

    • New SP = (Current SP) + (Size of PSW)

    • New SP = $(0170)_{16} + 2$

    • New SP = $(0172)_{16}$

    • (The 2 bytes of the PSW are stored at $(0171)_{16}$ and $(0172)_{16}$. The SP now points to the final top element at $(0172)_{16}$).

Final Value: $(0172)_{16}$


 

 "Trap" Information (What to Ignore)

 

This problem is difficult because it gives you three pieces of information that are completely irrelevant to finding the final stack pointer value.

  • TRAP 1: The content of PC just before the fetch... is $(5FA0)_{16}$

    • Why it's a trap: This information is needed to calculate the return address (the value of the PC that gets pushed). The CALL instruction is 2 words (4 bytes) long, so the return address pushed would be $(5FA0)_{16} + 4 = (5FA4)_{16}$. But we don't care what value is pushed; we only care that 2 bytes are pushed.

  • TRAP 2: The CALL instruction is of two words...

    • Why it's a trap: This is also part of the return address calculation. It has no impact on the stack pointer's final position.

  • TRAP 3: A stack... is implemented from memory location $(0100)_{16}$

    • Why it's a trap: This is the base of the stack. We don't need to know the base, only the current top (which is $(016E)_{16}$).

The most common mistake is to assume the stack grows downward, which would lead to $(016E)_{16} - 4 = (016A)_{16}$ (Option A).

• edited by
1 1 vote

Stack in main memory grows upwards. This statement tells that the stack pointer decrements on adding new elements to stack. For more details plz refer to hamacher 2nd chapter. So i think it will work as : 

  • Store the current value of PC in the stack 

    pc is 2 byte it means when we store pc in stack it will deccrease by 2   
    so current value of SP is (016E)16 -2 

     
  • Store the value of PSW register in the stack 
    psw is 2 byte it means when we store psw in stack it will decrease by 2   
    so current value of SP is (016E)16 -2-2 =(016A)16 
     
Answer:
Position:
Show:

Related questions

54 54 votes
7 answers 7 answers
23.1k
23.1k views
go_editor asked Feb 12, 2015
23,078 views
Assume that for a certain processor, a read request takes $50\:\text{nanoseconds}$ on a cache miss and $5\:\text{nanoseconds}$ on a cache hit. Suppose while running a pro...
60 60 votes
5 answers 5 answers
23.4k
23.4k views
go_editor asked Feb 13, 2015
23,430 views
In a connected graph, a bridge is an edge whose removal disconnects the graph. Which one of the following statements is true?A tree has no bridgesA bridge cannot be part ...
52 52 votes
9 answers 9 answers
22.0k
22.0k views
go_editor asked Feb 12, 2015
22,010 views
Consider the following routing table at an IP router:$$\begin{array}{|l|l|l|} \hline \textbf {Network No} & \textbf {Net Mask} & \textbf{Next Hop} \\\hline \text {128.96...
53 53 votes
4 answers 4 answers
18.0k
18.0k views
go_editor asked Feb 12, 2015
17,973 views
Consider the C program below#include <stdio.h int *A, stkTop; int stkFunc (int opcode, int val) { static int size=0, stkTop=0; switch (opcode) { case -1: size = val; brea...