Assembly code snippets

Back to the snippets overview

Details

TitleOneZeroMany flags case
AuthorThe Svin
Submitted by:The Svin
Date added:2002-03-30 19:16:14
Date modified:2002-03-30 19:16:14

Comments

the code is written for flags (bits set) analysing routin.
It returns index of case into ebx, so that your can jump to right code place using the index or
load right value without branching.
0 - not flags set
1 - single flag(only one bit set)
2 - many flags(many flags set)

Snippet

 xor ebx,ebx ;1
 mov edx,eax ;0
 cmp ebx,eax ;1
 lea ecx,[eax][-1] ;0
 adc ebx,0  ;1
 xor edx,ecx ;0
 and edx,eax ;1
 cmp edx,eax ;1
 adc ebx,0  ;1
;usage I call the code above "casecode" in the following examples:
;loading right data addr
.data
TxtNoFlags db 'Virtual Style',0
TxtSingleFlag db 'Single Flag Style',0
TxtComplex db 'Complex Style',0
ALIGN 4
TblStylesType dd offset TxtNoFlags,offset TxtSingleFlag,offset TxtComplex
.code
 ....
 casecode
 dword ptr [ebx*4][offset TblStylesType] = address of case string.
;jumping to case handler:
.data
TblFlagsHndl CaseZero,CaseSingle,CaseComlex
.code
....
    casecode
    lea ebx,[ebx*4][TblStylesType]
    jmp ebx
...
CaseZero:
...
CaseSingle:
...
CaseComlex:
...