azan

prayers time calculator written in nasm x86-64
git clone https://git.afify.dev/azan.git
Log | Files | Refs | README | LICENSE

macros.s (3837B)


      1 ; See LICENSE file for copyright and license details.
      2 
      3 %ifndef MACROS_S
      4 %define MACROS_S
      5 
      6 %define EXIT_SUCCESS	0
      7 %define EXIT_FAILURE	1
      8 %define STDOUT		1
      9 %define STDERR		2
     10 %define ROUND_UP	10B ;toward +inf
     11 %define ROUND_DOWN	01B ;toward -inf
     12 %define MAX_ARGC	2
     13 
     14 %macro CHECK_BSD 0
     15 %ifdef OpenBSD
     16 section .note.openbsd.ident note
     17 	dd 8, 4, 1
     18 	db "OpenBSD", 0
     19 	dd 0
     20 %elifdef NetBSD
     21 section .note.openbsd.ident note
     22 	dd 7, 4, 1
     23 	db "NetBSD", 0, 0
     24 	dd 0
     25 %endif
     26 %endmacro
     27 
     28 %macro OPENBSD_PLEDGE 0
     29 %ifdef OpenBSD
     30 	mov	rax, SYS_pledge	;pledge(
     31 	mov	rdi, promises	;const char *promises
     32 	xor	rsi, rsi	;const char *execpromises
     33 	syscall
     34 	cmp	rax, 0
     35 	jl	die_pledge
     36 %endif
     37 %endmacro
     38 
     39 %macro EEXIT 1
     40 	mov rax, SYS_exit
     41 	mov rdi, %1
     42 	syscall
     43 %endmacro
     44 
     45 %macro DIE 2
     46 	mov rax, SYS_write
     47 	mov rdi, STDERR
     48 	mov rsi, %1
     49 	mov rdx, %2
     50 	syscall
     51 	EEXIT EXIT_FAILURE
     52 %endmacro
     53 
     54 %macro PRINT_HM 0
     55 	SET_MSG
     56 	mov rax, SYS_write
     57 	mov rdi, STDOUT
     58 	mov rsi, res_msg
     59 	mov rdx, res_len
     60 	syscall
     61 %endmacro
     62 
     63 %macro SEC_TO_HM 1
     64 	;hours = floor(diff / sec_inhour) = xmm10
     65 	movsd	xmm10, %1
     66 	divsd	xmm10, [sec_inhour]
     67 	roundsd xmm10, xmm10, ROUND_DOWN
     68 	cvtsd2si r8, xmm10
     69 
     70 	;remaining_seconds = diff - (hours * sec_inhour) = xmm14
     71 	movsd	xmm14, %1
     72 	mulsd	xmm10, [sec_inhour]
     73 	subsd	xmm14, xmm10
     74 
     75 	;minutes = remaining_seconds / sec_inmin
     76 	divsd	xmm14, [sec_inmin]
     77 	roundsd	xmm14, xmm14, ROUND_DOWN
     78 	cvtsd2si r9, xmm14
     79 %endmacro
     80 
     81 %macro SET_MSG 0
     82 	xor rdx, rdx
     83 	mov rbx, 0xa
     84 	mov rax, r8
     85 	div ebx
     86 	add rax, 0x30
     87 	add rdx, 0x30
     88 	mov [res_msg+2], al
     89 	mov [res_msg+3], dl
     90 
     91 	xor rdx, rdx
     92 	mov rbx, 10
     93 	mov rax, r9
     94 	div ebx
     95 	add rax, 0x30
     96 	add rdx, 0x30
     97 	mov [res_msg+5], al
     98 	mov [res_msg+6], dl
     99 %endmacro
    100 
    101 %macro CALC_P2 0
    102 	;p2 =	cos(convert_degrees_to_radians(latitude)) *
    103 	;	cos(convert_degrees_to_radians(D)) = xmm1
    104 	movsd	xmm1, [latitude]
    105 	mulsd	xmm1, [to_rad]
    106 	COS	xmm1
    107 	movsd	xmm2, xmm8,
    108 	mulsd	xmm2, [to_rad]
    109 	COS	xmm2
    110 	mulsd	xmm1, xmm2
    111 %endmacro
    112 
    113 %macro CALC_P3 0
    114 	;p3 =	sin(convert_degrees_to_radians(latitude)) *
    115 	;	sin(convert_degrees_to_radians(D)) = xmm2
    116 	movsd	xmm2, [latitude]
    117 	mulsd	xmm2, [to_rad]
    118 	SIN	xmm2
    119 	movsd	xmm3, xmm8, ; xmm8 = D
    120 	mulsd	xmm3, [to_rad]
    121 	SIN	xmm3
    122 	mulsd	xmm2, xmm3
    123 %endmacro
    124 
    125 %macro NORM 2;	n = x - (y * floor(x / y));
    126 	movsd xmm14, %1
    127 	divsd xmm14, %2
    128 	roundsd xmm14, xmm14, ROUND_DOWN
    129 	mulsd xmm14, %2
    130 	subsd %1, xmm14
    131 %endmacro
    132 
    133 %macro CALC_T 1; T = p1 * p5
    134 	;p4 = -1.0 * sin(convert_degrees_to_radians(alpha)) = %1
    135 	mulsd	%1, [to_rad]
    136 	SIN	%1
    137 	mulsd	%1, [neg1]
    138 
    139 	;p5 = convert_radians_to_degrees(acos((p4 - p3) / p2)) = %1
    140 	subsd	%1, xmm2	; p4 - p3
    141 	divsd	%1, xmm1	; / p2
    142 	ACOS	%1
    143 	mulsd	%1, [to_deg]	; %1 = p5
    144 
    145 	;T = p1 * p5 = %1
    146 	mulsd	%1, [p1]
    147 %endmacro
    148 
    149 %macro PRINT_INT 1
    150 	nop
    151 	mov	rsi, tmp0+11	; pointer to the end of decimal number
    152 	mov	byte [rsi], 0xa	; add '\n'
    153 	cvttsd2si rax, 	%1	; convert double to int
    154 	mov	rbx, 0xa        ; hex number will divide to 10
    155 	mov	rcx, 1          ; decimal number length + '\n'
    156 	call next_digit
    157 
    158 	;print
    159 	mov	rax, SYS_write	; system call number (sys_write)
    160 	mov	rdi, STDOUT     ; first argument:  file handle (stdout)
    161 	mov	rdx, rcx        ; second argument: pointer to string
    162 	syscall
    163 %endmacro
    164 
    165 next_digit:
    166 	inc	rcx             ; calculate output length
    167 	xor	rdx, rdx        ; remainder storage should be 0 before divide
    168 	div	rbx             ; divide hex number to 10
    169 	add	rdx, 0x30       ; calculate ascii code of remainder
    170 	dec	rsi             ; calculate decimal digit place
    171 	mov	[rsi], dl       ; put decimal digit into string
    172 	cmp	rax, 0          ; is there hex digits any more?
    173 	jnz	next_digit
    174 	ret
    175 
    176 %macro PRINT_FLAG 1
    177 	movsd	xmm14, %1	;copy prayer to xmm14
    178 	cmp	r12b, byte 'u'
    179 	je	print_unix
    180 	cmp	r12b, byte 'n'
    181 	je	print_24
    182 	cmp	r12b, byte 'N'
    183 	je	print_12
    184 	subsd	%1, xmm6	;print diff = prayer time - tstamp = %1
    185 	SEC_TO_HM %1
    186 	PRINT_HM
    187 	EEXIT EXIT_SUCCESS
    188 %endmacro
    189 
    190 %endif ;MACROS_S