0
$\begingroup$

Suppose a processor lacks STACK Pointer Register. But, It does have STACK. Then, in my opinion, a program will still be able to call subroutines but, will be unable to return back from the subroutines. Please do clarify me where I have made a wrong assumption. Also do mention the possible outcomes of the scenario.

$\endgroup$
1
$\begingroup$

Using a stack for function calls does not require any special support from the processor. It's up to the compiler to decide where to put the data relative to a function call (return address, parameters, possibly reserved space for the return value). A common convention is to designate one particular register as the stack pointer. This does not require any special processor instruction, it can be a general-purpose register. A function call looks like this:

  • Write parameters to the memory region at address SP (stack pointer register)
  • Decrease SP by the size of the parameters
  • Write PC (program counter register) to the address SP
  • Decrease SP by the size of PC
  • Jump to the function entry point

Returning from the function looks like this:

  • Load a PC-sized value from SP into a register A
  • Increase SP by the size of PC
  • Jump to A

Some processors have a particular register R with instructions to store a word in memory at the address designated by R and decrement R, and to load a word in memory at the address designated by R and increment R. In this case, the processor documentation typically describes R as the stack pointer register and compilers use it to store the address of the function call stack. But if the processor doesn't have such instructions, the compiler just emits separate instructions for load/store and for increment/decrement. The register doesn't need any special properties.

$\endgroup$
  • $\begingroup$ Okay. Thank you, Gilles for your elaboration. $\endgroup$ – Shashwat Vaibhav Sep 18 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.