The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ragdog on May 24, 2010, 07:10:31 PM

Title: Char table
Post by: ragdog on May 24, 2010, 07:10:31 PM
Hi

Gives a solution for check if valid char in a string
If not give me a result for invalid string

WIth a Editbox it this simply with
   .if uMsg==WM_CHAR
      mov eax,wParam
      .if (al>="0" && al<="9") || (al>="A" && al<="F") || (al>="a" && al<="f") || al=="-" || al==VK_BACK
         .if al>="a" && al<="f"
            sub al,20h
         .endif
         invoke CallWindowProc,EditOldProc,hWnd,uMsg,eax,lParam
         ret
      .endif

My why is now with a character table
szValidChars db  "ABCDEF1234567890-",0

Have your an idea?
Title: Re: Char table
Post by: redskull on May 24, 2010, 07:32:28 PM
REPNE SCASB will scan a string (your "table") for a byte (the input char)
Title: Re: Char table
Post by: clive on May 24, 2010, 07:48:08 PM
What are you trying to achieve? Faster processing? If you want raw speed you could use a 256 entry lookup/translate table

Also doing the tests in this order would be more efficient (ie faster)
      mov eax,wParam
       .if al>="a" && al<="f"
          sub al,20h
       .endif
      .if (al>="0" && al<="9") || (al>="A" && al<="F") || al=="-" || al==VK_BACK
         invoke CallWindowProc,EditOldProc,hWnd,uMsg,eax,lParam
         ret
      .endif
Title: Re: Char table
Post by: ragdog on May 24, 2010, 08:17:37 PM
no i coding a tool without a resource (dialog)

Title: Re: Char table
Post by: joemc on May 25, 2010, 02:20:20 AM
less double testing
     
       mov eax,wParam
       .if al>="a" && al<="f"
          sub al,20h
          invoke CallWindowProc,EditOldProc,hWnd,uMsg,eax,lParam
          ret
      .elseif (al>="0" && al<="9") || (al>="A" && al<="F") || al=="-" || al==VK_BACK
         invoke CallWindowProc,EditOldProc,hWnd,uMsg,eax,lParam
      .endif
      ret


or how i would write. May be awful, sorry if it is.  You'll be waiting on that CallWindowProc anyways :)


  movzx eax, wParam
  cmp al, 008h  ; VK_BACK
  je OldProc
  cmp al, 02Dh  ; "-"
  je OldProc
  cmp al, 030h  ; "0"
  jb NoProc
  cmp al, 03Ah  ; "9"+1
  jb OldProc
  cmp al, 041h  ; "A"
  jb NoProc
  cmp al, 047h ; "F"+1
  jb OldProc
  cmp al, 061h ; "a"
  jb NoProc
  cmp al, 066h ; "f"
  ja NoProc
  sub al,20h
OldProc:     invoke CallWindowProc,EditOldProc,hWnd,uMsg,eax,lParam
NoProc:     ;xor eax,eax   ; probably want to return zero. not sure what you want 
                ret
Title: Re: Char table
Post by: dedndave on May 25, 2010, 01:48:37 PM
maybe i'm missing something   :P
but, shouldn't the RET be outside the IF structure ?
      mov eax,wParam
       .if al>="a" && al<="f"
          sub al,20h
       .endif
      .if (al>="0" && al<="9") || (al>="A" && al<="F") || al=="-" || al==VK_BACK
         invoke CallWindowProc,EditOldProc,hWnd,uMsg,eax,lParam
      .endif
      ret
Title: Re: Char table
Post by: clive on May 25, 2010, 02:01:58 PM
@Joe - Yes, I'd probably write it in real assembler too.

@Dave - Hard to say, ragdog only provided a code fragment, so just tried to match the fall-through behaviour.
Title: Re: Char table
Post by: ragdog on May 29, 2010, 08:36:25 AM
Hi

Ok i have solved it


.data
szHexAllowed db '0123456789ABCDEFabcdef ',0

.code
mov eax, wParam
mov   edi,offset szHexAllowed
mov   ecx,sizeof szHexAllowed
repnz   scasb
.if     zero?
.if al > '9'
and al,not 20h
.endif

invoke CallWindowProc,hOldProc,hWnd,uMsg,eax,lParam
ret
.endif


I hope your undstand now what i mean

Greets
Title: Re: Char table
Post by: dedndave on May 29, 2010, 09:58:52 AM
i liked Clive's idea of a 256-byte LU table   :U
Title: Re: Char table
Post by: ragdog on May 29, 2010, 11:06:03 AM
Why?
Title: Re: Char table
Post by: dedndave on May 29, 2010, 02:48:01 PM
well - although it requires a 256-byte table, it would be easy to generate - the table doesn't have to be pre-defined
once you have the table made, this is probably the fastest conversion method - a very simple loop

the table needs to be generated once at program initialization
the code can probably be made even smaller   :P
but, this will give you an idea...
        .DATA?

HexTable db 256 dup(?)

        .CODE

;--------------------------------------------------------------------------------

TableInit proc

        xor     eax,eax
        mov     edx,offset HexTable
        mov     ecx,64
        push    edx

Table1: mov     [edx],eax
        add     edx,4
        dec     ecx
        jnz     Table1

        pop     edx
        mov     al,8
        mov     [edx+eax],al
        mov     al,2Dh
        mov     [edx+eax],al
        mov     cl,10
        mov     al,30h

Table2: mov     [edx+eax],al
        inc     eax
        dec     ecx
        jnz     Table2

        mov     al,41h
        mov     cl,6

Table3: mov     [edx+eax]al
        mov     [edx+eax+20h],al
        inc     eax
        dec     ecx
        jnz     Table3

        ret

TableInit endp

;--------------------------------------------------------------------------------

then, to use it...
        mov     eax, wParam
        mov     al,HexTable[eax]
        or      eax,eax
        jz      No_Proc

        invoke  CallWindowProc,hOldProc,hWnd,uMsg,eax,lParam
        ret

No_Proc: