assemblymemmove

Problem of "cyclic data " in x86_64 assembly code


I'm trying to write memmove in assembly: the problem is that in my section data I've got two line : source and destination, and when I'm trying to pass 20 byte from source to destination while source got just 15 byte the 16th byte that pass from source to destination need to be the first byte of destination but the first byte of source passed,

  1. In the first picture we can see that RAX is on 21 then the data that need to pass is source+21 = 43,
  2. We can see that the result of source+21 is 176(176 = source+0) and not 43 like it is supposed to be,
  3. In the last pic we can see that in RCX there is source + 22 = 122(122= source+1) and suppose to be 161

It's look like the code is cyclic , thanks for your help!

in the first picture we can see that rax is on 21 then the data that need to pass is source+21 = 43 we can see that the result of source+21 is 176 and not 43 like it suppose to be in the last pic we can see that in rcx there is source + 22= 122 and suppose to be 161


Solution

  • The source and destination arrays follow each other in memory. There's no gap between them.

    The very first transfer that your copying loop does will read the first byte from source (176) and write it over the first byte of destination (43). Hereafter the value 43 is gone forever!
    When you inspect source+21, which is the same as destination+0, only the value 176 remains.

    The second transfer that your copying loop does will read the second byte from source (122) and write it over the second byte of destination (161). Hereafter the value 161 is gone forever!
    When you inspect source+22, which is the same as destination+1, only the value 122 remains.