简体   繁体   中英

converting assembly code to x86_64

I need help porting a i386 assemble file to x86_64. Fairly new to assembly code so need help with this effort.

So here is the old assembly file. And below that is the ported version of mine (all i did was change register type). I get a segmentation fault when i run it on x86_64 Arch. Any idea why ? Anything I am missing while porting this. This function is called from main and the value passed to it are

argv=0xffffd954 --> for i386 argv=0x7fffffffe7b8 --> for x86_64

// i386 register_swap

.align  4,0x90
.global register_swap
.type   register_swap,@function

register_swap:

   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

// x86_64 register_swap

.align  4,0x90
.global register_swap
.type   register_swap,@function

register_swap:

mov    4(%rsp),%rax     /* Get our context pointer */
                            /* Don't need to save A */
mov    %rbx, 12(%rax)       /* Save B */

mov     %rsi, 16(%rax)      /* Save SI */
mov     %rdi, 20(%rax)      /* Save DI */
mov     %rbp, 24(%rax)      /* Save frame pointer */
mov     %rsp, 28(%rax)      /* Save stack pointer */

mov     0(%rsp), %rdx       /* Fetch our return address */
mov     %rdx,  0(%rax)      /* Save our return address */

xor     %rax,%rax       /* Construct return code of 1 */
inc     %rax

ret

Registers are twice as large in x86_64: 8 bytes instead of 4. You will have to change the offsets in your code.

.align  4,0x90
.global register_swap
.type   register_swap,@function

register_swap:

mov    8(%rsp),%rax     /* Get our context pointer */
                            /* Don't need to save A */
mov    %rbx, 24(%rax)       /* Save B */

mov     %rsi, 32(%rax)      /* Save SI */
mov     %rdi, 40(%rax)      /* Save DI */
mov     %rbp, 48(%rax)      /* Save frame pointer */
mov     %rsp, 56(%rax)      /* Save stack pointer */

mov     0(%rsp), %rdx       /* Fetch our return address */
mov     %rdx,  0(%rax)      /* Save our return address */

xor     %rax,%rax       /* Construct return code of 1 */
inc     %rax

ret

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM