Question

UNIX Functions and Stack, I will give good ratings if answered properly

Function calls in UNIX Write assembly code to impl

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This would be the contents of the stack if we have a function foo with the prototype:

   int foo (int arg1, int arg2, int arg3) ;
and foo has two local int variables. (We are assuming here that sizeof(int) is 4 bytes). The stack would look like this if say the main function calledfoo and control of the program is still inside the function foo. In this situation, main is the "caller" and foo is the "callee".

The ESP register is being used by foo to point to the top of the stack. The EBP register is acting as a "base pointer". The arguments passed by mainto foo and the local variables in foo can all be referenced as an offset from the base pointer.

The convention used here is that the callee is allowed to mess up the values of the EAX, ECX and EDX registers before returning. So, if the caller wants to preserve the values of EAX, ECX and EDX, the caller must explicitly save them on the stack before making the subroutine call. On the other hand, the callee must restore the values of the EBX, ESI and EDI registers. If the callee makes changes to these registers, the callee must save the affected registers on the stack and restore the original values before returning.

Parameters passed to foo are pushed on the stack. The last argument is pushed first so in the end the first argument is on top. Local variables declared in foo as well as temporary variables are all stored on the stack.

Return values of 4 bytes or less are stored in the EAX register. If a return value with more than 4 bytes is needed, then the caller passes an "extra" first argument to the callee. This extra argument is address of the location where the return value should be stored. I.e., in C parlance the function call:

   x = foo(a, b, c) ;
is transformed into the call:
   foo(&x, a, b, c) ;
Note that this only happens for function calls that return more than 4 bytes.

Let's go through a step-by-step process and see how a stack frame is set up and taken down during a function call.

ESP ==> 1pix.gif .
.
.
1pix.gif

Callee saved registers
EBX, ESI & EDI
(as needed)

  
temporary storage

local variable #2 [EBP - 8]

local variable #1 [EBP - 4]

EBP ==> Caller's EBP

Return Address

Argument #1 [EBP + 8]

Argument #2 [EBP + 12]

Argument #3 [EBP + 16]

Caller saved registers
EAX, ECX & EDX
(as needed)

.
.
.

Fig. 1


The caller's actions before the function call

1pix.gif 1pix.gif

ESP ==> Return Address

Arg #1 = 12

Arg #2 = 15

Arg #3 = 18

Caller saved registers
EAX, ECX & EDX
(as needed)
1pix.gif
EBP ==> .
.
.

Fig. 2

In our example, the caller is the main function and is about to call a function foo. Before the function call, main is using the ESP and EBP registers for its own stack frame.

First, main pushes the contents of the registers EAX, ECX and EDX onto the stack. This is an optional step and is taken only if the contents of these 3 registers need to be preserved.

Next, main pushes the arguments for foo one at a time, last argument first onto the stack. For example, if the function call is:

     a = foo(12, 15, 18) ;

The assembly language instructions might be:

        push    dword 18 
        push    dword 15
        push    dword 12

Finally, main can issue the subroutine call instruction:

        call    foo

When the call instruction is executed, the contents of the EIP register is pushed onto the stack. Since the EIP register is pointing to the next instruction in main, the effect is that the return address is now at the top of the stack. After the call instruction, the next execution cycle begins at the label named foo.

Figure 2 shows the contents of the stack after the call instruction. The red line in Figure 2 and in subsequent figures indicates the top of the stack prior to the instructions that initiated the function call process. We will see that after the entire function call has finished, the top of the stack will be restored to this position.


The callee's actions after function call

When the function foo, the callee, gets control of the program, it must do 3 things: set up its own stack frame, allocate space for local storage and save the contents of the registers EBX, ESI and EDI as needed.

So, first foo must set up its own stack frame. The EBP register is currently pointing at a location in main's stack frame. This value must be preserved. So, EBP is pushed onto the stack. Then the contents of ESP is transferred to EBP. This allows the arguments to be referenced as an offset from EBP and frees up the stack register ESP to do other things. Thus, just about all C functions begin with the two instructions:

        push    ebp
        mov     ebp, esp
The resulting stack is shown in Figure 3. Notice that in this scheme the address of the first argument is 8 plus EBP, since main's EBP and the return address each takes 4 bytes on the stack.
1pix.gif 1pix.gif

ESP=EBP => main's EBP

Return Address

Arg #1 = 12 [EBP + 8]

Arg #2 = 15 [EBP + 12]

