The goal of this tutorial is to show how to copy memory using the destination source and registers.
Copy memory is a complex issue and depends on the rule of addressing.
In this case I used the registers: edi, esi, ecx.
My example uses also a structure to understand another step into assembly programming with FASM the structure area.
This structure named MY_STRUC starts with this template:
I create a procedure named copy_memory to copy memory.
The source code has commented lines that let you see how this working.
Some parts of these comments let you create an executable to test with the debugger.
The base of this copy memory issue is the assembly instruction named movsb.
The movsb moves only a single byte from the source string to the destination.
To move the entire string, first, initialize ecx to the number N of bytes in the source string and execute rep movsb.
As you know the ecx is a 32-bit register.
Let’s see the example:
; for test add:
;format PE GUI 5.0
;section '.code' code import writeable readable executable
var_struc rd 1
address_test rd 1
;fasm code for a structure named MY_STRUC start with a var_struc
;procedure fasm for copy memory using the destination and source
proc copy_memory uses edi esi ecx, source, dest, bytes
;one random hexa input address for testing the source
address_test MY_STRUC <0xf000>
var_struc MY_STRUC <>
mov edi, [dest]
mov esi, [source]
mov ecx, [bytes]
; for test add:
; call using the stdcall of the procedure with the var structure
; the content of var_struc will be put into add address_test
stdcall copy_memory, addr address_test, addr var_struc, sizeof.MY_STRUC