News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Flag setting method

Started by Neil, November 06, 2010, 11:55:30 AM

Previous topic - Next topic

jj2007

Quote from: Neil on November 06, 2010, 04:20:06 PM
That's interesting jj, but unfortunately there are 100+ flags :(

Would still work if you access them with immediates:
include \masm32\include\masm32rt.inc

Flags MACRO pos1
LOCAL posMem, posBit
  posMem = pos1/32
  posBit = pos1/32-posMem
  bt MyFlags[posMem*4], posBit
  EXITM <CarrY?>
ENDM

SetFlags MACRO pos1, mode
LOCAL posMem, posBit
  posMem = pos1/32
  posBit = pos1/32-posMem
  if mode
bts MyFlags[posMem*4], posBit
  else
btr MyFlags[posMem*4], posBit
  endif
ENDM

.data?
MyFlags dd 4 dup(?) ; 128 flags

.code
start:
SetFlags 0, 1
SetFlags 99, 1
.if Flags(99)
MsgBox 0, "Flag 99 set", "Hi", MB_OK
.else
MsgBox 0, "Flag 99 clear", "Hi", MB_OK
.endif
SetFlags 99, 0
.if Flags(99)
MsgBox 0, "Flag 99 set", "Hi", MB_OK
.else
MsgBox 0, "Flag 99 clear", "Hi", MB_OK
.endif
exit
end start

Neil

To get that to work I would have to change my flag definition from bytes to dwords, but still worth thinking about.

dedndave

Neil,
you say there are ~100 flags
is it safe to assume there are more than one set of flags ?
i say that because you have ESI holding the base address
how many sets of flags are there ?

jj2007

Quote from: Neil on November 07, 2010, 11:28:27 AM
To get that to work I would have to change my flag definition from bytes to dwords, but still worth thinking about.

It actually works with bits, and testing is done with .if Carry? etc, or if you prefer, with the jc/jnc instructions.

Neil

Dave there is only one set of flags. esi is only loaded up when required.

Neil

jj I thought that BTR & BTS only worked with words & dwords, or am I getting a bit confused somewhere :dazzled:

jj2007

Quote from: Neil on November 07, 2010, 12:48:44 PM
jj I thought that BTR & BTS only worked with words & dwords, or am I getting a bit confused somewhere

It is confusing indeed. You test for a bit in a dword registe, but it can also be memory - and then a bt MyMem, eax has apparently no limits, i.e. you can define the bit offset in eax freely. If I find time, I will design The Perfect Macro :wink

Neil

I look forward to that  :bg

dedndave

if there is only one set of flags, then ESI need not contain the base address - it may be specified as a direct address

instead of...
       mov byte ptr [esi+n],0
you can use
       mov byte ptr FlagTable[n],0
that simplifies the addressing mode

furthermore, if there are only ~100 flags, use 4-aligned dwords for each one
who cares about a few hundred extra bytes ?
if you want fast....
       mov dword ptr FlagTable[4*n],0
(note - this assembles as a single address value)
even better if the 0/1 are in register   :bg

if the flag data needs to be saved to a file at some point, reduce it with an algo and expand it on read

Neil

I like that one dave  :8) I would never of thought of that method & you're right a few hundred bytes is neither here or there.

dedndave

i think it's faster
at least it was on an 8086, if i remember correctly - lol