What Advantages Do Stack Parameters Have Over Register Parameters
15.four Passing Parameters at the Assembler Level
- Now that we are familiar with the concepts underlying parameter passing, nosotros shall come across how this is achieved at the assembler level. In a high level language like C, all the developer has to make up one's mind is whether to pass parameters past value or by reference, and the compiler will automagically take care of the rest. Still, at the assembler level, after having decided to pass a parameter by value/reference, the programmer must also make up one's mind how the parameters are actually passed to the subroutine. This can be washed in three distinct ways: via registers, via a retention location or via the stack. These are the actual mechanisms used to pass parameters: using whatsoever of these mechanisms we can either cull to pass a parameter by value or past reference.
15.four.1 Passing Parameters via Registers
- This is the simplest of the three techniques. All nosotros have to do is put the parameter nosotros want to pass in some annals. Then the subroutine called simply accesses the right register to go its hands on the parameter. Of course the subroutine will take to have been written in such a way as to expect its argument to be in a given register. By putting the address of some parameter in memory in an accost register, this technique can be used to implement passing by reference. Likewise given some parameter, if nosotros put a copy of its value in some data annals, then we are in effect implementing passing by value.
The advantage of this technique is that information technology is very quick: the subroutine demand not admission retentivity in guild to fetch the parameters. Information technology likewise makes for position independent code and re-aspirant lawmaking, equally no absolute memory address is used. On the other hand, its scope of awarding is limited: if many parameters need to be passed then registers volition run out . This problem is alike to the i we had when the return address had to exist passed to a subroutine, and similarly we shall meet that the stack will exist the cornerstone technique to pass parameters.
A terminal note: since a subroutine can only render a single value, it is customary to reserve a register to hold the value returned by a subroutine. The gas assembler for the m68k family unit uses register d0 to hold the returned value after a subroutine call. This topic will be treated at length in the lecture 17, "Interfacing C and 68k assembler".
15.4.two Passing Parameters via Memory Locations
- In this case a static memory location is used to serve the purpose of a mailbox: the calling program "deposits" the parameter(due south) in the given memory location and the subroutine then fetches them from this location. This is normally used to pass information to memory mapped i/o devices, as the interrupt handlers for these devices wait the data transferred to be in a given static location, which of course prohibits the use of a stack. Otherwise nosotros shall rarely encounter this technique as it is not very flexible, and the code generated doesn't support reentrancy. Furthermore using the stack is just as efficient and information technology supports reentrancy. The post-obit instance should make things clear.
Case 15.iv
Click here for the text file
MAILBOX: | retentivity location used to transfer information.discussion
...
movel d0, MAILBOX | put value of d0 in memory
bsr POSTMAN
POSTMAN: movel MAILBOX, ... | fetch the information from the retentivity location
...
rts
15.4.3 Passing Parameters via the Stack
- The stack is normally the preferred fashion to pass arguments to a subroutine. Although this technique is a bit more than involved in one case you lot become it correct, it is bullet proof, and allows ane to laissez passer as many parameters to a subroutine as desired. Using this standard also has the advantage of hiding from the principal program all the details of the subroutine (for example if passing via registers is implemented, then the calling program needs to know in what registers does the subroutine expects its parameters.)
- To pass parameters to a subroutine, the calling programme pushes them on the stack in the reverse order so that the last parameter to laissez passer is the offset one pushed, and the beginning parameter to pass is the last 1 pushed. This way the get-go parameter is on top of the stack and the last one is at the bottom of the stack. Most compilers exclusively pass parameters to a subroutines via the stack regardless of the number of parameters. In item the gcc compiler implements this scheme by default. For example, consider the post-obit call to the printf library function:
- printf("The first %s number is %d ", "Ramanujan", 1729);
| | ||
| Effigy 15.3: Parameters are pushed onto the stack in the opposite with respect to ordering in the role call. Since 1729 is an integer, information technology would exist encoded as 32 bits by well-nigh compilers for the m68k family. |
Every bit seen previously the instruction pea and the addressing modes sp@+ and sp@- facilitates the direction of the stack and are invariably used when parameters need to be stacked.
This web page was last updated June 25, 1998. Transport comments to cs273-feedback.
Copyright © McGill University, 1998. All rights reserved.
Reproduction of all or part of this work is permitted for educational or enquiry purposes provided that this copyright notice is included in any copy.
What Advantages Do Stack Parameters Have Over Register Parameters,
Source: https://www.cs.mcgill.ca/~cs573/fall2002/notes/lec273/lecture15/15_4.htm
Posted by: wrendrithad.blogspot.com

0 Response to "What Advantages Do Stack Parameters Have Over Register Parameters"
Post a Comment