大会备忘录

时间:2018-12-02 15:10:36

标签: c assembly x86-64 memmove

我正在尝试将此Memmove C代码进行汇编,而没有得到预期的结果。

我在xubuntu上使用x86-64程序集,调试了2个小时后,我看不到哪里错了。

C记忆代码:

#include <stdio.h> 

extern void * memmove(void *dest, void *src, size_t n);

int main () { 
  char str1[] = "Geeks";  // Array of size 6
  char str2[] = "Quiz";   // Array of size 5 

  puts("str1 before memmove "); 
  puts(str1); 

  /* Copies contents of str2 to sr1 */
  memmove(str1, str2, sizeof(str2));

  puts("\nstr1 after memmove "); 
  puts(str1); 

  return 0; 
} 
/*

void * memmove(void *dest, void *src, size_t n) { 

    char *d = (char *) dest; 
    char *s = (char *) src; 

    if(s == d)
        return dest;

    if(s < d) { 
        //copy from back 
        s=s+n-1; 
        d=d+n-1; 
        while(n--) { 
            *d-- = *s--; 
        } 
    } 
    else { 
        //copy from front 
        while(n--) 
            *d++ = *s++; 
    } 
    return dest; 
} */

组装代码:

.globl memmove

# RDI = dest
# RSI = src
# RDX = n

# R8 = d
# R9 = s

memmove:
    mov     %rdi, %r8       # d = dest
    mov     %rsi, %r9       # s = src
    jmp     if_equal

if_equal:
    cmp     %r8, %r9        # s == d
    jz      retDest
    ja      else            # s > d
    jb      if_s_minor      # s < d

if_s_minor:
    add     %rdx, %r9       # s = s + n
    sub     $1, %r9         # s = s - 1

    add     %rdx, %r8       # d = d + n
    sub     $1, %r8         # d = d - 1

    jmp     while1

while1:
    cmp     $0, %rdx        # n > 0 ?
    jna     retDest         # if n <= 0 go to retDest
    sub     $1, %rdx        # n--

    movb    (%rsi), %cl     # *dst-- = *src--
    movb    %cl, (%rdi) 

    leaq    -1(%r8), %r8    # *d--
    leaq    -1(%r9), %r9    # *s--
    jmp     while1

else:
    jmp     while2

while2:
    cmp     $0, %rdx        # n > 0 ?
    jna     retDest
    sub     $1, %rdx        # n--

    movb    (%rsi), %cl     # *dst = *src
    movb    %cl, (%rdi) 

    leaq    1(%r8), %r8 # *d++
    leaq    1(%r9), %r9 # *s++
    jmp     while2

retDest:
    mov     %rdi, %rax
    ret

.end

本来应该在第二张照片上显示“ Quiz”,但它显示了这一点:

str1在记忆之前 极客

记忆后的

str1 Qeeks

1 个答案:

答案 0 :(得分:1)

movb    (%rsi), %cl     # *dst = *src
movb    %cl, (%rdi) 
leaq    1(%r8), %r8 # *d++
leaq    1(%r9), %r9 # *s++
jmp     while2

这里的问题是您的代码更改了%r8%r9寄存器,但是实际移动停留中使用的%rdi%rsi寄存器相同,从而一次又一次地重复复制相同的字节!

while1 代码中当然也存在同样的问题。