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
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
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
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
You could also use RECORD (http://msdn2.microsoft.com/en-us/library/axf2b4ay(VS.80).aspx) and MASK (http://msdn2.microsoft.com/en-us/library/c3c74w11(vs.80).aspx), but the method Raymond describes is much more straightforward.
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 (http://msdn2.microsoft.com/en-us/library/axf2b4ay(VS.80).aspx) and MASK (http://msdn2.microsoft.com/en-us/library/c3c74w11(vs.80).aspx), 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!
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