;---------------------------------------
;
; Command line arguments via FASM & GCC (win32)
; Calculate SIN rad from prompt input
; (c)2018 S.A.R
;
; Compile: fasm sine.asm
; Link   : gcc -m32 sine.obj -s -o sine.exe
; Usage  : sine v1 v2 vn (single-spaced)
;
;---------------------------------------
format MS COFF
public _WinMain@16

extrn _printf
extrn _atof

section '.data' writeable
msg1 db 0ah,' Usage: sine v1 v2 vn (single-spaced)',0ah,0ah,0
fmt1 db 'sine(%#+.4f) = %#+.6f',0ah,0
val1 dq 0.0
val2 dq 0.0

section '.text' executable
_WinMain@16:
        enter   0,0
        sub     ebx,1            ;argc
        cmp     ebx,1
        jb      error
        mov     edi,[ebp+16]     ;*argv
        xor     esi,esi
  more: call    str_lengthd
        mov     byte[edi+esi],0  ;0-ended for atof()
        push    edi
        call    _atof
        add     esp,4
        fst     qword[val1]
        fsin
        fstp    qword[val2]
        finit
        push    dword[val2+4]
        push    dword[val2]
        push    dword[val1+4]
        push    dword[val1]
        push    fmt1
        call    _printf
        add     esp,4*5
        add     edi,esi          ;Next argument string
        add     edi,1            ;skip space
        sub     ebx,1            ;argument countdown
        jnz     more
        leave
        ret
error:  push    msg1
        call    _printf
        add     esp,4
        leave
        ret

;in: EDI
;Find length of delimiter-ended string (20h)
;return length in ESI
str_lengthd:
        push    edi ecx
        mov     al,0
        cmp     ebx,1
        je      last
        mov     al,20h
  last: mov     ecx,-1
        repne   scasb
        mov     esi,-2
        sub     esi,ecx
        pop     ecx edi
        ret
