School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Assembly Source Code


You're Browsing As A Guest! Register Now...
Become an Expert!

Join 353,769 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,867 people online right now.Registration is fast and FREE... Join Now!




HexString to Long

This function (cdecl calling convention) will convert a hex string into a long value.

Submitted By: NickDMax
Actions:
Rating:
Views: 447

Language: Assembly

Last Modified: March 29, 2009
Instructions: This is a C style function so you must pass the parameters on the stack. Push the pointer onto the stack before you call. *** added error checking will now only read 8 chars and will stop on invalid chars.

Snippet


  1. ; EXAMPLE C program:
  2. ;
  3. ; #include <stdio.h>
  4. ; #define DEBUG_TESTCASE(testCase, var) var = hex2num( testCase); \
  5. ;    printf("Test: \"%15s\" = 0x%08X = %u\n",testCase, var, var);
  6. ;
  7. ;extern long hex2num(const char *);
  8. ;
  9. ; int main() {
  10. ;      unsigned long a;
  11. ;     DEBUG_TESTCASE("1234ABCD", a);
  12. ;     DEBUG_TESTCASE("5678efab", a);
  13. ;     DEBUG_TESTCASE("FfFfFfFf", a);
  14. ;     DEBUG_TESTCASE("3.141593", a);
  15. ;     DEBUG_TESTCASE("12345678ABCD", a);
  16. ;     DEBUG_TESTCASE("JAVABABE", a);
  17. ;     DEBUG_TESTCASE("CafeBabe", a);
  18. ;     DEBUG_TESTCASE("0000123@", a);
  19. ;     DEBUG_TESTCASE("0000123^", a);
  20. ;     DEBUG_TESTCASE("10000000", a);
  21. ;     DEBUG_TESTCASE(   "10000", a);
  22. ;     DEBUG_TESTCASE(     "100", a);
  23. ;     DEBUG_TESTCASE(       "1", a);
  24. ;     DEBUG_TESTCASE("a0000000", a);
  25. ;     DEBUG_TESTCASE(   "A0000", a);
  26. ;     DEBUG_TESTCASE(     "F00", a);
  27. ;     DEBUG_TESTCASE(       "f", a);
  28. ;     DEBUG_TESTCASE("100\x01", a);
  29. ;     return 0;
  30. ; }
  31.  
  32.  
  33. .386p
  34. PUBLIC _hex2num
  35.  
  36. MODEL flat, C
  37. _TEXT    segment dword public use32 'CODE'
  38.  
  39. _hex2num proc near                     ; extern "C" long hex2num(const char *ptr)
  40.     push ebp                           
  41.     mov ebp, esp                       ; set base pointer to stack pointer
  42.     push ebx
  43.         mov edx, dword ptr [ebp+8]     ; place the pointer into edx
  44.         xor ebx, ebx                   ; zero ebx
  45.         xor ecx, ecx                   ; zero our return value
  46.         and eax, 0FFh                  ; mask eax to just al.
  47. beginWhile:                            ; while (*ptr !=0)
  48.         cmp byte ptr [edx], 0          ; test to see if *ptr == 0
  49.         je short endWhile              ; if so, jump over loop.
  50.         cmp ebx, 08h                   ; a long is only 8 bytes long
  51.         je short endWhile              ; if we have read 8 chars end.
  52.         mov al, [edx]                  ; load a byte form the string
  53.         sub al, 30h                    ; al = al - '0' -- subtract 48
  54.  
  55.         cmp al, 09h                    ; if (al <= 9) jmp doCalc
  56.         jbe doCalc
  57.         cmp al, 11h                    ; Check chars between '0' and 'A'
  58.         jb endWhile
  59.        
  60.         sub al, 07h                    ; al = al - ('A' - '0') + 10 (sub al, 11h, add al, 0xA)
  61.         cmp al, 0Fh                    ; if (al <= 15) jmp doCalc
  62.         jbe doCalc
  63.         cmp al, 2Ah                    ; Check chars betweeen 'A' and 'a'
  64.         jb endWhile
  65.        
  66.         sub al, 20h                    ; al = al - ('a' - 'A')
  67.        
  68.         cmp al, 0Fh                    ; if (al > 15) then we have an illegal char
  69.         ja endWhile                    ;   so we exit the proc as is.
  70.        
  71. doCalc:       
  72.         add ecx, ecx                   ; ecx = 2 ecx
  73.         lea ecx, [8*ecx]               ; ebx = 8 * ecx = 16 * original_ecx neat eh! no need for mult
  74.         add ecx, eax                   ; ecx += al
  75.         inc edx                        ; inc ptr
  76.         inc ebx                        ; inc count
  77.         jmp short beginWhile
  78. endWhile:
  79.         mov eax, ecx                   ; return the value
  80.     pop ebx
  81.     pop ebp
  82.     ret
  83. _hex2num endp
  84.  
  85. _TEXT    ends
  86.  
  87. END

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month