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