首先,我要感谢任何回复此帖的人,非常感谢您的帮助!
我正在尝试计算作为命令行参数传递给我的程序的一组浮点数的标准偏差。首先,我将每个参数字符串存储在temp
变量中,然后使用sscanf()
将它们解析为浮点数。我的第一个问题是第一个问题(当我说第一个时,我说的是我的程序名称之后的那个...实际上是第二个)命令行参数没有得到解析。在我用来计算数字平均值的循环中,我尝试在将其解析为浮点数后立即打印出每个参数。除了第一个之外,它总是打印所有这些。第一个始终打印为-nan
。此外,变量std_dev
也会打印为-nan
,即使我从不修改它。这让我很困惑。我究竟做错了什么?
以下是代码:
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 = ¶m_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 = ¶m_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
再次感谢!
[编辑:我还没有完成实际计算标准偏差。我所拥有的只是计算平均值的循环。]