flat assembler
Message board for the users of flat assembler.

Index > Linux > Reading int or float from the command line

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

Joined: 01 Mar 2011
Posts: 555
fasmnewbie 20 Apr 2017, 13:08
I changed your program a little bit so that you can take both formats from the command prompt

; ----------------------------------------------------------------------------- 
; 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 
format: db "%g", 10, 0 
error:  db "There are no cmds to average", 10, 0 

section .text 
        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
        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 
        movq    xmm0,xmm8
        movq    xmm1,[count]
        cvtsi2sd xmm1,[count]
        divsd   xmm0, xmm1              
        mov     rdi, format             ;erste arg für printf 
        mov     rax, 1                  ;printf hat var args es gibt 1 nicht 
        call    printf                  ;printf(format, sum/count) 
        add     rsp, 8                  ;wiederherstellen des stackpointers 

        mov     rdi, error 
        xor     rax, rax 
        call    printf 
        add     rsp,8

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

Joined: 23 Jul 2015
Posts: 67
dstyl 25 Apr 2017, 15:12
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

Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.