I'll try to explain this as best I can....

Say you've got a number (single byte). You've also got several constants defined.

This is hard to explain....

For example, you grab the styles of a window as a dword. What's the slickest way to break that dword number back to the various equates. Like 012345678h would be broken down to "WS_BLAH or WS_WHATEVER or WS_ETC"? For my purposes, I've got a small number of equates, 4 at the moment, defined as 1-4. So what would the best way to break down 7 (for example) into 'NUMBER1 or NUMBER2 or NUMBER4' and display that info in a messagebox or whatever? A big .if/.else switch doesn't seem like the best solution, even though for my current purposes it wouldn't be too unmanageable.


hope that made sense :)

thanks,
will
Posted on 2003-07-24 18:28:28 by Will
Rule #2531, paragraph II, section b:

If not in code, in data.
So, basically, use a data table:

A_Table:

DD CTEXT("Value1")
DD CTEXT("Value2")
DD CTEXT("Value3")
DD CTEXT("Value4")

push DWORD PTR
pop StringAddress

In this table only one value may be in each DWORD, but I have a sneeky idea that you want something else - where each bit is a value that toggles? In that case use a value in the table for each bit and loop until the DWORD is zero, or all bits tested (non-destructive).

B_Table:


DD CTEXT("Value0")
...
...
DD CTEXT("Value31")

push 0 ; stopper on the stack
mov ecx, 31

_0: bt eax, ecx
jnc _1
push DWORD PTR [B_Table][ecx*4]
_1: dec ecx
jns _0

; now pop each string address off the stack until zero is reached...
Posted on 2003-07-24 18:43:05 by bitRAKE
Wow, that was fast!

That's pretty slick and exactly what I was looking for.


thanks a bunch,
will

edit: I learned a couple new opcodes too (bt, jnc, and jns), which is always nice. This also allows me to easily and quickly change the number of strings as well. Excellent!
Posted on 2003-07-24 18:50:04 by Will
I would prefer the "test" instruction instead of "bt", I think it reflects better the single bit character of the bit field:
	mov ecx, 80000000h

BitLoop: test eax, ecx
jnz bit_is_set

shr ecx, 1
jnz BitLoop
but you cannot use the mask as offset in the string table, so you would need an additional counter.
Posted on 2003-07-25 15:58:30 by beaster