x86 assembly language question

Started by
21 comments, last by Washu 19 years, 7 months ago
Assembly language is a bit far in my head and I can't remember how to copy a 32 bit value multiple times in an array. I vaguely remember that there is a specific instruction for that where you have to put the count in cx (I think) the destination address in some other register and then call some instruction to do the work. Can someone please refresh my memory? Ex: const int nTimesToCopy = 512; const DWORD dwValue = 0xFF00FF00; __asm { // what goes here? }
Frederic FerlandStrategy First, Inc.http://www.strategyfirst.com
Advertisement
I believe rep stosd is what you're looking for. It'll take whatever value is in eax and put it at es:edi, add 4 to edi, and repeat that ecx times.
Ra
Well, first make sure that EDI points to the destination array, load ECX with the length of the array, then load EAX with the value to load into the array. then just REP STOSD

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

rep will work but there is acutally a string copy instruction...but i can't remember it either... sorry :(
Of course, the real question is: Why not just use memset?

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Something along the lines of:

const int nTimesToCopy = 512;
const DWORD dwValue = 0xFF00FF00;
unsigned char array[ nTimesToCopy * sizeof( dwValue )];

__asm
{
mov EDI, [array]
mov ECX, nTimesToCopy
mov EAX, dwValue
rep stosd
}

No guarantees that it will work as it's from memory and I think that you may need to set EDI to a DWORD PTR to array, but it gives you the general idea.
Thanks! I'll try that.

Washu, the reason I can't use memset is that even though it takes an int as a parameter, it only copies 1 byte repeatedly, not 4 bytes like what I need to do.
Frederic FerlandStrategy First, Inc.http://www.strategyfirst.com
Hello !

Not sure you'll do better than a C version of your copy algorithm. Compiler generally do very good stuff when dealing with loops.
If you still want to go the assembly way, you may want to check the memcpy() code. It is very optimized. Maybe you just need to modify it so it can write 32 bit ints instead of bytes.

HTH,
Quote:Original post by i1977
Washu, the reason I can't use memset is that even though it takes an int as a parameter, it only copies 1 byte repeatedly, not 4 bytes like what I need to do.


Are you sure? I was under the impression that it would copy the largest blocks possible until the remaining number of bytes is smaller than said block. Then it would copy any remaining bytes the slow way.
Hi smr,
memset() fills a block of memry with a single byte. Wether it copies 4 bytes in a row or not is simply a matter of optimisations. Since the OP wants to init a block of memory using 4 different bytes. This is completely different, and memset do not allow to do that.

Truly,

This topic is closed to new replies.

Advertisement