Archived

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

ironfroggy

VC++ assembly error

Recommended Posts

i cant seem to get this code to compile, but it compiles on my friends machine just fine. I can''t figure out whats wrong. here''s the function:
/* sets the value of a single bit */
void setbit(byte* p, u32 s, bool b) {
	u32 t;
	/* TODO: Share with bitset::set() */
	/* move pointer to the correct byte where bit resides */
	p += s/8;
	/* write bit */
	/* WIN32-only assembly */
	#ifdef WIN32
	t = *p;
	/* ASSEMBLY BLOCK */
	__asm
	{
					mov	EAX, t			; move data value to data register
					mov	EBX, s			; move offset to data register
					mov ECX, b			; move the bit value
					cmp	ECX, 1			; determine what value to set (ERROR)
					je	SET_TRUE		; could probably skip this instruction
					jne	SET_NTRUE
		SET_TRUE:	bts	EAX, EBX		; set bit to 1
					mov t, EAX			; move value back to variable
					jmp	END
		SET_NTRUE:	btr	EAX, EBX		; set bit to 0
					mov t, EAX			; move value back to variable
					jmp	END
		END:
	}
	#endif
	/* in the case of writing a 1 */
	if (b) {
		/* if bit is zero, simply add the bitmask from get() */
		if (getbit(p,s) == 0) {
			*p += 2>>(s%8);
			return;
		}
		/* otherwise you do nothing */
		return;
	}
	/* in the case of writing a 0 */
	/* if the bit is 1 */
	if (getbit(p,s))
		/* subtract the mask */
		*p -= 2>>(s%8);
	/* done */
	return;
}
 
and here''s the error: error C2443: operand size conflict (http://www.ironfroggy.com/) (http://www.ironfroggy.com/pinch)

Share this post


Link to post
Share on other sites
mov ECX, b
You are trying to move an 8-bit value into a 32 bit register. Not good.

Side note: When using inline assembly, errors are often reported to be on the wrong line.

Share this post


Link to post
Share on other sites
if you want to do this, use....

movzx = "move and zero extend".
movsx = "move and sign extend".

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
Dude, you don't need assembly to do that.

typedef DWORD* bitarry;
inline void setbit(bitarray p, DWORD bitIndex)
{
DWORD bit(1);
bit <<= bitIndex&5;
p[bitIndex>>5] |= bit;
}

inline void clrbit(bitarray p, DWORD bitIndex)
{
DWORD bit(1);
bit <<= bitIndex&5;
bit = ~bit;
&p[bitIndex>>5] &= bit;
}

Replace the 5's with 3's if you use a BYTE* for the bitarray (DWORD* is more efficent).

Edited by - Magmai Kai Holmlor on January 30, 2002 8:52:08 PM

Share this post


Link to post
Share on other sites