clairec icon

Untitled

clairec | PRO | 05/10/17 04:45:32 AM UTC | 0 ⭐ | 416 👁️ | Never ⏰ | []
ASM (NASM) |

0 B

|

None

|

0 👍

/

0 👎

asciiToFloat:
        ;; Converts an ascii string to a floating point
        ;; If at any point it finds a syntax error it returns zero
        ;; and stops execution.
        ;; Result is returned in ST
        
        ;; Regex for matching numbers (For the sake of claire's brain)
        ;; [-+]?[0-9]*\.[0-9]+
        push ebp
        mov ebp, esp
        push esi
        
        fldz                    ; Initialize ST as 0
        fldz st(1)
        cld
        mov esi, [ebp+8]
        ;; We're using 0 in ah to "remember" a positive number, 1 for negative
        mov ah, 0               ; Assume Positive until proven otherwise
 
        ;; Test if the first byte was a sign or a digit or a decimal
        lodsb
testForPlus:    
        cmp al, 2bh             ; Test against +
        jne testForMinus        
        lodsb                   ; No need to mov ah, 0, it's already done
        jmp testForDigit
testForMinus:   
        cmp al, 2dh             ; Test against -
        jne testForDigit
        mov ah, 1
        lodsb
testForDigit:   
        mov ecx, 1
wholePartLoop:
        cmp al, 30h             ; compare against [0-9]
        jl testForPoint
        cmp al, 39h 
        jg errorExit            ; If this happens something is wrong
 
        sub al, 30h             ; Convert to decimal
        fimul ecx
        fiadd al
        mul ecx, 10
        lodsb
        jmp wholePartLoop
testForPoint:   
        cmp al, 2eh             ; Test against .
        jne errorExit           ; Again, failing this means something is wrong
        lodsb
        mov ecx, 1
        mov edx, 1
        fxch
fractionConvert:
        cmp al, 0
        je fractionize
        cmp al, 30h
        jl errorExit
        cmp al, 39h
        jg errorExit
 
        sub al, 30h
        fimul ecx
        mul ecx, 10
        lodsb
        jmp fractionConvert
fractionize:
        fidiv ecx
        fadd
        cmp ah, 1
        jne endAsciiToFloat
        fimul -1
endAsciiToFloat:
        pop ebp
        ret                     
errorExit:
        fldz
        jmp endAsciiToFloat

Comments