This article is aim to explain how to use Assemble Language realize those common function
In C. But I fail to get a simple method to introduce it because of some reasons. I will try
Extract some key point at this article. Then analysize a example in detail in next article.
(Of course, for those experienced guys, maybe a example is enough .)
1. How can we create local variable in a function?
/**
* How can we create local variable in a function?
*
* This is a interesting problem. First at all, what is the essence of a variable?
* The answer is space, memory space. Recall the reason that we introduced the concept
* The variable. We need those space to save some information. So, in fact, create a variable is
* Equal to allocate some space.
*
* Now, the question is we shoshould reside those variable in where?
* Actually, we has been know something about global variable and static variable. They are
* Arranged when compiled. But what about local variable? Where is it reside in?
* Obviusly, We can't arrange a special memory space for it when compile. Think about
* Recursive function. If we specify a special memory space for a local variable, how can we
* Ensure a function is reentrant. That will be awful, totally awful. So the conclusion is we
* Can't reside the local variable in a absolute address, no matter where. It shocould be reside
* In a relative address. If we build a base address for every function call, then every function
* Call will become independent. That is what we want theoretically. Actually, how can we
* Build that frame?
*
* Now, it is time to introduce a important design -- stack frame. It is helpful to realize
* Our plan above. Two special register, ESP and EBP, was introduced to build a frame for
* Function. In addition, it provide a nice solution for function call, too.
*
* The emphasis of this frame is stack. It is building down. % EBP was used to point
* The bottom of the frame. % ESP was used to point to the top of frame. That means if we
* Build a function like this:
* Int func ()
*{
*....
*}
*
* We will get a frame like this
* 0xbfffe6a0 <-- % EBP
*....
* 0xbfffe690 <-- % ESP
*
* If there are some local variable in the function, just like this:
* Int func ()
*{
* Int A, B, C;
*}
*
* The frame maybe:
* 0xbfffe6a0 <-- % EBP
* 0xbfffe69c <-- C, 4 bytes
* 0xbfffe698 <-- B, 4 bytes
* 0xbfffe694 <-- A, 4 bytes
* <-- % ESP
*
* We Have Been know how to create some local variable in assemble language.
*/
2. How can we call a function?
/*
* How can we call a function?
*
* This seems like simple. From the view of machine, call a function is equal to change
* Flow of instructions, is equal to revise the value of register % IP.
* Mov func, IP;
*
* But how can we return the caller? Maybe save the value of IP before revise is necessary
*. It seems we have two choice: save in a register, or save in the memory. Think about
* Current Situation, register is still finite, and function call is infinite logically. So the memory
* Will be a good choice.
*
* But where shocould be the place for those IP informations? I thought we cocould allocate
* Some memory space to build a special structure. It is used to save those information. Actually,
* A better way was introduced. See the following example, if we call a subroutine in main
* Function:
* Int main ()
*{
* Fun ();
*}
* Void fun ()
*{
*...
*}
*
*
* The value of pre-IP will be pushed into the stack and register % IP will be revise to
* Entry of fun function. The information will be saved like this.
* 0xbfffe6a0 <-- % EBP for main ()
* 0xbfffe69c <-- var 2
* 0xbfffe698 <-- var 1
* 0xbfffe694 <-- var 0
*....
* XXXXXXXX <-- return address
* <-- % Esp for main ()
*
* When we finish this subroutine, We cocould return the caller by pop the value into Register % IP.
*/
3. How can we pass parameters between caller and callee? /**
* How can we pass parameters between caller and callee?
*
* In C language, it is common to transmit data between caller and callee. How can we
* Realize it in assemble language? Need to say, every function call shoshould have a independent
* Parameters list, because we need our function is reentrant. See this example:
* Int main ()
*{
* Fun (1, 2, 3 );
*}
* Int fun (int A, int B, int C)
*{
*
*}
*
* Before we call fun (). The stack frame of main is:
* 0xbbbb00ff <-- % EBP for main
*... <-- Variables
*... <-- % Esp for main
*
*
* When we call this function, the following steps will be done.
* First at all, push the parameters into the stack,
* 0xbbbb00ff <-- % EBP for main
*... <-- Variables
* 0xbbbb00ec <-- 3, the last parameter
* 0xbbbb00e8 <-- 2, the second parameter
* 0xbbbb00e4 <-- 1, the first parameter
* <-- % Esp for main
*
* Then push the return address of caller,
* 0xbbbb00ff <-- % EBP for main
*... <-- Variables
* 0xbbbb00ec <-- 3, the last parameter
* 0xbbbb00e8 <-- 2, the second parameter
* 0xbbbb00e4 <-- 1, the first parameter
* 0xbbbb00e0 <-- return address of caller
* <-- % Esp for main
*
* Then jump to the function. It will build a new stack frame for this call. But before that,
* We need to save the information of caller frame. Because we will recover the frame of caller
* When we finish this subroutine. The frame is:
* 0xbbbb00ff <-- the base address of Main Frame
*... <-- Variables
* 0xbbbb00ec <-- 3, the last parameter
* 0xbbbb00e8 <-- 2, the second parameter
* 0xbbbb00e4 <-- 1, the first parameter
* 0xbbbb00e0 <-- return address of caller
* <-- The top address of Main Frame
* <-- % EBP for fun
* 0xbbbb00dc <-- the value of % EBP of main.
*... <-- Variables
* <-- % Esp for fun
*
* Based on those steps, we build a new frame for callee.
*/