What is Stack Allocation of Procedure Calls?


In stack allocation, it can analyze how the memory is allocated at runtime when a procedure is called & when the value from the procedure is returned.

  • Passing Parameter to Procedure (param x)− When actual parameter x is passed to the procedure, it will be pushed into the stack, i.e., push (x).

∴ param(x) refers to push (x) which refers to decrementing of the top pointer from N + 1 to N and x will be pushed onto the stack.

∴ The following statements will be executed.

  • top = top – 1
  • *top = x or 0[top] = x


Here 0[top] means 0 offsets from the top, i.e., 0 distance away from the top, i.e., the top itself.

∴ *top = x will assign value of top = x.

  • Calling Procedure (call P, n)− Execution of this statement will insert all entries of activation record of P, i.e., the number of arguments, return address, space for the return value, old stack pointer onto the stack.

∴ call P, n will lead to the execution of the following statements.

  • push(n)− Push the number of arguments.
  • push(I1)− l1is the label of the return address.
  • push()− Keep empty space for the return value to be filled.
  • push(Sp)− Store old stack pointer.
  • goto I2− I2 is the label of the first statement of procedure P.


  • First Statement of procedure (procbegin)− It assigns the value of stack pointer to old SP. Top pointer will point to the top of the activation record. Local data, i.e., the size of Procedure P is added to the stack.

If SOP= Size of procedure or memory is taken by local data of procedure.

∴ Following statements will be executed −

  • 𝐒𝐏 = 𝐭𝐨𝐩− Stack Pointer SP will point to old SP which was earlier stored at the top.
  • 𝐭𝐨𝐩 = 𝐒𝐏 + 𝐒𝐎𝐏− Size of Procedure (SOP) is added to SP to give top position.


Now top pointer will point to the top of the activation record.

  • Return Statement (return value)− When the procedure returns a value, the returned value will be stored at an empty stack which is kept free for it above the stack pointer (SP) position.

When the procedure returns, the activation record of the procedure will be deleted or popped from the memory location.

When the procedure P returns the value, set the top pointer to the value it had before P was called.

Set pointer P to the value of old SP, i.e., SP of the procedure which had called P.

The top pointer will point to extra storage of activation record of procedure which had called P.

Following statements will be executed on returning a value −

  • 𝟏[𝐒𝐏] = 𝐯𝐚𝐥𝐮𝐞− Since the return value is 1 location always SP pointer. The returned value will be stored at offset 1 from SP.
  • 𝐭𝐨𝐩 = 𝐒𝐏 + 𝟐− top points to the return address.
  • 𝐒𝐏 =∗ 𝐒𝐏− Set pointer SP to the value of old SP.
  • 𝐥 =∗ 𝐭𝐨𝐩− the value of the l will contain the return address.
  • 𝐭𝐨𝐩 = 𝐭𝐨𝐩 + 𝟏− top points to the number of arguments.
  • 𝐭𝐨𝐩 = 𝐭𝐨𝐩 + 𝟏 +∗ 𝐭𝐨𝐩− Currently, top points to the number of arguments, addition with 1 will move the top pointer to an actual parameter, addition with *top, i.e., the number of arguments will move the top pointer to the position it was earlier before P was called.


Ginni
Ginni

e

Updated on: 08-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements