title: in-memory copying in assembly
body: <p>First up, using NASM and the target is x86, and running in 16-bit Real Mode of x86.
  I want to copy code at one memory location to another, and then do a call/jmp to the destination, so I can execute the code there. The code is initially at 0x1000:0x0 (segment:offset). Now, I want to copy it to, say 0x3000:0x0. And the code is 512 bytes in size, exactly. I'm trying to do this with following routine.</p>
  
  <pre><code>org 0x500
  
  ;The code to be copied is located at 0x1000:0x0. We want to copy it to 0x3000:0x0
  
  copy:
  
    mov esi,0x1000  ; source address of the code in DS:SI
    mov edi,0x3000  ; destination address of the code in ES:DI
    mov ecx,0x200   ; size of the code, 512 bytes (0x200)
    rep movsb       ; copy bytes from address in SI to address in DI.
  
    jmp 0x3000:0x0  ; jump to the destination , and execute the code there.
  </code></pre>
  
  <p>The code being copied just prints a string. So if the above snippet works, I would see that string on screen. Also, I have verified that the copied code is working , and that it is indeed present at 0x1000:0x0, so no such obvious/silly mistakes made.
  The above routine is failing for some reason. The possible points of failure, in my opinion, could be wrong addresses given. I'm not sure of what to put in SI and DI before the copying. Should these be offsets or the actual address? The documentation doesn't make this clear. Also, should I be explicitly initializing ES and DS?</p>
  
  <p>I have tried various combinations of things to try to make this work, but to no avail. One of them was this:</p>
  
  <pre><code>org 0x500
  
  ;The code to be copied is located at 0x1000:0x0. We want to copy it to 0x3000:0x0
  
  copy:
    mov bx,0x1000
    mov ds,bx       ; set DS explicitly to 0x1000.
    mov esi,0x0     ; source address of the code in DS:SI (0x1000:0x0)
    mov bx,0x3000
    mov es,bx       ; set ES explicitly to 0x3000
    mov edi,0x0     ; destination address of the code in ES:DI (0x3000:0x0)
    mov ecx,0x200   ; size of the code, 512 bytes (0x200)
    rep movsb       ; copy bytes from address in SI to address in DI.
  
    jmp 0x3000:0x0  ; jump to the destination , and execute the code there.
  </code></pre>
  
  <p>So here I explicitly set ES:DI and DS:SI. This also did not work. I also tried giving the actual physical addresses to SI and DI, failed. Now, I'm out of options. I'm sure there's some conceptual memory addressing mistake being made here, but I'm not able to catch it.
  (And yeah, the code copied is 512 bytes in size, raw binary).</p>
  
  <p>Thanks.</p>