I am trying to port a 32bit assembly function to 64bit. Its save_context and restore_restore. Basically porting from i386 to x86_64
So here is my porting
32bit version :
.align 4,0x90
.global save_context
.type save_context,@function
save_context:
movl 4(%esp),%eax
movl %ebx, 12(%eax)
movl %esi, 16(%eax)
movl %edi, 20(%eax)
movl %ebp, 24(%eax)
movl %esp, 28(%eax)
movl 0(%esp), %edx
movl %edx, 0(%eax)
xorl %eax,%eax
incl %eax
ret
FUNCTION CALL IN MAIN :
u_int32_t context[10];
if (cpu_save_context(context) == TRUE) {
// do something
}
gdb >> p context
$2 = {<some value>, 0, 0, <some value>, 0, 0, <some value>, <some value>, 0, 0}
64bit Version:
.align 4,0x90
.global save_context
.type save_context,@function
cpu_save_context:
mov %rdi,%rax /* Get our context pointer from rdi */
/* Don't need to save A */
mov %rbx, 24(%rax) /* Save B */
mov %r12, 32(%rax) /* Save r12 */
mov %r13, 40(%rax) /* Save r13 (8*3+16)*/
mov %r14, 48(%rax) /* Save r13 */
mov %r15, 56(%rax) /* Save r13 */
mov %rbp, 72(%rax) /* Save frame pointer */
mov %rsp, 88(%rax) /* Save stack pointer */
mov 8(%rbp), %r10 /* Fetch our return address */
mov %r10, 8(%rax) /* Save our return address */
xor %rax,%rax /* Construct return code of 1 */
inc %rax
ret
FUNCTION CALL IN MAIN :
u_int64_t context[10]; // i have tried increasing 10->20 and 40//still save errors
if (cpu_save_context(context) == TRUE) {
// do something
}
gdb >> p context
$2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
So the functions works for 32bit version, but for 64 bit no such luck. I have posted the similar question and they believe my porting of assembly code is fine. Segmentation fault in assembly code + C
Can someone help debugging this .