Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Promit

Quick x86 asm question (RGB/BGR byte swap)

This topic is 5322 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

I'm trying to convert this code:
	for( i = 0; i < m_Size * 3; i+=3 )
	{
		//switch BGR to RGB

		temp = RawData[i];
		RawData[i] = RawData[i + 2];
		RawData[i + 2] = temp;
	}
  
Into this:
	temp = m_Size;
	__asm
	{
		mov ecx, temp
		mov ebx, RawData
		Switch:
			mov al, [ebx]
			mov ah, [ebx+2]
			mov [ebx+2], al
			mov [ebx], ah

			add ebx, 3
			dec ecx
			jnz Switch
	}
 
And VC 6 is giving me a very strage error off the mov ebx, RawData line: error C2443: operand size conflict Commenting the line just before that fixes it, so I'm guessing this is something stupid I'm not catching off with; I'm not so hot with assembly. Could someone please point out what I'm doing wrong? [EDIT] Changing temp in the first assembly line to a numeric literal also fixes the error. I don't get it >_< [edit 2} source tags and edit are buggy. [edited by - Promit on February 29, 2004 4:04:07 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
you have to tell it what size data you want to move i.e 16 bit in this case

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hmmm - 24 bit data ... couple of things to watch out for - I assume you''re doing this for a speed up - if thats the case you''re going to have sever caching issues (accesssing unaligned data usually incurs a penalty) - you might consider 32 bit RGB(+A/unused) to get around that - if that''s the case you could load into a 32bit register, use the bswap instruction & then shift to loose the unused 8 bits, something like

temp = m_Size;
__asm {
mov ecx, temp
mov ebx, RawData
Switch:
mov eax, [ebx]
bswap eax
shr eax, 8
mov [ebx], eax
add ebx, 4
dec ecx
jnz Switch
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oh - same AP as last time - something else to watch for - VC6''s error messages for assembly usually identify the wrong line for errors/warnings in __asm blocks (I think it''s the case that they tend to be a line ahead of the error)

Share this post


Link to post
Share on other sites
See, it''s a bitmap file I''m loading, so I don''t get to choose how it''s aligned; I have to deal with it as I get it. I can''t add an A component just for the hell of it.

This is what Visual Studio does with the loop (all opitmizations)

; 72 : //for each pixel in the texture

; 73 : for( unsigned int i = 0; i < m_Size * 3; i+=3 )

mov eax, DWORD PTR [esi+12]
add esp, 4
lea ecx, DWORD PTR [eax+eax*2]
test ecx, ecx
jbe SHORT $L33394
push ebx
push edi
or ebx, -1
xor edi, edi
lea ecx, DWORD PTR [ebp+1]
sub ebx, ebp
$L33392:

; 74 : {
; 75 : m_TexData[i*4/3] = RawData[i];

mov eax, -1431655765 ; aaaaaaabH
mov DWORD PTR 8+[esp+12], ebx
mul edi
mov eax, DWORD PTR [esi+24]
mov bl, BYTE PTR [ecx-1]
shr edx, 1
mov BYTE PTR [edx+eax], bl

; 76 : m_TexData[i*4/3+1] = RawData[i+1];

mov eax, DWORD PTR [esi+24]
mov bl, BYTE PTR [ecx]
mov BYTE PTR [edx+eax+1], bl

; 77 : m_TexData[i*4/3+2] = RawData[i+2];

mov eax, DWORD PTR [esi+24]
mov bl, BYTE PTR [ecx+1]
mov BYTE PTR [edx+eax+2], bl


I don''t know if that''s better or not, but it looks a whole hell of a lot longer...

Share this post


Link to post
Share on other sites

  • 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!