title: How do I convert a string representing a signed hex int into its signed int Doubleword number in 80x86?
body: <p>So I am taking an assembly language course and I am stuck on this problem from the book:
  Using the windows 32 console (so I have an <code>io.h</code> to use), I am supposed to take a valid hex value inputted by the user and then display the actual hex value in the register <code>EAX</code>.  So if the user entered "AB CD E2 18", then after the procedure <code>EAX</code> would hold the value: <code>ABCDE218</code>.
  The parts that I am stuck on are the <code>A</code>-<code>F</code> values.  If I use <code>A</code> for example, I can get the bits to read <code>00000010</code>, but I don't know how to change that into its hex value <code>A</code>.  Here is what I have so far:</p>
  
  <pre><code>.586
  .MODEL FLAT
  .CODE
  
  hexToInt    PROC
          push   ebp                 ; save base pointer
          mov    ebp, esp            ; establish stack frame
          sub    esp, 4              ; local space for sign
          push   ebx                 ; Save registers
          push   edx
          push   esi
          pushfd                     ; save flags
  
          mov    esi,[ebp+8]         ; get parameter (source addr)
  
  WhileBlankD:
          cmp    BYTE PTR [esi],' '  ; space?
          jne    EndWhileBlankD      ; exit if not
          inc    esi                 ; increment character pointer
          jmp    WhileBlankD         ; and try again
  EndWhileBlankD:
  
          mov    eax,1               ; default sign multiplier
  IfPlusD:cmp    BYTE PTR [esi],'+'  ; leading + ?
          je     SkipSignD           ; if so, skip over
  IfMinusD:
          cmp    BYTE PTR [esi],'-'  ; leading - ?
          jne    EndIfSignD          ; if not, save default +
          mov    eax,-1              ; -1 for minus sign
  SkipSignD:
          inc    esi                 ; move past sign
  EndIfSignD:
  
          mov    [ebp-4],eax         ; save sign multiplier
          mov    eax,0               ; number being accumulated
  
  WhileDigitD:
          cmp    BYTE PTR [esi],'0'  ; compare next character to '0'
          jb     EndWhileDigitD      ; not a digit if smaller than '0'
          cmp    BYTE PTR [esi],'9'  ; compare to '9'
          ja     TestForHexD      
          mov    bl,[esi]            ; ASCII character to BL
          and    ebx,0000000Fh       ; convert to single-digit integer
          and    eax, ebx
          shl    eax, 4
          inc    esi
          jmp    WhileDigitD
  TestForHexD:
          cmp    BYTE PTR [esi], 'F'
          ja     EndWhileDigitD
          mov    bl, [esi]
          sub    bl, 31h
          and    ebx, 000000FFh
          or     al, bl
          shl    eax, 4
          inc    esi                 ; increment character pointer
          jmp    WhileDigitD         ; go try next character
  EndWhileDigitD:
  
  ; if value is &lt; 80000000h, multiply by sign
          cmp    eax,80000000h       ; 80000000h?
          jnb    endIfMaxD           ; skip if not
          imul   DWORD PTR [ebp-4]   ; make signed number
  endIfMaxD:
  
          popfd                      ; restore flags
          pop    esi                 ; restore registers
          pop    edx
          pop    ebx
          mov    esp, ebp            ; delete local variable space
          pop    ebp 
          ret                        ; exit
  hexToInt    ENDP
  
          END
  </code></pre>
  
  <p>The <code>TestForHex</code> label is where I am trying to convert the ASCII string to hex. I was looking around and read that I could accomplish my goal by shifting and masking, but I can't figure it out and I can't find any examples.  At this point I am sure its something really small that I am just over looking, but I am stuck.</p>