answersLogoWhite

0

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 stack
  • The caller calls the subroutine, pushing the return address on the stack
  • The subroutine pushes caller registers onto the stack, if that is the agreed convention for that call
  • The subroutine pushes the BP register on the stack and then copies the stack pointer (SP) to BP
  • The subroutine adjusts SP downward, allocating the required local memory
  • The subroutine accesses arguments with [BP+offset] addressing
  • The subroutine accesses local variables with [BP-offset] addressing
  • When 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 caller
  • The 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.

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

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

stack segment


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.


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.


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 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.


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).


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").


What are subroutines?

In computer programming, a subroutine is an identified sequence of instructions with a start and an end point which may be invoked from another part of the program. When a subroutine is called, the processor executes the instructions until it reaches the end of the subroutine, at which point control is returned to the point in the program immediately following the call. In most programming languages, a set of conventions are followed which allow values to be passed into the subroutine and for a result to be returned, so that the subroutine can be used in many different contexts. This is the most basic form of reusable software. In higher-level languages, functions and methods are specialized forms of subroutines.


What is the significance of 'XCHG' and 'SPHL' instruction?

The 'XCHG' instruction in assembly language is used to exchange the values of two operands, typically a register and a memory location, effectively performing a swap without needing a temporary variable. It is significant for implementing atomic operations in multi-threaded programming. The 'SPHL' instruction, specific to certain assembly languages like 8085, is used to load the stack pointer (SP) with the address contained in the HL register pair, which is crucial for managing the stack in subroutine calls and local variable storage. Both instructions are fundamental for efficient low-level programming and system control.


What is stack pointer?

The CPU has an automatic program stack. This stack can be located anywhere in the address space and can be any size up to the amount of memory available in the system. Normally the SP is initialized by one of the first instructions in an application program. The stack is configured as a data structure that grows downward from high memory to low memory. At any given time, the SP holds the 16-bit address of the next free location in the stack. The stack acts like any other stack when there is a subroutine call or on an interrupt. ie. pushing the return address on a jump, and retrieving it after the operation is complete to come back to its original location. There are instructions that push and pull the A and B accumulators and the X and Y index registers. These instructions are often used to preserve program context.


What is subroutine?

Subroutine is an instruction sequence in a machine or assembly language program that can be prewritten and referred to as often as needed. Subroutine is used for controlling thing e.g. traffic lights burglar alarms they all use subroutine


Basic concept and application of stack?

A stack is a last in first out (LIFO) data structure. Stacks are typically used in backtracking algorithms where values are popped off the stack in the reverse order they were pushed onto the stack. Procedural programming languages make use of a call stack so that functions can easily return control to their callers -- which is vital when a function may have more than one call site. The calling function pushes the return address onto the call stack and then passes control to the callee function. When the callee has finished its task, the return address is popped off the stack and control returned to that address. In this way, functions can call other functions (including themselves) without losing track of where those calls came from. The call stack is also used to store a function's local variables as well as any arguments that were passed to the function by its caller. The function's exception handlers can also be placed on the stack. When an unhandled exception occurs, the call stack unwinds until a stack frame with a suitable exception handler is located. If the call stack unwinds all the way to the program's entry point (the main function) and there is still no handler, the program terminates with an unhandled exception error.