Getting -nan when doing computations with the FPU Nasm x86 Assembly

1.1k views Asked by At

First of all I would like to thank anyone who replies to this, your help is greatly appreciated!

I am trying to compute the standard deviation of a set of floating point numbers passed to my program as command line parameters. First I store each of the parameter strings in a temp variable, then I parse them to floating point numbers using sscanf(). My first problem is that the first (and when I say first, I am talking about the one after the name of my program...so actually the second) command line parameter doesn't get parsed. In the loop that I use to compute the average of the numbers I try to print out each parameter right after I parse it to a floating point number. It always prints all of them except the first one. The first one always gets printed as -nan. Also, the variable std_dev gets printed as -nan as well, even though I never modify it. This puzzles me. What am I doing wrong?

Here is the code:

EXTERN printf
EXTERN sscanf 
GLOBAL main

SEGMENT .data
form0:      DB "The standard deviation is: %d", 10, 0
form1:      DB "The standard deviation is: %f", 10, 0 
fpform:     DB "%f", 0  
avg:        DD 0.0 
std_dev:    DD 0.0   
temp:       DD 0.0 

debugform:  DB "temp=%f, std_dev=%f, avg=%f", 10, 0 

SEGMENT .text 
main:
        push    ebp                         ; compose stack frame 
        mov     ebp, esp
        push    ebx
        push    ecx

        mov     ebx, [ebp + 8]              ; ebx = # of params
        mov     ecx, [ebp + 12]             ; ecx = &param_table 
        cmp     ebx, dword 1                ; no params passed? 
        je      .end_0                      ; YES - print 0 
                                            ; NO - compute and print std_dev 
        finit                               ; initialize fpu 
        fldz                                ; initialize fpu registers with 0's 
        fldz
        fldz
        fldz
        fldz
        fldz
        fldz
        fldz

        call    findStdDev                  ; find the standard deviation of the numbers passed as command line params
        call    debugPrint
        pushad                              ; preserve all registers before making system call
        fld     dword [avg]                 ; load standard deviation into fpu
        sub     esp, 8                      ; reserve space for a qword in the stack
        fstp    qword [esp]                 ; load the 64-bit representation of std_dev into stack
        push    form1                       ; pass format string
        call    printf                      ; print out the standard deviation
        add     esp, 12 
        popad 

        jmp     .end                        ; 
        pop     ebp 
        ret 
.end_0:                                     ; if no parameters are passed, print std_dev = 0
        push    dword 0 
        push    form0
        call    printf
        add     esp, 8
.end: 
        pop     ecx
        pop     ebx
        pop     ebp                         ; restore used register and return from program 
        ret 

;-----------------------------------------------------------------------------------
findStdDev:                                 ; assumes ebx = # params, ecx = &param_table 
        push    edi                         ; save used regs 
        push    esi                         
        xor     esi, esi                    ; clear esi as a precaution 
        mov     esi, dword 1                ; loop counter for loop to find average of numbers  
.find_avg:  
        cmp     esi, ebx                    ; esi == number of params?
        je      ._break                     ; YES - break 
        pushad                              ; preserve registers before calling a C function
        push    temp                        ; temp storage for string representation of input nums
        push    fpform                      ; format string 
        push    dword [ecx + esi*4]         ; current command line param
        call    sscanf                      ; parse string to f.p. 
        add     esp, 12 
        popad 

        call    debugPrint                  ; should leave the CPU and FPU registers how they were before the call

        fld     dword [temp]                ; st0 = temp ; st1 = sum 
        faddp   st1, st0                    ; st0 = sum + temp
        ;call   debugPrint                  ; should leave the CPU and FPU registers how they were before the call
        inc     esi                         ; move to next command line param
        jmp     .find_avg

._break:
        mov     [temp], ebx                 ; temp = num of params
        sub     [temp], dword 1
        fld     dword [temp]                ; st0 = #params; st1 = sum of params
        fdivp   st1, st0                    ; st0 = (sum of params)/(#params)
        fstp    dword [avg]                 ; set avg; clear st0 

        pop     esi 
        pop     edi 
        ret 

;----------------------------------------------------------------------------------------       
debugPrint:

        pushfd 
        pushad
        sub     esp, 24
        fld     dword [avg]
        fld     dword [std_dev]
        fld     dword [temp]
        fstp    qword [esp]
        fstp    qword [esp+8]
        fstp    qword [esp+16]
        push    debugform
        call    printf
        add     esp, 28
        popad
        popfd
        ret 

Thanks again!

[Edit: I haven't finished actually computing the standard deviation. What I have there is just the loop that computes the average.]

0

There are 0 answers