News:

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

Response Matrix?

Started by Mark Jones, July 14, 2005, 04:36:49 AM

Previous topic - Next topic

Mark Jones

Hi, quick generic question. If we had a byte which represents seven mutually-inclusive conditions, what would be the best way to write a decoder for it? Okay that needs more explanation. Say seven bits represent the presence of the optical colors ROYGBIV. If RED and BLUE are true, then I'd like to print the string PURPLE. But if RED and YELLOW are true, we want to print ORANGE. If RED and ORANGE are true, we want to print RED-ORANGE. If only RED is true, then print RED. How do we code that type of algorithm in MASM? Thanks for any insight. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Phil

I'm often known for my in-efficiency, especially in my initial response to a problem-solution, but I would just build a table of 128 string pointers to your desired results since you are only talking about 7-bits. It would be a lot of typing but you could simplify the process with macros.

color0 db "Black",0
color1 db "Red",0
color2 db "Orange",0
color3 db "Red-Orange",0
color4 db "Yellow",0
color5 db "Orange",0
color6 db "Yellow-Orange",0
color7 db "Red-Orange-Yellow",0     ; or whatever that color would be
....
color64 db "Violet",0
....
color127 db "Rainbow",0             ; or whatever all colors would be

colortab dd color0, color1, color2, color3, .... color127

colorlookup proc                    ; called with 7-bit color in eax
     and eax,127                    ; make sure it's only 7 bits
     mov eax,colortab[4*eax]        ; return the color string pointer
     ret
colorlookup endp


Without doing something like this you would have a ton of combinatorial code. There are probably many other better ways but something like this would be my first approach.


Jeff

if it were me, i would approach it using bit testing.

.DATA
szRed BYTE "Red",0
szOrange BYTE "Orange",0
szYellow BYTE "Yellow",0
szGreen BYTE "Green",0
szBlue BYTE "Blue",0
szIndigo BYTE "Indigo",0
szViolet BYTE "Violet",0
rgColors DWORD szRed,szOrange,szYellow,szGreen,szBlue,szIndigo,szViolet
szBlack BYTE "Black",0
szDash BYTE '-',0

Colors PROC STDCALL, nColor:DWORD
LOCAL multi:DWORD
    PUSH ecx
    MOV multi,0         ;keeps track of the colors found (for formatting)
    XOR ecx,ecx         ;start at the 0bit
    Color:
        BT nColor,ecx   ;check bit
        JNC NoColor
        CMP multi,0
        JE @F
        INVOKE WriteString, ADDR szDash
@@:     INC multi
        INVOKE WriteString, rgColors[4*ecx]
    NoColor:
    INC ecx
    CMP ecx,6
    JBE Color
    CMP multi,0
    JA @F
    INVOKE WriteString, ADDR szBlack
@@: PUSH ecx
    RET
Colors ENDP


that is... if you wanted individual colors.  mixed cases call for a the table that phil posted but is fairly wasteful if you asked me.

ps. "all colors" is white  ;)

roticv

Just make sure that WriteString does not modify ecx.

PBrennick

My vote is for Phil's approach, a look up table is a cool solution the bits that are set represent a value from 0 to 127 and automatically become the index (offset) into the table.  what could be simpler?

Paul
The GeneSys Project is available from:
The Repository or My crappy website