Arg #3 = 18 [EBP + 16]

Caller saved registers
EAX, ECX & EDX
(as needed)
1pix.gif

Fig. 3

In the next step, foo must allocate space for its local variables. It must also allocate space for any temporary storage it might need. For example, some C statements in foo might have complicated expressions. The intermediate values of the subexpressions must be stored somewhere. These locations are usually called temporary, because they can be reused for the next complicated expression. Let's say for illustration purposes that foohas 2 local variables of type int (4 bytes each) and needs an additional 12 bytes of temporary storage. The 20 bytes needed can be allocated simply by subtracting 20 from the stack pointer:
        sub     esp, 20
The local variables and temporary storage can now be referenced as an offset from the base pointer EBP.

Finally, foo must preserve the contents of the EBX, ESI and EDI registers if it uses these. The resulting stack is shown in Figure 4.

The body of the function foo can now be executed. This might involve pushing and popping things off the stack. So, the stack pointer ESP might go up and down, but the EBP register remains fixed. This is convenient because it means we can always refer to the first argument as [EBP + 8] regardless of how much pushing and popping is done in the function.

Execution of the function foo might also involve other function calls and even recursive calls to foo. However, as long as the EBP register is restored upon return from these calls, references to the arguments, local variables and temporary storage can continue to be made as offsets from EBP.

1pix.gif 1pix.gif

ESP ==> Callee saved registers
EBX, ESI & EDI
(as needed)

  
temporary storage
[EBP - 20]

local variable #2 [EBP - 8]

local variable #1 [EBP - 4]

EBP==> main's EBP

Return Address

Arg #1 = 12 [EBP + 8]

Arg #2 = 15 [EBP + 12]

Arg #3 = 18 [EBP + 16]

Caller saved registers
EAX, ECX & EDX
(as needed)
1pix.gif

Fig. 4


The callee's actions before returning

1pix.gif 1pix.gif

ESP ==> Arg #1 = 12

Arg #2 = 15

Arg #3 = 18

Caller saved registers
EAX, ECX & EDX
(as needed)
1pix.gif
EBP ==> .
.
.

Fig. 5

Before returning control to the caller, the callee foo must first make arrangements for the return value to be stored in the EAX register. We already discussed above how function calls with return values longer than 4 bytes are transformed into a function call with an extra pointer parameter and no return value.

Secondly, foo must restore the values of the EBX, ESI and EDI registers. If these registers were modified, we pushed their original values onto the stack at the beginning of foo. The original values can be popped off the stack, if the ESP register is pointing to the correct location shown in Figure 4. So, it is important that we do not lose track of the stack pointer ESP during the execution of the body of foo --- i.e., the number of pushes and pops must be balanced.

After these two steps we no longer need the local variables and temporary storage for foo. We can take down the stack frame with these instructions:

        mov     esp, ebp
        pop     ebp

The result is a stack that is exactly the same as the one shown in Figure 2. The return instruction can now be executed. This pops the return address off the stack and stores it in the EIP register. The result is the stack shown in Figure 5.

The i386 instruction set has an instruction "leave" which does exactly the same thing as the mov and pop instructions above. Thus, it is very typical for C functions to end with the instructions:


        leave
        ret
Add a comment
Know the answer?
Add Answer to:
UNIX Functions and Stack, I will give good ratings if answered properly Function calls in UNIX...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Note that the main function that I have provided does use <string.h> as it constructs test...

    Note that the main function that I have provided does use <string.h> as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the <string.h> library. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not *...

  • Write a program in C using Unix system calls and functions that will change the permissions on a ...

    Write a program in C using Unix system calls and functions that will change the permissions on a file. The executable shall be called “mychmod” and will be executed by: mychmod -u rwx -g rwx -o rwx -U rwx -G rwx -O rwx file1 file2 ... The lowercase options will add permissions while the uppercase options will remove permissions. Each of the switches is optional and should be interpreted as the ones in the Unix command chmod(1), you can review...

  • Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The...

    Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The functions are described below on page 2. (Just write the prototypes) When necessary, use the variables declared below in maino. mm 1.) showMenu m2.) getChoice 3.) calcResult m.) showResult 5.) getInfo mm.) showName 7.) calcSquare 8.) ispositive int main { USE THESE VARIABLES, when needed, to write function prototypes (#1 - #8) double num1 = 1.5; double num2 = 2.5; char choice; double result;...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT