在NASM中使用atof

时间:2013-11-22 05:48:21

标签: floating-point command-line-arguments nasm fpu atof

我试图在NASM中实现一个简单的程序,它从命令行参数中获取未知数量的浮点值,并将它们加在一起。这似乎对我不起作用,我认为这可能是因为我正在使用错误的atof调用。我在atof调用之后输出浮点数以查看它们已经工作,并且我没有得到我输入的数字。此外,我甚至得不到这些打印数字的总和,所以那里也有问题。我已经查找了类似代码的示例,但遗憾的是,NASM几乎没有在线记录,比如Java。

这是我的代码:

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

示例输入/输出:

In:4 7 8 9

输出:0.0 0.0 0.0 0.0,总和0.0

In:7.3 6.9

输出:0.0 -0.0,总和272008302207532160516096.0

In:8.8 6.3 3.98

输出:-0.0 0.0 0.058750,总和-230215375187831947264.0

0 个答案:

没有答案