;---------------------------------------
;
; Command line arguments via FASM & GCC (Linux64)
; Calculate SIN rad from prompt input
; (c)2018 S.A.R
;
; Compile: fasm sinr64.asm
; Link   : gcc -m64 sinr64.o -s -o sinr64 -lm
; Usage  : sinr64 v1 v2 vn (single-spaced)
;
;---------------------------------------
	format ELF64
	public main

	extrn printf
	extrn atof
	extrn sin
	
	section '.data' writeable
frmt 	db 'sinr(%#+.6f) = %#+.10f',0ah,0
usge 	db 0ah,' Usage: sinr64 v1 v2 vn (single-spaced)',0ah,0ah,0
val1 	dq 0.0


	section '.text' executable
main:
	sub	rsp,8
	mov	ebx,edi		;argc
	sub	ebx,1
	jz	usage
	mov	r12,rsi		;Table of argument string pointers
	add	r12,8		;skip prog's name
more:	mov	rdi,[r12]	;Argument string pointer
	call	strlengthd
	mov	byte[rdi+rdx],0
	call	atof
	movq	[val1],xmm0
	mov	eax,1
	call	sin
	movq	xmm1,xmm0
	movq	xmm0,[val1]
	mov	rdi,frmt
	mov	eax,2
	call	printf
	add	r12,8		;Next argument string pointer
	sub	ebx,1		;argument countdown
	jnz	more
	add	rsp,8
	ret
usage:	mov	rdi,usge
	call	printf
	add	rsp,8
	ret
	
strlengthd:
	push	rdi
	mov	al,0
	cmp	rbx,1
	je	skip
	mov	al,20h
skip:	mov	rcx,-1
	repne	scasb
	mov	rdx,-2
	sub	rdx,rcx
	pop	rdi
	ret
	
