ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

harfangeek

I'd like to know what is the best method to save common registers (EAX, EBX and so on) in addition with EBP. Here is my sample code. It juste make a call to my write function, passing the string adress and lenght in parameter (parameters are put in the stack). In the write function, I save EBP, give it his new value, get back the parameters and call the write syscall.

.section .data

    ask_number_str:
        .ascii "Hello world"
    anstr_end:
        .set ANSTR_SIZE, anstr_end - ask_number_str


.section .bss

.section .text

    .globl main

    main:
        movl $42, %eax
        movl $123, %ebx
        movl $456, %ecx
        movl $789, %edx
        pushl $ANSTR_SIZE
        pushl $ask_number_str
        call write
        add $8, %esp

    exit:
        movl $1, %eax
        movl $0, %ebx
        int $0x80

    write:
        pushl %ebp
        movl %esp, %ebp        
        movl $4, %eax
        movl $1, %ebx
        movl 8(%ebp), %ecx
        movl 12(%ebp), %edx
        int $0x80
        popl %ebp
        ret

I'd like to add the save of EAX, EBX, ECX and EDX on function call. I see three ways to save the common registers:

  • First, save them in the write function, before the "mov %esp, %ebp" (before assigning the new adress for ebp). => Problem : ESP, and so EBP will point in the stack after the common register values, so I will have to go deeper in the stack to find the parameters (I will have to pass through the values of EAX, EBX, ECX, EDX before finding the parameters)

  • Second, save them in the write function, after the "mov %esp, %ebp". => Problem : ESP, and so EBP will point in the stack before the common register values, so I will have to go furtherer in the stack to play with local variables (I will have to pass the 4 values, if I save EAX, EBX, ECX and EDX, to have the beginning adress of my local variables).

  • Third, save them in the calling function (here the main), before the function call (and before the push of parameters). No problem of stack, but this is not very elegant I think, and I need to add 8 lines for each call (4 push before the call and 4 pop after).

So, what is for you the best method? Surely another method I didn't think of :)

Thank you for your help!

Jester

If you have decided to use a frame pointer, the usual thing to do is save them after setting up ebp. Your saved registers are not different from any local variables, you can even access them with mov instructions instead of push/pop if you like and you may put them under your other variables if you don't want to use bigger offsets.

Note that frame pointers are usually not required as you can address relative to esp. Also, common calling conventions typically allow eax, ecx and edx to be modified, so if you adhere to such all you need to save is ebx.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

From Dev

ASM space optimization: EAX vs EBX

From Dev

Latency semantics of read registers on x86 ASM

From Dev

Why does x86 architecture use two stack registers (esp ; ebp)?

From Dev

Writing to stack as local variable in _start function (x86 ASM)

From Dev

What are the ESP and the EBP registers?

From Dev

call in x86 real mode does not save return address

From Dev

(x86) Is the value of ESP realtive to EBP, or not?

From Dev

How can I call this x86 ASM CALL in C++ with typedef or inline

From Dev

What registers are preserved through a linux x86-64 function call

From Dev

x86_64 registers rax/eax/ax/al overwriting full register contents

From Dev

What is the best way to save function arguments and call the function later?

From Dev

Accessing function args from the stack relative to EBP while pushing/popping other registers?

From Dev

x86 Assembler - Which registers to push?

From Dev

Assembly x86 registers signed or unsigned

From Dev

ASM x86 Push and pop

From Dev

x86 ASM - Read line by line

From Dev

ASM x86 Push and pop

From Dev

ROL in x86 ASM in PROC parameter

From Dev

asm X86 - segmentation fault?

From Dev

What is the state of the registers after a function call?

From Dev

What is the state of the registers after a function call?

From Dev

Does the return value always go into eax register after a method call?

From Dev

x86 assembly code to put string in EAX register

From Dev

Does __asm{}; return the value of eax?

From Dev

ASM Java replace method call instruction

From Dev

ASM Java replace method call instruction

From Dev

Understanding pre/post assembly code for a function call in x86 IA32 assembly

From Dev

Assembly x86 convert to ARM function call with varying number of parameters to Arm

Related Related

  1. 1

    ASM x86 Function call : best method to save EBP and common registers (EAX, EBX, ...)

  2. 2

    ASM space optimization: EAX vs EBX

  3. 3

    Latency semantics of read registers on x86 ASM

  4. 4

    Why does x86 architecture use two stack registers (esp ; ebp)?

  5. 5

    Writing to stack as local variable in _start function (x86 ASM)

  6. 6

    What are the ESP and the EBP registers?

  7. 7

    call in x86 real mode does not save return address

  8. 8

    (x86) Is the value of ESP realtive to EBP, or not?

  9. 9

    How can I call this x86 ASM CALL in C++ with typedef or inline

  10. 10

    What registers are preserved through a linux x86-64 function call

  11. 11

    x86_64 registers rax/eax/ax/al overwriting full register contents

  12. 12

    What is the best way to save function arguments and call the function later?

  13. 13

    Accessing function args from the stack relative to EBP while pushing/popping other registers?

  14. 14

    x86 Assembler - Which registers to push?

  15. 15

    Assembly x86 registers signed or unsigned

  16. 16

    ASM x86 Push and pop

  17. 17

    x86 ASM - Read line by line

  18. 18

    ASM x86 Push and pop

  19. 19

    ROL in x86 ASM in PROC parameter

  20. 20

    asm X86 - segmentation fault?

  21. 21

    What is the state of the registers after a function call?

  22. 22

    What is the state of the registers after a function call?

  23. 23

    Does the return value always go into eax register after a method call?

  24. 24

    x86 assembly code to put string in EAX register

  25. 25

    Does __asm{}; return the value of eax?

  26. 26

    ASM Java replace method call instruction

  27. 27

    ASM Java replace method call instruction

  28. 28

    Understanding pre/post assembly code for a function call in x86 IA32 assembly

  29. 29

    Assembly x86 convert to ARM function call with varying number of parameters to Arm

HotTag

Archive