cfunctionassemblycpu-registers

debugging the assembly equivalent of a c code to understand the function call


Just for my curiosity, I was looking on how the values passed to a function are actually operated by the called function. To make my doubt clear, I have an understanding that a compiler generates a code for a c code compiling it sequentially(Please correct if I am wrong). My doubt is how is the parameter value accessed in the called function? I mean parameter must be some a part of the calling function (like main() in my given example). How the compiler arrange that value passed in the calling function is same as the value accessed in the called function. To make my point clear, please look at the following code:

#include <stdio.h>

void check(int);

int main()
{
    check(9999);
}

void check(int a)
{
    int b;
    b = a;
}

In the above code after the execution of code, value of b = 9999; but how come the value of a in the function check() attain the value of 9999 at assembly level when the function check() is called from main(). Is it like parameters are stored in certain registers and accessed accordingly using those registers in the check(). I hope you understood my question.


Solution

  • calling conventions depend upon the ABI and the target processor (for your compiler).

    There is a wikipage on x86 calling convention. On Linux, you should read the x86-64 ABI. (Your a formal argument to check is passed in a register).

    The compiler is allowed to optimize your check to a nop (since its argument a has no observable effect)

    Read also wikipage on evaluation strategy: the C programming language requires a call-by-value semantics.

    Consider (if using GCC) compiling with gcc -fverbose-asm -S (perhaps also with optimizing options like -O2) and look inside the produced *.s assembly code.