I am trying to implement a simple program in NASM that takes in an unknown number of float values from the command line arguments, and adds them together. This seems not to be working for me, and i think it might be because i am using atof calls incorrectly. I output the floats after the atof call to see that they have worked, and i dont get the numbers i put in. Furthermore, i dont even get the sum of these printed numbers, so something is wrong there as well. I have looked for samples of similar code, but sadly NASM isnt documented nearly as well online as, say, Java.
Here is my code:
extern atof
extern printf
extern exit
global main
section .data
formats: db "%s", 10, 0
formatd: db "%d", 10, 0
formatf: db "%f", 10, 0
debug1: db "fell through copy loop.", 10, 0
debug2: db "fell through location.", 10, 0
section .bss
floatAvg: resq 1
numArgs: resd 1
tempFlt1: resq 1
tempFlt2: resq 1
section .text
main:
mov ecx, [esp + 4]
dec ecx
mov dword [numArgs], ecx
mov ebx, [esp + 8]
add ebx, 4
;no output if no args
cmp ecx, 0
je endProg
;find the sum
dec ecx
FINIT
FLDZ ; sum is zero
sumLoop:
push ecx ;preserve
push dword [ebx]
call atof
add esp, 4
FSTP qword [tempFlt1]
;debug
FLD dword [tempFlt1]
sub esp, 8
FSTP qword [esp]
push formatf
call printf
add esp, 12
FADD qword [tempFlt1]
pop ecx ;restore
dec ecx
add ebx, 4
cmp ecx, 0
jge sumLoop
FSTP qword [tempFlt2]
FLD dword [tempFlt2]
sub esp, 8
FSTP qword [esp]
push formatf
call printf
add esp, 12
endProg:
call exit
Example Input/Output:
In: 4 7 8 9
Out: 0.0 0.0 0.0 0.0, sum 0.0
In: 7.3 6.9
Out: 0.0 -0.0, sum 272008302207532160516096.0
In: 8.8 6.3 3.98
Out: -0.0 0.0 0.058750, sum -230215375187831947264.0