News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Strange mov....

Started by SCHiM, October 23, 2010, 06:53:09 PM

Previous topic - Next topic

SCHiM

I'm back again, this time with a question regarding the mov command
I want to copy the contents of one buffer into another
Here's the code I'm currently using:

xor esi, esi
xor eax, eax
add eax, offset PRVMS

cpy:
mov [SendBuffer + esi], eax

inc eax
inc esi

cmp esi, sizeof PRVMS
jne cpy


I've looked up the mov command, and I should be able to do this, but the assembler tells me that I'm providing invailid instruction operands
can someone help me?

Twister

mov dword ptr [SendBuffer + esi], eax

SCHiM

Quote from: Baluga Boo on October 23, 2010, 06:56:42 PM
mov dword ptr [SendBuffer + esi], eax

Why does that work?
using the ptr [] thingy forces the the cpu to treat Sendbuffer + esi as a dword right?
But how can that my address (SendBuffer) isn't 32 bits already?

dedndave

he's right - DWORD PTR should not be required
although, i might write it as...
        mov     SendBuffer[esi],eax

i see a couple other things, though
i think INC ESI should be ADD ESI,4
that way, each dword in the array is updated, instead of overwriting part of the previous one
well - not sure what you are doing there
maybe you just want byte values into the buffer
if that is the case, and speed is important, i might write 4 bytes at a time to 4-aligned addresses

also, in the initialization code:
        xor     eax,eax
        add     eax,offset PRVMS


can simply be:
        mov     eax,offset PRVMS

Coma


invoke  RtlMoveMemory,ADDR BufferDestination,ADDR BufferSource,sizeof BufferSource
Coma Homepage 20:00-23:00 GMT+2 --- http://ge2001.dyndns.org:44792

SCHiM

Quote from: Coma on October 23, 2010, 07:29:22 PM

invoke  RtlMoveMemory,ADDR BufferDestination,ADDR BufferSource,sizeof BufferSource


That would certainly do the job, but I want to learn from it ;)
I already program in C++ so using api's isn;t exacly what I could call learning

dedndave

well - that will copy a buffer
you seem to be generating one

but, it doesn't make sense, really
the values in EAX are dwords - and reflect the value of a dword address (offset PRVMS plus some index)
you are incrementing into the array by byte offsets
the SIZEOF operator returns the defined length in bytes

if you want to place bytes, i suggest creating 4 bytes in EAX, then storing them

raymond

QuoteI want to copy the contents of one buffer into another

If that is really what you want to do, here's the simplest way.

lea  esi,source_buffer
lea  edi, destination_buffer
mov  ecx,sizeof source_buffer
rep  movsb


What your assembler probably did not like may have been due to the fact that your destination array SendBuffer must have been declared as a BYTE array into which you were trying to move DWORDS (mov [SendBuffer + esi], eax).

And, if you correct it to move DWORDS as suggested by the "dword ptr" addition, what you would essentially be getting with your code is filling the destination array with an incrementing byte, starting with the low byte of the offset of your source array.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

SCHiM

Quote from: raymond on October 24, 2010, 02:31:20 AM
QuoteI want to copy the contents of one buffer into another

If that is really what you want to do, here's the simplest way.

lea  esi,source_buffer
lea  edi, destination_buffer
mov  ecx,sizeof source_buffer
rep  movsb


What your assembler probably did not like may have been due to the fact that your destination array SendBuffer must have been declared as a BYTE array into which you were trying to move DWORDS (mov [SendBuffer + esi], eax).

And, if you correct it to move DWORDS as suggested by the "dword ptr" addition, what you would essentially be getting with your code is filling the destination array with an incrementing byte, starting with the low byte of the offset of your source array.

How does that work?
An can I use it to store more than 2 other buffers into the destination buffers?
eg. can I use destination_buffer + sizeof the other buffer?


raymond

QuoteHow does that work?

movsb is one of those assembly instructions which uses ESI as a pointer to the source and EDI as a pointer to the destination as the default registers. Both registers get modified by 1 after each iteration, according to the direction flag. The rep prefix uses the ECX register as the default register for the count of repetitions.

QuoteAn can I use it to store more than 2 other buffers into the destination buffers?

You can store as many as you want as long as you don't exceed the capacity of that destination buffer.

Quotecan I use destination_buffer + sizeof the other buffer?

If you do one after the other, EDI would already be pointing to the first byte following the end of the previous transfer; if you want to do it later, store the value of EDI and recover it into EDI when needed. The only instructions required would then be to load ESI with the pointer to the source of the next transfer and load ECX with the size of that new transfer. Then, rep movsb does it again.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com