Jump to content
  • Advertisement
Sign in to follow this  
i1977

x86 assembly language question

This topic is 5101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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? }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!