answersLogoWhite

0


Best Answer

The stack has many purposes, but is chiefly used to enable procedure calls. When you invoke a function, the return address (the next instruction after the function call) is pushed onto the stack. Control is then passed to the function. When the called function returns, it pops the return address off the stack and control is returned to that address. In this way functions can call functions without losing track of where the calls came from because return addresses are popped in the reverse order they were pushed. In other words, it provides an automatic "breadcrumb trail" without which it would be impossible to call and return from functions.

The stack is also used to pass actual arguments to functions by pushing values onto the stack immediately after the return address. The called function can then pop those values off the stack and assign them to its formal arguments.

Finally, the stack is used to store the function's local variables prior to calling a function. These are pushed onto the stack before the return address is pushed. In this way, a function can call itself recursively without losing track of its own local variables.

Since invoking and returning from functions is a time-consuming operation, compilers will try to expand functions inline and thus eliminate the overhead of the function call. However, this needs to be weighed against the increased code size which can affect overall performance. By keeping functions small (one or two very simple statements), programs not only become much easier to read and maintain, there's also a much greater opportunity for inline expansion. Complex functions are never good examples for inline expansion.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is role of stack in calling a subroutine and returning from the routine?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are stacks subroutines in 8085 microprocessor?

A subroutine is a group of instructions that will be used repeatedly in diff locations of the program..........rather than repeating the same instructions several times, they can be grouped into a subroutine that is called from diff locations. 8085 has 2 instruction set for dealing with subroutines: 1.CALL -direct the program execution to the subroutine. Generally it pushes address of next instruction of program counter onto the stack,then goes to the address of subroutine. 2.RET:- pops the address of next instruction from the stack and places it in the program counter and returns to that address to continue processing. For example, you have an often used value stored in HL. You have to call a subroutine that you know will destroy HL (with destroy I mean that HL will be changed to another value, which you perhaps don't know). Instead of first saving HL in a memory location and then loading it back after the subroutine, you can push HL before calling and directly after the calling pop it back. Of course, it's often better to use the pushes and pops inside the subroutine.


What is an activation record?

An activation record is a data structure containing subroutine state information located on the call stack.


Mention the importance of subroutines in programming?

A subroutine is typically defined as being a function or procedure that can be invoked at any point in a program, causing the procedural execution to branch to the subroutine. This is not unlike a goto or jump statement, however subroutines also make use of the program's call stack to allow values (arguments or parameters) to be passed to the subroutine, including the address of the caller so that execution can return to the point of the call when the subroutine falls from scope. A subroutine can also (optionally) return a single value to the caller, known as the return value. Return values are temporary and will fall from scope when the expression that invoked the subroutine falls from scope. However, by assigning a subroutine to a variable, the return value can be stored in that variable and can then used in subsequent expressions. Subroutines pop their arguments from the stack and construct local variables or references from the argument values. Arguments passed by reference can therefore be used to return values through the referenced values. Values returned in this manner are known as output arguments. Subroutines that use output arguments often use the subroutine's temporary return value to indicate any errors that occurred within the subroutine, with zero indicating success (no error).


Implementation of stack using recursion?

public void reverse(Stack st) { int m = (int)st.Pop(); if (st.Count != 1) reverse(st); Push(st , m); } public void Push(Stack st , int a) { int m = (int)st.Pop(); if (st.Count != 0) Push(st , a); else st.Push(a); st.Push(m); }


Which function call Does not consume stack space?

Calling an in-line function, which is not actually a function-call.

Related questions

How can a stack be used for implementing a subroutine call?

A stack can be used to implement a subroutine call because the return address is pushed onto the stack, and control transfers to the subroutine. If the subroutine were to then call another subroutine, or even itself, a second return address would be pushed. When the subroutine returns control, the return address is popped off of the stack and control transfers to the return address.In addition, the stack can be used to pass arguments. The caller can push the arguments onto the stack before calling the subroutine. The subroutine can then access the arguments by making references relative to the stack pointer. When the subroutine returns control, the caller then pops the arguments off of the stack.Further, the subroutine can use the stack to store local variables. It adjusts the stack pointer in the direction of pushing things onto the stack, and then make references to that region of memory between the original value of the stack pointer and the new value of the stack pointer.This is exactly how most subroutines work on the 8086/8088 (and higher) processors...The caller pushes arguments on the stackThe caller calls the subroutine, pushing the return address on the stackThe subroutine pushes caller registers onto the stack, if that is the agreed convention for that callThe subroutine pushes the BP register on the stack and then copies the stack pointer (SP) to BPThe subroutine adjusts SP downward, allocating the required local memoryThe subroutine accesses arguments with [BP+offset] addressingThe subroutine accesses local variables with [BP-offset] addressingWhen the subroutine is done, it sets SP back to BP, pops caller's registers off of the stack, pops caller's return address off of the stack, and then returns to the callerThe caller then pops the arguments off of the stack.The structure in memory, representing arguments, return address, saved registers if needed, and local variables, with BP in the center, is called a stack frame.Done properly, a subroutine can be fully recursive, even able to call itself, or be called by other threads, because all local variables and the registers if saved are stored on the stack. The ability to be recursive is also called reentrancy.


Which segment is used to store interrupt and subroutine return address registers?

stack segment


What are stacks subroutines in 8085 microprocessor?

A subroutine is a group of instructions that will be used repeatedly in diff locations of the program..........rather than repeating the same instructions several times, they can be grouped into a subroutine that is called from diff locations. 8085 has 2 instruction set for dealing with subroutines: 1.CALL -direct the program execution to the subroutine. Generally it pushes address of next instruction of program counter onto the stack,then goes to the address of subroutine. 2.RET:- pops the address of next instruction from the stack and places it in the program counter and returns to that address to continue processing. For example, you have an often used value stored in HL. You have to call a subroutine that you know will destroy HL (with destroy I mean that HL will be changed to another value, which you perhaps don't know). Instead of first saving HL in a memory location and then loading it back after the subroutine, you can push HL before calling and directly after the calling pop it back. Of course, it's often better to use the pushes and pops inside the subroutine.


Which program segment at run time is used to store interrupt and subroutine return addresses?

If this is a homework assignment, you really should try to answer it on your own first, otherwise the value of the reinforcement of the lesson due to actually doing the assignment will be lost on you.Interrupt and subroutine return addresses are stored on the stack, so the stack segment is used for interupt and subroutine addresses.


What is stack space?

A type of RAM that is organized as a stack. or part of RAM that has software to make it operate like a stack. A stack memory operates like one of those chip dispensers they use in Los Vegas. You push the chips onto the stack. When you remove one (called a pop), it was the one on the top, the last one you put in. The first one you put in is the last one you take out. They are used by certain types of computer hardware and software that needs data accessed in that way, FILO (first in last out) and LIFO (last in first out). For example subroutine return addresses. When the CPU executes a subroutine call, the return address is pushed on the stack. The subroutine may call another subroutine, with another return address pushed on the stack. And more. then when the subroutines are exited, the addresses are POPed off the stack and executed. The use of a stack ensures the returns are all executed in the correct order.


What is an activation record?

An activation record is a data structure containing subroutine state information located on the call stack.


Stack register are used to store?

The stack register contains the address of the last item pushed on the stack. The stack is a region of memory used for temporary storage of instruction addresses and register values in a Last-In-First-Out (nested) structure. It is used for interrupts and subroutine calls.


What will happen when pop instruction is executed?

The top of stack to copied to the specified register and the stack pointer is incremented by 2. A special form of POP, RET, has the program continuing with the popped address in the program counter, i.e. a return from subroutine or function call.


Mention the importance of subroutines in programming?

A subroutine is typically defined as being a function or procedure that can be invoked at any point in a program, causing the procedural execution to branch to the subroutine. This is not unlike a goto or jump statement, however subroutines also make use of the program's call stack to allow values (arguments or parameters) to be passed to the subroutine, including the address of the caller so that execution can return to the point of the call when the subroutine falls from scope. A subroutine can also (optionally) return a single value to the caller, known as the return value. Return values are temporary and will fall from scope when the expression that invoked the subroutine falls from scope. However, by assigning a subroutine to a variable, the return value can be stored in that variable and can then used in subsequent expressions. Subroutines pop their arguments from the stack and construct local variables or references from the argument values. Arguments passed by reference can therefore be used to return values through the referenced values. Values returned in this manner are known as output arguments. Subroutines that use output arguments often use the subroutine's temporary return value to indicate any errors that occurred within the subroutine, with zero indicating success (no error).


Implementation of stack using recursion?

public void reverse(Stack st) { int m = (int)st.Pop(); if (st.Count != 1) reverse(st); Push(st , m); } public void Push(Stack st , int a) { int m = (int)st.Pop(); if (st.Count != 0) Push(st , a); else st.Push(a); st.Push(m); }


Which function call Does not consume stack space?

Calling an in-line function, which is not actually a function-call.


What is the difference between branch instruction call sub routine program and interrupt?

Branch Instruction A branch (or jump on some computer architectures, such as the PDP-8 and Intel x86) is a point in a computer program where the flow of control is altered. The term branch is usually used when referring to a program written in machine code or assembly language; in a high-level programming language, branches usually take the form of conditional statements, subroutine calls or GOTO statements. An instruction that causes a branch, a branch instruction, can be taken or not taken: if a branch is not taken, the flow of control is unchanged and the next instruction to be executed is the instruction immediately following the current instruction in memory; if taken, the next instruction to be executed is an instruction at some other place in memory. There are two usual forms of branch instruction: a conditional branch that can be either taken or not taken, depending on a condition such as a CPU flag, and an unconditional branch which is always taken. Call Subroutine instructions Call Subroutine instructions and Return From Subroutine instructions within the instruction stream. The first stage stores a return address in a return register when a Call Subroutine instruction is predicted. The first stage predicts a return to the return address in the return register when a Return From Subroutine instruction is predicted. A second stage decodes each Call Subroutine and Return From Subroutine instruction in order to maintain a Return Stack Buffer that stores a stack of return addresses. Each time the second stage decodes a Call Subroutine instruction, a return address is pushed onto the Return Stack Buffer. Correspondingly, each time the second stage decodes a Return From Subroutine instruction, a return address is popped off of the Return Stack Buffer. The second stage verifies predictions made by the first stage and predicts return addresses for Return From Subroutine instructions that were not predicted by the first stage. A third stage executes Return From Subroutine instructions such that the predictions are verified. Finally, a fourth stage retires Return From Subroutine instructions and ensures that no instructions fetch after a mispredicted return address are committed into permanent state. Program interrupt an interrupt is an asynchronous signal from hardware indicating the need for attention or a synchronous event in software indicating the need for a change in execution. A hardware interrupt causes the processor to save its state of execution via a context switch, and begin execution of an interrupt handler. Software interrupts are usually implemented as instructions in the instruction set, which cause a context switch to an interrupt handler similar to a hardware interrupt. Interrupts are a commonly used technique for computer multitasking, especially in real-time computing. Such a system is said to be interrupt-driven. An act of interrupting is referred to as an interrupt request ("IRQ").