azan

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

commit 076217cc7b876d9b7c361efa5b5a87a53ed7bcbf
parent 968f04993b74f11763d05ffc9c3078201412cc26
Author: afify <hassan@afify.dev>
Date:   Wed, 18 Nov 2020 07:30:29 +0300

[feat] calculate fajr unix timestamp, config.s

- add config.s to configure timezone and other user options
- remove util.s (no usage)
- add new macros ACOS ASIN ROUND_UP ROUND_DOWN

Diffstat:
MMakefile | 2+-
Mazan-nasm.s | 137+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Aconfig.s | 30++++++++++++++++++++++++++++++
Mmacros.s | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Dutil.s | 50--------------------------------------------------
5 files changed, 221 insertions(+), 64 deletions(-)

diff --git a/Makefile b/Makefile @@ -4,7 +4,7 @@ include config.mk BIN = azan-nasm -SRC = ${BIN}.s util.s +SRC = ${BIN}.s OBJ = ${SRC:.s=.o} all: options ${BIN} diff --git a/azan-nasm.s b/azan-nasm.s @@ -5,14 +5,15 @@ BITS 64 %include "syscalls.s" %include "macros.s" +%include "config.s" CHECK_OPENBSD section .rodata sec_inday: dq 86400.0 jul1970: dq 2440587.5 offset: dq 0xc142b42c80000000 ;double -2451545 - to_rad: dq 0x3f91df46a2529d39 ;double pi / 180 to radian - to_deg: dq 0x404ca5dc1a63c1f8 ;double 180 / pi to degree + to_rad: dq 0x3f91df46a2529d39 ;double pi / 180 + to_deg: dq 0x404ca5dc1a63c1f8 ;double 180 / pi g_1: dq 0x3fef8a099930e901 ;double 0.98560027999999999 g_2: dq 0x40765876c8b43958 ;double 357.529 e_3: dq 0xbe9828c0be769dc1 ;double -3.5999999999999999E-7 @@ -24,11 +25,17 @@ section .rodata 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 - EqT: resq 1 g: resq 1 g2: resq 1 sing: resq 1 @@ -39,20 +46,39 @@ section .bss cosL: resq 1 RA_2: resq 1 asin_x: resq 1 + x: resq 1 section .text global _start _start: -; get_timestamp: +get_timestamp: mov rax, SYS_gettimeofday ;sys_gettimeofday( mov rdi, tstamp ;struct timeval *tv, mov rsi, rsi ;struct timezone* tz syscall -; calc_julian:(timestamp / sec_in_day) + jul1970 - cvtsi2sd xmm0, [tstamp] ;convert timestamp to double - divsd xmm0, [sec_inday] ;timestamp / sec_in_day +; start_of_day: ; = tstamp - (tstamp % 86400); + mov edi, [tstamp] + movsx rax, edi + mov edx, edi + imul rax, rax, -1037155065 + sar edx, 31 + shr rax, 32 + add eax, edi + sar eax, 16 + sub eax, edx + imul edx, eax, 86400 + mov eax, edi + sub eax, edx + cvtsi2sd xmm15, rdx + movsd xmm14, [time_zone] + mulsd xmm14, [hours_to_sec] + subsd xmm15, xmm14 + +; calc_julian:;(tstamp / sec_inday) + jul1970 + cvtsi2sd xmm0, [tstamp] ;convert tstamp to double + divsd xmm0, [sec_inday] ;tstamp / sec_inday addsd xmm0, [jul1970] ;div result + jul1970 ; xmm0 = julian @@ -175,5 +201,102 @@ _start: ; 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)); + movsd xmm1, [longitude] + divsd xmm1, [RA_1] + movsd xmm0, [duhr_1] + addsd xmm0, [time_zone] + subsd xmm0, xmm9 + subsd xmm0, xmm1 + +; normalize duhr + movsd xmm1, xmm0 + divsd xmm1, [pray_1] + roundsd xmm1, xmm1, ROUND_DOWN ;floor(xmm1) + mulsd xmm1, [pray_1] + subsd xmm0, xmm1 +;xmm0 = duhr + +; get_fajr:; fajr = duhr - T(fajr_angle, D); + ;T +; p2 = cos(convert_degrees_to_radians(latitude)) * +; cos(convert_degrees_to_radians(D)); + movsd xmm1, [latitude] + mulsd xmm1, [to_rad] + movsd [x], xmm1 + fld qword [x] + fcos + fstp qword [x] + movsd xmm1, [x] + + movsd xmm2, xmm8, + mulsd xmm2, [to_rad] + movsd [x], xmm2 + fld qword [x] + fcos + fstp qword [x] + movsd xmm2, [x] + mulsd xmm1, xmm2 +;xmm1 = p2 + +; p3 = sin(convert_degrees_to_radians(latitude)) * +; sin(convert_degrees_to_radians(D)); + movsd xmm2, [latitude] + mulsd xmm2, [to_rad] + movsd [sine], xmm2 + fld qword [sine] + fsin + fstp qword [sine] + movsd xmm2, [sine] + + movsd xmm3, xmm8, ; xmm8 = D + mulsd xmm3, [to_rad] + movsd [sine], xmm3 + fld qword [sine] + fsin + fstp qword [sine] + movsd xmm3, [sine] + mulsd xmm2, xmm3 +;xmm2 = p3 + +; p4 = -1.0 * sin(convert_degrees_to_radians(alpha)); + movsd xmm3, [fajr_angle] + mulsd xmm3, [to_rad] + movsd [sine], xmm3 + fld qword [sine] + fsin + fstp qword [sine] + movsd xmm3, [sine] + 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] + 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 + +; duhr: ; xmm0 +; p2: ; xmm1 +; p3: ; xmm2 +; fajr: ; xmm3 +; EqT: ; xmm9 +; D: ; xmm8 +; start_of_day: ; xmm15 EEXIT EXIT_SUCCESS diff --git a/config.s b/config.s @@ -0,0 +1,30 @@ +; See LICENSE file for copyright and license details. + +%ifndef CONFIG_S +%define CONFIG_S + +section .rodata +; Location + time_zone: dq +3.0 + longitude: dq 39.826168 + latitude: dq 21.422510 + altitude: dq 0.0 + +; calculation + use_umm_al_qura db 1 + ramadan db 0 + +; Fajr Isha Asr + fajr_angle dq 18.5 + isha_angle dq 17.0 ; used if use_umm_al_qura = 0 + use_major db 1 ; Asr method, use hanafi if 0 + +;Muslim World League 18 17 +;Umm al-Qura University, Makkah 18.5 autoset +;Egyptian General Authority of Survey 19.5 17.5 +;Islamic Society of North America (ISNA) 15 15 +;University of Islamic Sciences, Karachi 18 18 +;Institute of Geophysics, University of Tehran 17.7 14* +;Shia Ithna Ashari, Qum 16 14 + +%endif ;CONFIG_S diff --git a/macros.s b/macros.s @@ -3,15 +3,17 @@ %ifndef MACROS_S %define MACROS_S -%define EXIT_SUCCESS 0 -%define EXIT_FAILURE 1 -%define STDOUT 1 -%define STDERR 2 +%define EXIT_SUCCESS 0 +%define EXIT_FAILURE 1 +%define STDOUT 1 +%define STDERR 2 +%define ROUND_UP 10B ;toward +inf +%define ROUND_DOWN 01B ;toward -inf section .rodata failure_msg: db 0x5b, 0x1b, 0x5b, 0x33, 0x31, 0x6d, 0x46\ - 0x41, 0x49, 0x4c, 0x45, 0x44, 0x1b, 0x5b\ - 0x30, 0x6d, 0x5d, 0x20, 0x00 + 0x41, 0x49, 0x4c, 0x45, 0x44, 0x1b, 0x5b\ + 0x30, 0x6d, 0x5d, 0x20, 0x00 failure_msglen: equ $ - failure_msg julian_msg: db "Julian must be greader than offset.", 10, 0 julian_msglen: equ $ - julian_msg @@ -49,4 +51,56 @@ section .note.openbsd.ident note EEXIT EXIT_FAILURE %endmacro +; %macro ACOS 1; acos(x) = atan(sqrt((1-x*x)/(x*x))) +; fld qword %1 +; fld st0 +; fmul +; fld st0 +; fld1 +; fsubr +; fdivr +; fsqrt +; fld1 +; fpatan +; fstp qword %1 +; %endmacro + +; %macro ASIN 1 ;asin(x) = atan(sqrt(x*x/(1-x*x))) +; fld qword %1 +; fld st0 +; fmul +; fld st0 +; fld1 +; fsubr +; fdiv +; fsqrt +; fld1 +; fpatan +; fstp qword %1 +; %endmacro + + +%macro ACOS 1; acos(x) = atan(sqrt((1-x*x)/(x*x))) + fld qword %1 + fld st0 + fmul st0, st1 + fld1 + fsubrp st1, st0 + fsqrt + fxch + fpatan + fstp qword %1 +%endmacro + +%macro ASIN 1 + fld qword %1 + fld st0 + fmul st0, st1 + fld1 + fsubrp st1, st0 + fsqrt + fpatan + fstp qword %1 +%endmacro + %endif ;MACROS_S diff --git a/util.s b/util.s @@ -1,50 +0,0 @@ -BITS 64 -%include "syscalls.s" -%idefine rip rel $ -section .bss - digitSpace: resb 100 - digitSpacePos: resb 8 - -section .text - global _printRAX - -_printRAX: - mov rcx, digitSpace - mov rbx, 10 - mov [rcx], rbx - inc rcx - mov [digitSpacePos], rcx - -_printRAXLoop: - mov rdx, 0 - mov rbx, 10 - div rbx - push rax - add rdx, 48 - - mov rcx, [digitSpacePos] - mov [rcx], dl - inc rcx - mov [digitSpacePos], rcx - - pop rax - cmp rax, 0 - jne _printRAXLoop - -_printRAXLoop2: - mov rcx, [digitSpacePos] - - mov rax, SYS_write - mov rdi, 1 - mov rsi, rcx - mov rdx, 1 - syscall - - mov rcx, [digitSpacePos] - dec rcx - mov [digitSpacePos], rcx - - cmp rcx, digitSpace - jge _printRAXLoop2 - - ret