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!
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.
Comments