flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Linux > Reading int or float from the command line

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
fasmnewbie



Joined: 01 Mar 2011
Posts: 418
I changed your program a little bit so that you can take both formats from the command prompt


Code:
; ----------------------------------------------------------------------------- 
; 64-bit program that treats all its command line arguments as floating point  
; number and displays their average as a floating point number.  This program  
; uses a data section to store intermediate results, not that it has to, but  
; only to illustrate how data sections are used. 
; ----------------------------------------------------------------------------- 
global  main 

extern  atof
extern  printf 

section .data 
count:  dq 0 
sum:    dq 0 
formatdb "%g"100 
error:  db "There are no cmds to average"100 

section .text 
main
        sub     rsp,8
        dec     rdi                     ;argc-1, da wir den programm namen ignorieren 
        jz      nothingToAverage        ;wenn rdi == 0 
        mov     [count], rdi            ;speichere args in [count]
        pxor    xmm8,xmm8
accumulate
        push    rdi                     ;sichere reg vor atoi aufruf 
        push    rsi 
        mov     rdi, [rsi+rdi*8]        ;argv[rdi] c alignment beachten! 
        call    atof                    ;rax enthält den wert von arg 
        pop     rsi                     ;wiederherstellen der reg nach 
        pop     rdi                     ;aufruf von atoi 
        addpd   xmm8,xmm0               ;summieren der werte
        dec     rdi                     ;argc runterzählen 
        jnz     accumulate              ;wiederhole wen argc != 0 
average
        movq    xmm0,xmm8
        movq    xmm1,[count]
        cvtsi2sd xmm1,[count]
        divsd   xmm0xmm1              
        mov     rdiformat             ;erste arg für printf 
        mov     rax1                  ;printf hat var args es gibt 1 nicht 
        call    printf                  ;printf(format, sum/count) 
        add     rsp8                  ;wiederherstellen des stackpointers 
        ret

nothingToAverage
        mov     rdierror 
        xor     raxrax 
        call    printf 
        add     rsp,8
        ret 



I hope this is what you want. This is using ATOF though
Post 20 Apr 2017, 13:08
View user's profile Send private message Visit poster's website Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 48
excuse me im sorry for my bad english. thanks for your time Smile
Post 25 Apr 2017, 15:12
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >

Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.