assembly - Display floating point by printf -
i have problem display floating point value using printf.
i'm trying display result of math function, 0.00.
could me , tell i'm doing wrong?
my gnu code:
.text text: .ascii "function result: %4.2f \n" .data x: .float 2.0 one: .float 1.0 result: .float 0.0 format: .ascii "%f" .global main main: push $x push $format call scanf finit flds x fmuls x #x*x fadds 1 fsqrt fsub 1 fsts result xor %eax, %eax push $result push $text call printf pushl $0 call exit
in gnu assembler, $ designates literal value (a value encoded instruction). value of label address. $x, $format, $result, , $text addresses of labels; addresses have values labeling. printf not use address %f. must pass value of floating-point number, not address. and, frank kotler notes, must pass 64-bit double, not 32-bit float.
the easiest way might insert add $-8, %esp (or add %esp, $-8, depending on order of operands in assembler version) before fsts result instruction , change fsts result instruction either fst (%esp) or fstl (%esp), depending on assembler. (also, these fstp (%esp) or fstpl (%esp) pop value floating-point stack instead of leaving there.) delete push $result.
these changes allocate 8 bytes on stack (in add instruction) , store floating-point result 8 bytes.
also, expect code responsible cleaning arguments passed called routines: should add 8 stack pointer after calling scanf pop 2 arguments, , should add twelve after calling printf pop new eight-byte argument , four-byte address of format string. program may work without these changes since terminate program calling exit. however, not possible return routine ret instruction without cleaning stack.
supplement
the following code works @ ideone.com, using second choice assembler (gcc-4.7.2):
.text text: .asciz "function result: %4.2f \n" .data x: .float 2.0 one: .float 1.0 result: .float 0.0 format: .asciz "%f" .global main main: push $x push $format call scanf add $8, %esp finit flds x fmuls x #x*x fadds 1 fsqrt fsub 1 add $-8, %esp fstpl (%esp) xor %eax, %eax push $text call printf add $12, %esp pushl $0 call exit
Comments
Post a Comment