x86 Func)on Call Conven)ons 1 Register use in the stack - - PowerPoint PPT Presentation

x86 func on call conven ons
SMART_READER_LITE
LIVE PREVIEW

x86 Func)on Call Conven)ons 1 Register use in the stack - - PowerPoint PPT Presentation

x86 Func)on Call Conven)ons 1 Register use in the stack frame %ESP - Stack Pointer This 32-bit register always points to the last element used on the stack. %EBP - Base Pointer This 32-bit register is used to reference all


slide-1
SLIDE 1

x86 ¡Func)on ¡Call ¡Conven)ons ¡

1 ¡

slide-2
SLIDE 2

Register use in the stack frame

%ESP - Stack Pointer This 32-bit register always points to the last element used on the stack. %EBP - Base Pointer This 32-bit register is used to reference all the function parameters and local variables in the current stack frame. %EIP - Instruction Pointer This holds the address of the next CPU instruction to be executed. It is saved onto the stack as part of the CALL instruction.

2 ¡

slide-3
SLIDE 3

3 ¡

Stack frame

ebp ¡ esp ¡ Old ¡eip ¡ Parameters ¡

Before entry

3 ¡

ebp ¡ esp ¡ Local ¡ variables ¡ Old ¡ebp ¡ Old ¡eip ¡ Parameters ¡ ebp ¡-­‑ ¡4 ¡ ebp ¡+ ¡8 ¡ ebp ¡+ ¡4 ¡

After entry

Stack ¡frame ¡

slide-4
SLIDE 4

Calling a function

1. Push parameters onto the stack, from right to left.

  • 2. Call the function. The contents of the %EIP

(instruction pointer) is pushed onto the stack. It points to the first byte after the CALL instruction.

4 ¡ 4 ¡

slide-5
SLIDE 5
  • 3. Save and update the %ebp.

pushl %ebp

  • movl %esp, %ebp

Executing a function

4.Save CPU registers used for temporaries.

  • 5. Allocate local variables.
  • 6. Perform the function's purpose.

Store return value, if any, in %eax.

5 ¡

slide-6
SLIDE 6
  • 8. Release local storage.
  • movl %ebp, %esp
  • 9. Restore the old base pointer.
  • popl %ebp
  • 10. Return from the function.

ret

  • 11. Clean up pushed parameters.

The caller must clean up the parameters pushed

  • nto the stack.

6 ¡

leave

  • 7. Restore saved CPU registers.
slide-7
SLIDE 7

An example

int main() { int i = 7; int j = 13; swap(&i, &j); } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }

7 ¡

slide-8
SLIDE 8

Stack frame

ebp ¡ esp ¡ Old ¡eip ¡ Parameters ¡

Before entry &j &i

ebp ¡ esp ¡ Local ¡ variables ¡ Old ¡ebp ¡ Old ¡eip ¡ Parameters ¡ ebp ¡-­‑ ¡4 ¡ ebp ¡+ ¡8 ¡ ebp ¡+ ¡4 ¡

After entry temp

8 ¡