News:

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

Storing/referencing a single bit?

Started by brixton, June 22, 2007, 07:59:45 PM

Previous topic - Next topic

brixton

Hey all,

If I have a byte or a DWORD, how can I reference a single bit within that data?  A similar idea is if I have many bytes, I can use BYTE PTR.  Is there a similar method for reading/storing a single bit?

Regards,

brixton
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

Darrel

Example test bit 3

        and     eax,08h
        jz
        jnz


Pointer to bit 3

        BitPtr  DWORD   3
        mov     edx,1
        mov     ecx,BitPtr
        shl     edx,cl
        and     edx,DWORDofInterest
        jz
        jnz


HTH,

Darrel

drizz

some more examples
MYDWORD equ 80000000H
BITINDEX equ 31; zero based

MOV EAX,MYDWORD
BT EAX,BITINDEX ; test index(zero based) bit in eax
SETC DL ; bit will be copied to carry flag

MOV EAX,MYDWORD
MOV ECX,BITINDEX
MOV EDX,1
SHL EDX,CL
TEST EAX,EDX
SETNZ DL ; zero flag will be empty

;IF BITINDEX IS CONSTANT
MOV EAX,MYDWORD
TEST EAX,(1 SHL BITINDEX)
SETNZ DL ; zero flag will be empty

;; HLL EQUIVALENT
.if EAX & (1 SHL BITINDEX)
; Bit is set
NOP
.endif
The truth cannot be learned ... it can only be recognized.

raymond

Or you could use "masks", either individual ones or within an array.

.data
   bit2mask   db   00000100b  ;using the numbering of bits from 0 to 7 for bytes
.code
   test dl,bit2mask
   jz   notset


.data
   maskarray  db  00000001b,00000100b,00010000b,00100000b,10000000b
.code
   test dl,maskarray[ecx]   ;ECX would need to be 0-4 to access the example array
   jz   notset

;or
   test ah,maskarray[3]
   jnz  bit5_is_set


You could also use such masks to set or reset the appropriate bit(s) without affecting any of the other bits.

xor  dl,bit2mask   ;sets the bit if it's OFF, clears it if it's ON

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

GregL

You could also use RECORD and MASK, but the method Raymond describes is much more straightforward.


brixton

Many thanks for the help!  The masks seem very intuitive and straightforward.

Also,

Quote from: Greg on June 23, 2007, 01:11:13 AM
You could also use RECORD and MASK, but the method Raymond describes is much more straightforward.

Regarding the RECORD type;

QuoteDeclares a record type consisting of the specified fields. fieldname names the field, width specifies the number of bits, and expression gives its initial value.

So this means I can declare a variable consisting of a single bit?  Sounds interesting and may try it out!
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

raleeper

You may find some variant of the following macros useful [but warning - you probably know more than I - these have not been tested in 32 bit environment]:

QF   MACRO   f,n      ;Query (test) Flag
   test   [flg&f], 1 SHL n
   ENDM

Example:

QF v,7   ;Copies bit 7 of some previously declared and initialized flgv to the Zero Flag.
jnz wherever

For other bit operations, try instead of test   [flg&f], 1 SHL n

clear
   and   [flg&f], NOT 1 SHL n
set
   or   [flg&f], 1 SHL n
change
   xor   [flg&f], 1 SHL n

Best wishes,

RAL