0

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

Drifter64
  • 1,103
  • 3
  • 11
  • 30
  • Could you give an example of the numbers you entered and the results you got? – Michael Nov 22 '13 at 09:32
  • This information was added at the bottom of my question. As you can see, neither the atof, nor the sum appear to be working properly. – Drifter64 Nov 22 '13 at 15:50
  • This is not likely to give you correct results: `FSTP qword [tempFlt1] / FLD dword [tempFlt1]`. Use the same size (`dword` or `qword`) for both the store and the load. – Michael Nov 22 '13 at 16:28
  • I made ALL references dword. The debug printing isnt seeming to work, but the sum is now correct...... – Drifter64 Nov 22 '13 at 19:18

0 Answers0