开发者

Copying Array Content to Another Array in Assembly

开发者 https://www.devze.com 2023-02-15 11:27 出处:网络
I\'m looking to copy some elements of an array to another one in Assembly. Both arrays are accessed via pointers which are stored in registers. So, edx would be pointing to one array and eax would poi

I'm looking to copy some elements of an array to another one in Assembly. Both arrays are accessed via pointers which are stored in registers. So, edx would be pointing to one array and eax would point to another. Basically, edx points to an array of character read in from a text file, and I'd like eax to contain only 32 of the characters. Here's what I'm attempting to do:

I386 Assembly using NASM

add edx, 8 ; the first 8 characters of the string are not wanted

mov cl, 32
ip_address:
; move the character currently pointed to by edx to eax (mov [eax], [edx])
inc edx
inc eax
loop ip_address

Again, i'd like this to place the 32 characters after the first eight to be placed in the second ar开发者_运维百科ray. The problem is that I'm stumped on how to do this.. Any help is very much appreciated.


You can't do direct memory-to-memory moves in x86. You need to use another scratch register:

mov ecx, [edx]
mov [eax], ecx

Or something like that...


Both ia32 and ia64 do contain a memory-to-memory string move instruction that can move bytes, "words", and "doublewords".

movsb
movsw
movsd

The source address is specified in ESI and the destination in EDI.1 By itself, it moves one byte, word, or doubleword. If the rep prefix is used, then ECX will contain a count and the instruction will move an entire string of values.


1. I think these instructions are the reason that the ESI and EDI registers are so named. (Source Index and Destination Index.)


The simple solution is to just do:

mov  ebx, [edx]
mov [eax], ebx

Be aware that under many platform's ABIs, ebx is a callee-save register, so you will need to save and restore its value in your function.

The simpler solution is to link against the standard library and call memcpy, which is perfectly acceptable in assembly, and will usually be substantially faster than writing your own loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消