clairec

Untitled

May 9th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. asciiToFloat:
  2.         ;; Converts an ascii string to a floating point
  3.         ;; If at any point it finds a syntax error it returns zero
  4.         ;; and stops execution.
  5.         ;; Result is returned in ST
  6.        
  7.         ;; Regex for matching numbers (For the sake of claire's brain)
  8.         ;; [-+]?[0-9]*\.[0-9]+
  9.         push ebp
  10.         mov ebp, esp
  11.         push esi
  12.        
  13.         fldz                    ; Initialize ST as 0
  14.         fldz st(1)
  15.         cld
  16.         mov esi, [ebp+8]
  17.         ;; We're using 0 in ah to "remember" a positive number, 1 for negative
  18.         mov ah, 0               ; Assume Positive until proven otherwise
  19.  
  20.         ;; Test if the first byte was a sign or a digit or a decimal
  21.         lodsb
  22. testForPlus:    
  23.         cmp al, 2bh             ; Test against +
  24.         jne testForMinus        
  25.         lodsb                   ; No need to mov ah, 0, it's already done
  26.         jmp testForDigit
  27. testForMinus:  
  28.         cmp al, 2dh             ; Test against -
  29.         jne testForDigit
  30.         mov ah, 1
  31.         lodsb
  32. testForDigit:  
  33.         mov ecx, 1
  34. wholePartLoop:
  35.         cmp al, 30h             ; compare against [0-9]
  36.         jl testForPoint
  37.         cmp al, 39h
  38.         jg errorExit            ; If this happens something is wrong
  39.  
  40.         sub al, 30h             ; Convert to decimal
  41.         fimul ecx
  42.         fiadd al
  43.         mul ecx, 10
  44.         lodsb
  45.         jmp wholePartLoop
  46. testForPoint:  
  47.         cmp al, 2eh             ; Test against .
  48.         jne errorExit           ; Again, failing this means something is wrong
  49.         lodsb
  50.         mov ecx, 1
  51.         mov edx, 1
  52.         fxch
  53. fractionConvert:
  54.         cmp al, 0
  55.         je fractionize
  56.         cmp al, 30h
  57.         jl errorExit
  58.         cmp al, 39h
  59.         jg errorExit
  60.  
  61.         sub al, 30h
  62.         fimul ecx
  63.         mul ecx, 10
  64.         lodsb
  65.         jmp fractionConvert
  66. fractionize:
  67.         fidiv ecx
  68.         fadd
  69.         cmp ah, 1
  70.         jne endAsciiToFloat
  71.         fimul -1
  72. endAsciiToFloat:
  73.         pop ebp
  74.         ret                    
  75. errorExit:
  76.         fldz
  77.         jmp endAsciiToFloat
Add Comment
Please, Sign In to add comment