azan

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

commit acf99376cac9cea7316cc25185621f378e0af26a
parent 168ed1a2e9279fe45a8976d6cca8e5d7ba7b80fa
Author: afify <hassan@afify.dev>
Date:   Fri, 20 Nov 2020 08:15:02 +0300

[feat] compare time with fajr & duhr, clean bss

- print msg of next prayer (fajr and duhr)
- clean bss section:
	use tmp0 and tmp1
- add  COS SIN ACOS ASIN ATAN2 macros

Diffstat:
Mazan-nasm.s | 314+++++++++++++++++++++++++++++++------------------------------------------------
Mmacros.s | 51+++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 168 insertions(+), 197 deletions(-)

diff --git a/azan-nasm.s b/azan-nasm.s @@ -9,8 +9,8 @@ BITS 64 CHECK_OPENBSD section .rodata - sec_inday: dq 86400.0 - jul1970: dq 2440587.5 + sec_inday: dq 0x40f5180000000000 ;double 86400.0 + jul1970: dq 0x41429ec5c0000000 ;double 2440587.5 offset: dq 0xc142b42c80000000 ;double -2451545 to_rad: dq 0x3f91df46a2529d39 ;double pi / 180 to_deg: dq 0x404ca5dc1a63c1f8 ;double 180 / pi @@ -23,30 +23,16 @@ section .rodata sing_1: dq 0x3FFEA3D70A3D70A4 ;double 1.915 sing_2: dq 0x3F947AE147AE147B ;double 0.020 RA_1: dq 0x402E000000000000 ;double 15.0 - asin_1: dq 0x3FF0000000000000 ;double 1 eqt_1: dq 0x4076800000000000 ;double 360.0 duhr_1: dq 0x4028000000000000 ;double 12.0 pray_1: dq 0x4038000000000000 ;double 24.0 neg1: dq 0xBFF0000000000000 ;double -1.0 - one: dq 0x3FF0000000000000 ;double 1 p1: dq 0X3fb1111111111111 ;double 0.066666666666666666 hours_to_sec: dq 0x40ac200000000000 ;double 3600 section .bss - - tstamp: resb 12 - julian: resq 1 - g: resq 1 - g2: resq 1 - sing: resq 1 - sin2g: resq 1 - sine: resq 1 - cose: resq 1 - sinL: resq 1 - cosL: resq 1 - RA_2: resq 1 - asin_x: resq 1 - x: resq 1 + tmp0: resq 1 + tmp1: resq 1 section .text global _start @@ -72,12 +58,12 @@ die_version: get_timestamp: mov rax, SYS_gettimeofday ;sys_gettimeofday( - mov rdi, tstamp ;struct timeval *tv, + mov rdi, tmp0 ;struct timeval *tv, mov rsi, rsi ;struct timezone* tz syscall -; start_of_day: ; = tstamp - (tstamp % 86400); - mov edi, [tstamp] + ; start_of_day = tstamp - (tstamp % 86400); + mov edi, [tmp0] movsx rax, edi mov edx, edi imul rax, rax, -1037155065 @@ -90,140 +76,87 @@ get_timestamp: mov eax, edi sub eax, edx cvtsi2sd xmm15, rdx - movsd xmm14, [time_zone] - mulsd xmm14, [hours_to_sec] - subsd xmm15, xmm14 + movsd xmm14, [time_zone] + mulsd xmm14, [hours_to_sec] + subsd xmm15, xmm14 + + ;tstamp = xmm6 convert tstamp to double + cvtsi2sd xmm6, [tmp0] -; calc_julian:;(tstamp / sec_inday) + jul1970 - cvtsi2sd xmm0, [tstamp] ;convert tstamp to double + ;julian = tstamp / sec_inday) + jul1970 = xmm0 + movsd xmm0, xmm6 ;copy tstamp to xmm0 divsd xmm0, [sec_inday] ;tstamp / sec_inday addsd xmm0, [jul1970] ;div result + jul1970 -; xmm0 = julian -; calc_equation_of_time: - addsd xmm0, [offset] ;d = julian - offset -; xmm0 = d +calc_equation_of_time: + ;d = julian - offset = xmm0 + addsd xmm0, [offset] - movsd xmm2, [to_rad] - movsd xmm1, [g_1] ;g = to_rad * ((d * 0.98560028) + 357.529) + ;g = to_rad * ((d * 0.98560028) + 357.529) = xmm1 + movsd xmm1, [g_1] mulsd xmm1, xmm0 addsd xmm1, [g_2] - mulsd xmm1, xmm2 -; xmm1 = g -; xmm2 = to_rad + mulsd xmm1, [to_rad] - movsd xmm3, [e_3] ;e = to_rad * (23.439 - (d * 0.00000036)) + ;e = to_rad * (23.439 - (d * 0.00000036)) = xmm3 + movsd xmm3, [e_3] mulsd xmm3, xmm0 addsd xmm3, [e_4] - mulsd xmm3, xmm2 -; xmm3 = e + mulsd xmm3, [to_rad] - movsd xmm4, [q_5] ;q = (d * 0.98564736) + 280.459 + ;q = (d * 0.98564736) + 280.459 = xmm4 + movsd xmm4, [q_5] mulsd xmm4, xmm0 addsd xmm4, [q_6] -; xmm4 = q - -; sing: ;sing = 1.915 * sin(g); - movsd xmm5, [sing_1] - finit - movsd [g], xmm1 - fld qword [g] - fsin - fstp qword [sing] - mulsd xmm5, [sing] -; xmm5 = sing - -; sin2g: ;sin2g = 0.020 * sin(2.0*g) - movsd xmm6, [sing_2] + + ;sing = 1.915 * sin(g) = xmm5 + movsd xmm5, xmm1 + SIN xmm5 + mulsd xmm5, [sing_1] + + ;sin2g = 0.020 * sin(2.0*g) = xmm1 addsd xmm1, xmm1 - movsd [g2], xmm1 - fld qword [g2] - fsin - fstp qword [sin2g] - mulsd xmm6, [sin2g] -; xmm1 = 2 * g -; xmm6 = sin2g - -; sine: ;sin(e) - movsd [sine], xmm3 - fld qword [sine] - fsin - fstp qword [sine] - -; cose: ;cos(e) - movsd [cose], xmm3 - fld qword [cose] - fcos - fstp qword [cose] - -; L: ;L = to_rad(q + sing + sin2g); - addsd xmm5, xmm6 ;sing + sin2g - addsd xmm5, xmm4 ;+ q - mulsd xmm5, xmm2 ;* to_rad -; xmm5 = L - -; sinL: ;sin(L) - movsd [sinL], xmm5 - fld qword [sinL] - fsin - fstp qword [sinL] - -; cosL: ;cos(L) - movsd [cosL], xmm5 - fld qword [cosL] - fcos - fstp qword [cosL] - -; RA: ;RA = to_deg(atan2(cose * sinL, cosL) / 15.0); - movsd xmm7, [cose] - mulsd xmm7, [sinL] - movsd [cose], xmm7 - fld qword [cose] ;load cose - fld qword [cosL] ;load cosL - fpatan ;calc atan2 (cose / cosL) - fstp qword [RA_2] ;save angle - movsd xmm7, [RA_2] + SIN xmm1 + mulsd xmm1, [sing_2] + + ;sin(e) = xmm8 + movsd xmm8 , xmm3 + SIN xmm8 + + ;cos(e) = xmm7 + movsd xmm7, xmm3 + COS xmm7 + + ;L = to_rad(q + sing + sin2g) = xmm5 + addsd xmm5, xmm1 + addsd xmm5, xmm4 + mulsd xmm5, [to_rad] + + ;sin(L) = xmm2 + movsd xmm2, xmm5 + SIN xmm2 + + ;cos(L) = xmm5 + COS xmm5 + + ;RA = to_deg(atan2(cose * sinL, cosL) / 15.0) = xmm7 + mulsd xmm7, xmm2 ;cose * sinL + ATAN2 xmm7, xmm5 ;result in xmm7 divsd xmm7, [RA_1] ;atan2 result /15.0 mulsd xmm7, [to_deg] ;* to_deg -; xmm7 = RA - -; D: ;D = to_deg(asin(sine * sinL)); -; asin = arctan(x / sqrt(1 - x * x)) - movsd xmm8, [sine] - mulsd xmm8, [sinL] - movsd [asin_x], xmm8 - ; xmm8 = x - - movsd xmm9, xmm8 ; xmm8 = x - mulsd xmm9, xmm8 ; xmm9 = x * x - movsd xmm10, [asin_1] ; xmm10 = 1 - subsd xmm10, xmm9 ; 1 - xmm9 - movsd [asin_1], xmm10 ; asin_1 = (1-x*x) - movsd [asin_x], xmm8 ; asin_1 = (1-x*x) -; xmm9 = x * x -; xmm10 = 1 - xmm9 - - fld qword [asin_x] - fld qword [asin_1] - fsqrt - fpatan - fstp qword [asin_1] - movsd xmm8, [asin_1] + + ;D = to_deg(asin(sine * sinL)) = xmm8 + mulsd xmm8, xmm2 + ASIN xmm8 mulsd xmm8, [to_deg] -; xmm8 = D - -; Eqt ;EqT = q / 15.0 - RA; - movsd xmm9, xmm4 ; move q to xmm9 - divsd xmm9, [RA_1] ; q / 15.0 - subsd xmm9, xmm7 ; - RA -; xmm9 = EqT -; EqT = EqT - 360.0 - subsd xmm9, [eqt_1] -;xmm8 = D -;xmm9 = EqT - -; get_duhr: duhr = 12.0 + time_zone - EqT - (longitude / 15.0); -; xmm0 - (pray_1 * floor(xmm1 / pray_1)); + + ;EqT = q / 15.0 - RA = xmm9 + movsd xmm9, xmm4 ;move q to xmm9 + divsd xmm9, [RA_1] ;q / 15.0 + subsd xmm9, xmm7 ;- RA + subsd xmm9, [eqt_1] ;EqT = EqT - 360.0 + +get_duhr: ;duhr = 12.0+time_zone-EqT-(longitude/15.0); movsd xmm1, [longitude] divsd xmm1, [RA_1] movsd xmm0, [duhr_1] @@ -231,88 +164,87 @@ get_timestamp: subsd xmm0, xmm9 subsd xmm0, xmm1 -; normalize duhr + ;normalize duhr + ;xmm0 - (pray_1 * floor(xmm1 / pray_1)); movsd xmm1, xmm0 divsd xmm1, [pray_1] roundsd xmm1, xmm1, ROUND_DOWN ;floor(xmm1) mulsd xmm1, [pray_1] subsd xmm0, xmm1 -;xmm0 = duhr + ;xmm0 = duhr -; get_fajr:; fajr = duhr - T(fajr_angle, D); - ;T -; p2 = cos(convert_degrees_to_radians(latitude)) * -; cos(convert_degrees_to_radians(D)); +get_fajr: ;fajr = duhr - T(fajr_angle, D); + ;calculate T = p1 * p5 + ;p2 = cos(convert_degrees_to_radians(latitude)) * + ; cos(convert_degrees_to_radians(D)) = xmm1 movsd xmm1, [latitude] mulsd xmm1, [to_rad] - movsd [x], xmm1 - fld qword [x] - fcos - fstp qword [x] - movsd xmm1, [x] - + COS xmm1 movsd xmm2, xmm8, mulsd xmm2, [to_rad] - movsd [x], xmm2 - fld qword [x] - fcos - fstp qword [x] - movsd xmm2, [x] + COS xmm2 mulsd xmm1, xmm2 -;xmm1 = p2 -; p3 = sin(convert_degrees_to_radians(latitude)) * -; sin(convert_degrees_to_radians(D)); + ;p3 = sin(convert_degrees_to_radians(latitude)) * + ; sin(convert_degrees_to_radians(D)) = xmm2 movsd xmm2, [latitude] mulsd xmm2, [to_rad] - movsd [sine], xmm2 - fld qword [sine] - fsin - fstp qword [sine] - movsd xmm2, [sine] - + SIN xmm2 movsd xmm3, xmm8, ; xmm8 = D mulsd xmm3, [to_rad] - movsd [sine], xmm3 - fld qword [sine] - fsin - fstp qword [sine] - movsd xmm3, [sine] + SIN xmm3 mulsd xmm2, xmm3 -;xmm2 = p3 -; p4 = -1.0 * sin(convert_degrees_to_radians(alpha)); + ;p4 = -1.0 * sin(convert_degrees_to_radians(alpha)) = xmm3 movsd xmm3, [fajr_angle] mulsd xmm3, [to_rad] - movsd [sine], xmm3 - fld qword [sine] - fsin - fstp qword [sine] - movsd xmm3, [sine] + SIN xmm3 mulsd xmm3, [neg1] -; p5 = convert_radians_to_degrees(acos((p4 - p3) / p2)); - subsd xmm3, xmm2 ; p4 - p3 - divsd xmm3, xmm1 ; / p2 - movsd [x], xmm3 - ACOS [x] - movsd xmm3, [x] + ;p5 = convert_radians_to_degrees(acos((p4 - p3) / p2)) = xmm3 + subsd xmm3, xmm2 ; p4 - p3 + divsd xmm3, xmm1 ; / p2 + ACOS xmm3 mulsd xmm3, [to_deg] ; xmm3 = p5 - mulsd xmm3, [p1] ; xmm3 = T - - movsd xmm4, xmm3 - movsd xmm3, xmm0 ; xmm3 = duhr - subsd xmm3, xmm4 ; xmm3 = duhr - T -; convert_fajr_to_sec: - mulsd xmm3, [hours_to_sec] ; convert to seconds - roundsd xmm3, xmm3, ROUND_DOWN ;floor(xmm1) - addsd xmm3, xmm15 + ;T = p1 * p5 = xmm3 + mulsd xmm3, [p1] + + ;fajr = duhr - T = xmm3 + movsd xmm4, xmm3 + movsd xmm3, xmm0 + subsd xmm3, xmm4 + +convert_fajr_to_time: + mulsd xmm3, [hours_to_sec] ;convert to seconds + roundsd xmm3, xmm3, ROUND_DOWN + addsd xmm3, xmm15 ;fajr seconds + start_of_day + ucomisd xmm3, xmm6 ;if fajr > tstamp + jae calculate_left_fajr + jmp convert_duhr_to_time + +convert_duhr_to_time: + mulsd xmm0, [hours_to_sec] ;convert to seconds + roundsd xmm0, xmm0, ROUND_DOWN + addsd xmm0, xmm15 ;duhr seconds + start_of_day + ucomisd xmm0, xmm6 ;if duhr > tstamp + jae calculate_left_duhr + jmp calculate_asr + +calculate_left_fajr: + DIE fajr_msg, fajr_len +; +calculate_left_duhr: + DIE duhr_msg, duhr_len +; +calculate_asr: + DIE asr_msg, asr_len ; duhr: ; xmm0 ; p2: ; xmm1 ; p3: ; xmm2 ; fajr: ; xmm3 +; tstamp: ; xmm6 ; EqT: ; xmm9 ; D: ; xmm8 ; start_of_day: ; xmm15 diff --git a/macros.s b/macros.s @@ -17,6 +17,15 @@ section .rodata version_msg: db "azan-nasm-", VERSION, 10, 0 version_len: equ $ - version_msg + fajr_msg: db "fajr is bigger", 10, 0 + fajr_len: equ $ - fajr_msg + + duhr_msg: db "duhr is bigger", 10, 0 + duhr_len: equ $ - duhr_msg + + asr_msg: db "asr is bigger", 10, 0 + asr_len: equ $ - asr_msg + %macro CHECK_OPENBSD 0 %ifdef OpenBSD section .note.openbsd.ident note @@ -41,8 +50,9 @@ section .note.openbsd.ident note EEXIT EXIT_FAILURE %endmacro -%macro ACOS 1; acos(x) = atan(sqrt((1-x*x)/(x*x))) - fld qword %1 +%macro ACOS 1 ;acos(x) = atan(sqrt((1-x*x)/(x*x))) + movsd [tmp0], %1 + fld qword [tmp0] fld st0 fmul st0, st1 fld1 @@ -50,18 +60,47 @@ section .note.openbsd.ident note fsqrt fxch fpatan - fstp qword %1 + fstp qword [tmp0] + movsd %1, [tmp0] %endmacro -%macro ASIN 1 - fld qword %1 +%macro ASIN 1 ;asin(x) = atan(sqrt(x*x/(1-x*x))) + movsd [tmp0], %1 + fld qword [tmp0] fld st0 fmul st0, st1 fld1 fsubrp st1, st0 fsqrt fpatan - fstp qword %1 + fstp qword [tmp0] + movsd %1, [tmp0] +%endmacro + +%macro COS 1 + movsd [tmp0], %1 + fld qword [tmp0] + fcos + fstp qword [tmp0] + movsd %1, [tmp0] +%endmacro + +%macro SIN 1 + movsd [tmp0], %1 + fld qword [tmp0] + fsin + fstp qword [tmp0] + movsd %1, [tmp0] +%endmacro + +%macro ATAN2 2 + movsd [tmp0], %1 + movsd [tmp1], %2 + fld qword [tmp0] ;x + fld qword [tmp1] ;y + fpatan + fstp qword [tmp0] + movsd %1, [tmp0] %endmacro %endif ;MACROS_S