0

I don't understand how stacks and register work during a recursion or normal function calls. Where the function's parameters & statement address got copy. The book says that compiler use stacks for this purpose but registers are used for that purpose. Here is the code

main()
{
int a = 5, b = 2, c;
c = add (a, b);
printf ("sum = %d", c); 
}
 
add(int i, int j)
{
 int sum;  
sum = i + j;
return sum ;  
}  
    

author says "Before transferring the execution control to the function add( ) the values of parameters a and b are pushed onto the stack. Following this the address of the statement printf( ) is pushed on the stack and the control is transferred to add( )." how return address statement works. How control returns to caller function?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
jonesalex
  • 31
  • 5
  • You'll want to read up on the `call` and `ret` instructions for x86. `call` places a return address on the stack so it can be returned to later, and `ret` pops it off the stack and jumps to it. – Carcigenicate Sep 06 '20 at 15:29
  • Note my comment above. This also depends on the Architecture. ARM uses a register for the return address, whereas x86 uses the stack. – Carcigenicate Sep 06 '20 at 15:31
  • You might want to take a look at [Calling Convention](https://en.wikipedia.org/wiki/Calling_convention) – sebastian Sep 06 '20 at 15:31
  • i saw a post on this site which says registers are used to store parameters and return address not stacks – jonesalex Sep 06 '20 at 15:32
  • @jonesalex For parameters that depends entirely on the convention used. It varies. Note the link that Sebastian posted. – Carcigenicate Sep 06 '20 at 15:33
  • @Carcigenicate i know that it is calling convention. Compiler stores the return address so it can return the control back to the caller function but why are the parameters get pushed onto the stack or copied on registers? – jonesalex Sep 06 '20 at 15:37
  • I'm not sure I understand the question. How would the function that's called get access to the parameters otherwise? – Carcigenicate Sep 06 '20 at 15:38
  • @ the book says when we call a function then before passing control to that sub-function the parameters pushed onto the stack and the return address of the next statement gets pushed on the stack as well – jonesalex Sep 06 '20 at 15:41
  • @Carcigenicate why do we have to push parameter onto the stack or register – jonesalex Sep 06 '20 at 15:42
  • Because you need to supply that information in a regulated way so that the function can access the information. If you didn't supply it in a specific way (by pushing or placing in a register), the function would need to start digging around the stack (or wherever else) to find the information it needs, which would be a mess. – Carcigenicate Sep 06 '20 at 15:45
  • @Carcigenicate so sub function access all the main function's information through stacks or registers? – jonesalex Sep 06 '20 at 15:55
  • Not necessarily all. The called function accesses whatever data the calling function gave to it. Although in most cases the amount of data given to the called function is static. – Carcigenicate Sep 06 '20 at 15:57
  • @Carcigenicate i am beginner in data-structure so i don't know many things . what is static data and how the main() transfer parameter's information to the sub-functions? – jonesalex Sep 06 '20 at 16:04
  • 1
    @jonesalex This has nothing to do with data-structures, but the compilation process of the code, which, as noted before is language, compiler and machine depended. For example, the stack pointer is stored usually in a dedicated register **in the CPU**. But it is so low-level and architecture depended, that you should not dive into it until you get more experienced. – Kerek Sep 11 '20 at 21:06
  • Modern calling conventions pass args in registers, only falling back to stack args for functions with more than 4 or 6 integer args. (Depending on the calling convention). [What registers are preserved through a linux x86-64 function call](https://stackoverflow.com/q/18024672). – Peter Cordes Sep 13 '20 at 16:32

0 Answers0