When running a program you can pass paramters, e.g.
$ myProgram par1 par2 par3
In C you can access these paramters by looking at argv,
int main (int argc, char *argv[])
{
char* aParameter = argv[1]; // Not sure if this is 100% right but you get the idea...
}
How would this translate in assembly / x86 machine code? How would you access the variables given to you? How would the system give you these variables?
Im very new to assembly, it seams you can only access registers and absolute addresses. I am puzzled how you could access parameters. Does the system preload the parameters into a special register for you?
Function calls
Parameters are usually passed on the stack, which is a part of memory that is pointed to by
esp. The operating system is responsible for reserving some memory for the stack and then setting upespproperly before passing control to your program.A normal function call could look something like this:
There are different responsibilities split between the calling function and the function that is being called, with regards to how they promise to preserve registers. These rules are referred to as calling conventions.
The example above uses the cdecl calling convention, which means that parameters are pushed onto the stack in reverse order, and the calling function is responsible for restoring
espback to where it pointed before those parameters were pushed to the stack. That’s whatadd esp, 8does.Main function
Typically, you write a
mainfunction in assembly and assemble it into an object file. You then pass this object file to a linker to produce an executable.The linker is responsible for producing startup code that sets up the stack properly before control is passed to your
mainfunction, so that your function can act as if it were called with two arguments (argc/argv). That is, yourmainfunction is not the real entry point, but the startup code jumps there after it has set up the argc/argv arguments.Startup code
So how does this “startup code” look? The linker will produce it for us, but it’s always interesting to know how stuff works.
This is platform specific, but I’ll describe a typical case on Linux. This article, while dated, explains the stack layout on Linux when an i386 program starts. The stack will look like this:
So the startup code can get the argc/argv values from the stack and then call
main(...)with two parameters: