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:
1 2 3 | struct MY_STRUC ;... ends |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | ;------ ; for test add: ;format PE GUI 5.0 ;section '.code' code import writeable readable executable ;include 'win32ax.inc' ;------ struct MY_STRUC var_struc rd 1 address_test rd 1 ;... ends ;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] rep movsb ret endp ;------ ; for test add: ;entry $ ;------ ; 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 |