fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
; fdate.asm — GNU-compatible 'date' command
;
; date: display or set the system date and time
; - date             — print current date/time in default format
; - date -u          — use UTC
; - date +FORMAT     — custom format string
; - date -R          — RFC 5322 date format
; - date -I[FMT]     — ISO 8601 format
; - date --help / --version
;
; Default output: "Thu Mar 12 20:00:00 UTC 2026"
; Uses clock_gettime(228) syscall, always UTC (no TZ file parsing).

%include "include/linux.inc"

extern asm_write
extern asm_exit
extern asm_strlen
extern asm_strcmp

global _start

section .bss
    out_buf: resb 256           ; output buffer
    num_tmp: resb 32            ; temp for number formatting

section .data

; Day names (3-char abbreviations)
day_names:
    db "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"

; Month names (3-char abbreviations)
mon_names:
    db "Jan", "Feb", "Mar", "Apr", "May", "Jun"
    db "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"

; Days per month (non-leap)
days_per_month:
    db 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

; RFC 5322 day names
rfc_day_names:
    db "Sun, ", "Mon, ", "Tue, ", "Wed, ", "Thu, ", "Fri, ", "Sat, "

; Datetime storage
dt_epoch:  dq 0
dt_year:   dw 0
dt_month:  db 0
dt_day:    db 0
dt_hour:   db 0
dt_min:    db 0
dt_sec:    db 0
dt_wday:   db 0
dt_yday:   dw 0
dt_format: db 0
dt_fmt_ptr: dq 0

section .text

; ============================================================
; format_2digit(eax=value 0-99) -> al=tens+'0', ah=ones+'0'
; ============================================================
format_2digit:
    push    rbx
    movzx   eax, al
    xor     edx, edx
    mov     ebx, 10
    div     ebx
    add     al, '0'
    add     dl, '0'
    mov     ah, dl
    pop     rbx
    ret

; ============================================================
; format_4digit(eax=year) -> stores in num_tmp, returns packed eax
; al=thousands, ah=hundreds, bits[16..23]=tens, bits[24..31]=ones
; ============================================================
format_4digit:
    push    rbx
    push    rcx
    movzx   eax, ax
    xor     edx, edx
    mov     ebx, 1000
    div     ebx
    add     al, '0'
    mov     [rel num_tmp], al
    mov     eax, edx
    xor     edx, edx
    mov     ebx, 100
    div     ebx
    add     al, '0'
    mov     [rel num_tmp + 1], al
    mov     eax, edx
    xor     edx, edx
    mov     ebx, 10
    div     ebx
    add     al, '0'
    add     dl, '0'
    mov     [rel num_tmp + 2], al
    mov     [rel num_tmp + 3], dl
    ; Return as packed dword
    movzx   eax, byte [rel num_tmp]
    movzx   edx, byte [rel num_tmp + 1]
    shl     edx, 8
    or      eax, edx
    movzx   edx, byte [rel num_tmp + 2]
    shl     edx, 16
    or      eax, edx
    movzx   edx, byte [rel num_tmp + 3]
    shl     edx, 24
    or      eax, edx
    pop     rcx
    pop     rbx
    ret

; ============================================================
; format_day_sp(eax=day 1-31) -> rsi points to 2-char (space-padded)
; ============================================================
format_day_sp:
    movzx   eax, al
    cmp     eax, 10
    jge     .two_digits
    mov     byte [rel num_tmp + 10], ' '
    add     al, '0'
    mov     [rel num_tmp + 11], al
    lea     rsi, [rel num_tmp + 10]
    ret
.two_digits:
    push    rbx
    xor     edx, edx
    mov     ebx, 10
    div     ebx
    add     al, '0'
    add     dl, '0'
    mov     [rel num_tmp + 10], al
    mov     [rel num_tmp + 11], dl
    lea     rsi, [rel num_tmp + 10]
    pop     rbx
    ret

; ============================================================
; is_leap(rdi=year) -> edx: 1 if leap, 0 if not
; ============================================================
is_leap:
    mov     rax, rdi
    xor     edx, edx
    push    rbx
    mov     rbx, 4
    div     rbx
    test    edx, edx
    jnz     .not_leap
    mov     rax, rdi
    xor     edx, edx
    mov     rbx, 100
    div     rbx
    test    edx, edx
    jnz     .yes_leap
    mov     rax, rdi
    xor     edx, edx
    mov     rbx, 400
    div     rbx
    test    edx, edx
    jnz     .not_leap
.yes_leap:
    mov     edx, 1
    pop     rbx
    ret
.not_leap:
    xor     edx, edx
    pop     rbx
    ret

; ============================================================
; epoch_to_datetime — convert Unix epoch (rdi) to datetime components
; Stores results in dt_* globals
; ============================================================
epoch_to_datetime:
    push    rbx
    push    rbp
    push    r12
    push    r13
    push    r14
    push    r15

    mov     [rel dt_epoch], rdi
    mov     rax, rdi            ; epoch seconds

    ; Compute seconds within day
    xor     edx, edx
    mov     rbx, 86400
    div     rbx                 ; rax = days since epoch, rdx = seconds in day
    mov     r8, rax             ; r8 = total days
    mov     r9, rdx             ; r9 = seconds in day

    ; Hour/Min/Sec from seconds in day
    mov     rax, r9
    xor     edx, edx
    mov     rbx, 3600
    div     rbx
    mov     [rel dt_hour], al
    mov     rax, rdx
    xor     edx, edx
    mov     rbx, 60
    div     rbx
    mov     [rel dt_min], al
    mov     [rel dt_sec], dl

    ; Day of week: (days + 4) % 7  (Jan 1 1970 = Thursday = 4)
    mov     rax, r8
    add     rax, 4
    xor     edx, edx
    mov     rbx, 7
    div     rbx
    mov     [rel dt_wday], dl

    ; Convert days to year/month/day
    mov     rax, r8             ; remaining days
    mov     r10, 1970           ; current year

.year_loop:
    mov     rbx, 365
    push    rax
    mov     rdi, r10
    call    is_leap
    pop     rax
    add     rbx, rdx            ; 365 or 366

    cmp     rax, rbx
    jl      .year_found
    sub     rax, rbx
    inc     r10
    jmp     .year_loop

.year_found:
    mov     [rel dt_year], r10w
    inc     rax                 ; 1-based yday
    mov     [rel dt_yday], ax
    dec     rax                 ; back to 0-based for month calc

    push    rax
    mov     rdi, r10
    call    is_leap
    pop     rax
    mov     r11, rdx            ; leap flag

    lea     rsi, [rel days_per_month]
    xor     ecx, ecx
.month_loop:
    movzx   ebx, byte [rsi + rcx]
    cmp     ecx, 1
    jne     .no_feb_adj
    add     ebx, r11d
.no_feb_adj:
    cmp     eax, ebx
    jl      .month_found
    sub     eax, ebx
    inc     ecx
    cmp     ecx, 11
    jle     .month_loop

.month_found:
    inc     ecx                 ; 1-based month
    mov     [rel dt_month], cl
    inc     eax                 ; 1-based day
    mov     [rel dt_day], al

    pop     r15
    pop     r14
    pop     r13
    pop     r12
    pop     rbp
    pop     rbx
    ret

; ============================================================
; emit_4digit_year: writes 4 chars at [rdi+rcx], advances rcx by 4
; ============================================================
emit_year:
    movzx   eax, word [rel dt_year]
    call    format_4digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    shr     eax, 16
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    ret

; ============================================================
; emit_2digit: writes 2 chars for byte in al at [rdi+rcx]
; ============================================================
emit_hms:
    ; Emit HH:MM:SS at [rdi+rcx]
    movzx   eax, byte [rel dt_hour]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    mov     byte [rdi + rcx], ':'
    inc     rcx
    movzx   eax, byte [rel dt_min]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    mov     byte [rdi + rcx], ':'
    inc     rcx
    movzx   eax, byte [rel dt_sec]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    ret

; ============================================================
; _start — entry point
; ============================================================
_start:
    mov     r14, [rsp]          ; argc
    lea     r15, [rsp + 8]      ; argv

    ; Flags
    xor     r12d, r12d          ; format: 0=default, 1=custom, 2=RFC, 3=ISO
    xor     r13d, r13d          ; format string pointer

    mov     rbx, 1
.parse_args:
    cmp     rbx, r14
    jge     .args_done

    mov     rdi, [r15 + rbx*8]

    ; --help
    push    rbx
    mov     rsi, str_opt_help
    call    asm_strcmp
    pop     rbx
    test    eax, eax
    jz      .show_help

    mov     rdi, [r15 + rbx*8]
    push    rbx
    mov     rsi, str_opt_version
    call    asm_strcmp
    pop     rbx
    test    eax, eax
    jz      .show_version

    mov     rdi, [r15 + rbx*8]

    ; +FORMAT
    cmp     byte [rdi], '+'
    jne     .check_dash
    lea     r13, [rdi + 1]
    mov     r12d, 1
    inc     rbx
    jmp     .parse_args

.check_dash:
    cmp     byte [rdi], '-'
    jne     .unknown_arg

    movzx   eax, byte [rdi + 1]
    cmp     al, 'u'
    je      .opt_u
    cmp     al, 'R'
    je      .opt_R
    cmp     al, 'I'
    je      .opt_I
    cmp     al, 'd'
    je      .opt_d
    cmp     al, '-'
    je      .check_long
    jmp     .unknown_arg

.opt_u:
    cmp     byte [rdi + 2], 0
    jne     .check_long
    inc     rbx
    jmp     .parse_args

.opt_R:
    cmp     byte [rdi + 2], 0
    jne     .unknown_arg
    mov     r12d, 2
    inc     rbx
    jmp     .parse_args

.opt_I:
    mov     r12d, 3
    inc     rbx
    jmp     .parse_args

.opt_d:
    cmp     byte [rdi + 2], 0
    jne     .unknown_arg
    ; Skip the -d and its argument
    add     rbx, 2
    jmp     .parse_args

.check_long:
    ; --utc, --universal
    mov     rdi, [r15 + rbx*8]
    push    rbx
    mov     rsi, str_opt_utc
    call    asm_strcmp
    pop     rbx
    test    eax, eax
    jz      .opt_u_long

    mov     rdi, [r15 + rbx*8]
    push    rbx
    mov     rsi, str_opt_universal
    call    asm_strcmp
    pop     rbx
    test    eax, eax
    jz      .opt_u_long

    mov     rdi, [r15 + rbx*8]
    push    rbx
    mov     rsi, str_opt_rfc_email
    call    asm_strcmp
    pop     rbx
    test    eax, eax
    jz      .opt_R_long

    jmp     .unknown_arg

.opt_u_long:
    inc     rbx
    jmp     .parse_args

.opt_R_long:
    mov     r12d, 2
    inc     rbx
    jmp     .parse_args

.unknown_arg:
    mov     edi, STDERR
    mov     rsi, str_err_prefix
    mov     edx, str_err_prefix_len
    call    asm_write
    mov     rdi, [r15 + rbx*8]
    call    asm_strlen
    mov     rdx, rax
    mov     rsi, [r15 + rbx*8]
    mov     edi, STDERR
    call    asm_write
    mov     edi, STDERR
    mov     rsi, str_err_post
    mov     edx, str_err_post_len
    call    asm_write
    mov     edi, STDERR
    mov     rsi, str_try
    mov     edx, str_try_len
    call    asm_write
    mov     edi, 1
    call    asm_exit

.args_done:
    ; Save format info
    mov     [rel dt_format], r12b
    mov     [rel dt_fmt_ptr], r13

    ; Get current time
    sub     rsp, 16
    mov     eax, SYS_CLOCK_GETTIME
    xor     edi, edi            ; CLOCK_REALTIME
    mov     rsi, rsp
    syscall
    test    rax, rax
    jnz     .clock_failed

    mov     rdi, [rsp]          ; epoch seconds
    add     rsp, 16

    ; Convert to broken-down time
    call    epoch_to_datetime

    ; Format based on mode
    movzx   eax, byte [rel dt_format]
    cmp     al, 2
    je      .format_rfc5322
    cmp     al, 3
    je      .format_iso8601
    cmp     al, 1
    je      .format_custom

    ; ── Default format: "Thu Mar 12 20:00:00 UTC 2026\n" ──
    lea     rdi, [rel out_buf]
    xor     ecx, ecx

    ; Day name
    movzx   eax, byte [rel dt_wday]
    imul    eax, 3
    lea     rsi, [rel day_names]
    add     rsi, rax
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 2]
    mov     [rdi + rcx], al
    inc     rcx
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; Month name
    movzx   eax, byte [rel dt_month]
    dec     eax
    imul    eax, 3
    lea     rsi, [rel mon_names]
    add     rsi, rax
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 2]
    mov     [rdi + rcx], al
    inc     rcx
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; Day (space-padded)
    push    rcx
    push    rdi
    movzx   eax, byte [rel dt_day]
    call    format_day_sp
    pop     rdi
    pop     rcx
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; HH:MM:SS
    call    emit_hms
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; "UTC "
    mov     byte [rdi + rcx], 'U'
    inc     rcx
    mov     byte [rdi + rcx], 'T'
    inc     rcx
    mov     byte [rdi + rcx], 'C'
    inc     rcx
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; Year
    call    emit_year

    ; Newline
    mov     byte [rdi + rcx], 10
    inc     rcx

    mov     rdx, rcx
    lea     rsi, [rel out_buf]
    mov     edi, STDOUT
    call    asm_write
    xor     edi, edi
    call    asm_exit

.format_rfc5322:
    ; "Thu, 12 Mar 2026 20:00:00 +0000\n"
    lea     rdi, [rel out_buf]
    xor     ecx, ecx

    ; Day name + ", "
    movzx   eax, byte [rel dt_wday]
    imul    eax, 5
    lea     rsi, [rel rfc_day_names]
    add     rsi, rax
    ; Copy 5 bytes "Thu, "
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 2]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 3]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 4]
    mov     [rdi + rcx], al
    inc     rcx

    ; Day (zero-padded)
    movzx   eax, byte [rel dt_day]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; Month name
    movzx   eax, byte [rel dt_month]
    dec     eax
    imul    eax, 3
    lea     rsi, [rel mon_names]
    add     rsi, rax
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 2]
    mov     [rdi + rcx], al
    inc     rcx
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; Year
    call    emit_year
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; HH:MM:SS
    call    emit_hms
    mov     byte [rdi + rcx], ' '
    inc     rcx

    ; "+0000\n"
    mov     byte [rdi + rcx], '+'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    mov     byte [rdi + rcx], 10
    inc     rcx

    mov     rdx, rcx
    lea     rsi, [rel out_buf]
    mov     edi, STDOUT
    call    asm_write
    xor     edi, edi
    call    asm_exit

.format_iso8601:
    ; "2026-03-12\n"
    lea     rdi, [rel out_buf]
    xor     ecx, ecx

    call    emit_year
    mov     byte [rdi + rcx], '-'
    inc     rcx
    movzx   eax, byte [rel dt_month]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    mov     byte [rdi + rcx], '-'
    inc     rcx
    movzx   eax, byte [rel dt_day]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    mov     byte [rdi + rcx], 10
    inc     rcx

    mov     rdx, rcx
    lea     rsi, [rel out_buf]
    mov     edi, STDOUT
    call    asm_write
    xor     edi, edi
    call    asm_exit

.format_custom:
    lea     rdi, [rel out_buf]
    xor     ecx, ecx
    mov     rbp, [rel dt_fmt_ptr]   ; format string

.fmt_loop:
    movzx   eax, byte [rbp]
    test    al, al
    jz      .fmt_done

    cmp     al, '%'
    jne     .fmt_literal
    inc     rbp
    movzx   eax, byte [rbp]
    test    al, al
    jz      .fmt_done

    cmp     al, 'Y'
    je      .fmt_Y
    cmp     al, 'm'
    je      .fmt_m
    cmp     al, 'd'
    je      .fmt_d
    cmp     al, 'H'
    je      .fmt_H
    cmp     al, 'M'
    je      .fmt_M
    cmp     al, 'S'
    je      .fmt_S
    cmp     al, 'a'
    je      .fmt_a
    cmp     al, 'A'
    je      .fmt_a
    cmp     al, 'b'
    je      .fmt_b
    cmp     al, 'B'
    je      .fmt_b
    cmp     al, 'e'
    je      .fmt_e
    cmp     al, 'Z'
    je      .fmt_Z
    cmp     al, 'n'
    je      .fmt_n
    cmp     al, 't'
    je      .fmt_t
    cmp     al, '%'
    je      .fmt_percent
    cmp     al, 'F'
    je      .fmt_F
    cmp     al, 'T'
    je      .fmt_T
    cmp     al, 'R'
    je      .fmt_R_spec
    cmp     al, 'u'
    je      .fmt_u
    cmp     al, 'w'
    je      .fmt_w
    cmp     al, 'j'
    je      .fmt_j
    cmp     al, 'p'
    je      .fmt_p
    cmp     al, 'I'
    je      .fmt_I
    cmp     al, 'z'
    je      .fmt_z
    cmp     al, 's'
    je      .fmt_s

    ; Unknown — output %X literally
    mov     byte [rdi + rcx], '%'
    inc     rcx
    mov     [rdi + rcx], al
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_literal:
    mov     [rdi + rcx], al
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_Y:
    call    emit_year
    inc     rbp
    jmp     .fmt_loop

.fmt_m:
    movzx   eax, byte [rel dt_month]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_d:
    movzx   eax, byte [rel dt_day]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_H:
    movzx   eax, byte [rel dt_hour]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_M:
    movzx   eax, byte [rel dt_min]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_S:
    movzx   eax, byte [rel dt_sec]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_a:
    movzx   eax, byte [rel dt_wday]
    imul    eax, 3
    lea     rsi, [rel day_names]
    add     rsi, rax
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 2]
    mov     [rdi + rcx], al
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_b:
    movzx   eax, byte [rel dt_month]
    dec     eax
    imul    eax, 3
    lea     rsi, [rel mon_names]
    add     rsi, rax
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 2]
    mov     [rdi + rcx], al
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_e:
    push    rcx
    push    rdi
    movzx   eax, byte [rel dt_day]
    call    format_day_sp
    pop     rdi
    pop     rcx
    mov     al, [rsi]
    mov     [rdi + rcx], al
    inc     rcx
    mov     al, [rsi + 1]
    mov     [rdi + rcx], al
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_Z:
    mov     byte [rdi + rcx], 'U'
    inc     rcx
    mov     byte [rdi + rcx], 'T'
    inc     rcx
    mov     byte [rdi + rcx], 'C'
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_n:
    mov     byte [rdi + rcx], 10
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_t:
    mov     byte [rdi + rcx], 9
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_percent:
    mov     byte [rdi + rcx], '%'
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_F:
    call    emit_year
    mov     byte [rdi + rcx], '-'
    inc     rcx
    movzx   eax, byte [rel dt_month]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    mov     byte [rdi + rcx], '-'
    inc     rcx
    movzx   eax, byte [rel dt_day]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_T:
    call    emit_hms
    inc     rbp
    jmp     .fmt_loop

.fmt_R_spec:
    movzx   eax, byte [rel dt_hour]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    mov     byte [rdi + rcx], ':'
    inc     rcx
    movzx   eax, byte [rel dt_min]
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_u:
    movzx   eax, byte [rel dt_wday]
    test    eax, eax
    jnz     .fmt_u_ok
    mov     eax, 7
.fmt_u_ok:
    add     al, '0'
    mov     [rdi + rcx], al
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_w:
    movzx   eax, byte [rel dt_wday]
    add     al, '0'
    mov     [rdi + rcx], al
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_j:
    movzx   eax, word [rel dt_yday]
    ; 3-digit zero-padded
    push    rbx
    xor     edx, edx
    mov     ebx, 100
    div     ebx
    add     al, '0'
    mov     [rdi + rcx], al
    inc     rcx
    mov     eax, edx
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    pop     rbx
    inc     rbp
    jmp     .fmt_loop

.fmt_p:
    movzx   eax, byte [rel dt_hour]
    cmp     eax, 12
    jge     .fmt_pm
    mov     byte [rdi + rcx], 'A'
    inc     rcx
    mov     byte [rdi + rcx], 'M'
    inc     rcx
    inc     rbp
    jmp     .fmt_loop
.fmt_pm:
    mov     byte [rdi + rcx], 'P'
    inc     rcx
    mov     byte [rdi + rcx], 'M'
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_I:
    movzx   eax, byte [rel dt_hour]
    test    eax, eax
    jnz     .fmt_I_nz
    mov     eax, 12
    jmp     .fmt_I_out
.fmt_I_nz:
    cmp     eax, 12
    jle     .fmt_I_out
    sub     eax, 12
.fmt_I_out:
    call    format_2digit
    mov     [rdi + rcx], al
    inc     rcx
    mov     [rdi + rcx], ah
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_z:
    mov     byte [rdi + rcx], '+'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    mov     byte [rdi + rcx], '0'
    inc     rcx
    inc     rbp
    jmp     .fmt_loop

.fmt_s:
    mov     rax, [rel dt_epoch]
    lea     rsi, [rel num_tmp + 20]
    mov     byte [rsi], 0
    push    rbx
    mov     rbx, 10
.fmt_s_loop:
    xor     edx, edx
    div     rbx
    add     dl, '0'
    dec     rsi
    mov     [rsi], dl
    test    rax, rax
    jnz     .fmt_s_loop
    pop     rbx
.fmt_s_copy:
    movzx   eax, byte [rsi]
    test    al, al
    jz      .fmt_s_done
    mov     [rdi + rcx], al
    inc     rcx
    inc     rsi
    jmp     .fmt_s_copy
.fmt_s_done:
    inc     rbp
    jmp     .fmt_loop

.fmt_done:
    mov     byte [rdi + rcx], 10
    inc     rcx
    mov     rdx, rcx
    lea     rsi, [rel out_buf]
    mov     edi, STDOUT
    call    asm_write
    xor     edi, edi
    call    asm_exit

.clock_failed:
    mov     edi, STDERR
    mov     rsi, str_clock_err
    mov     edx, str_clock_err_len
    call    asm_write
    mov     edi, 1
    call    asm_exit

.show_help:
    mov     edi, STDOUT
    mov     rsi, str_help
    mov     edx, str_help_len
    call    asm_write
    xor     edi, edi
    call    asm_exit

.show_version:
    mov     edi, STDOUT
    mov     rsi, str_version
    mov     edx, str_version_len
    call    asm_write
    xor     edi, edi
    call    asm_exit


section .rodata

str_opt_help:
    db "--help", 0

str_opt_version:
    db "--version", 0

str_opt_utc:
    db "--utc", 0

str_opt_universal:
    db "--universal", 0

str_opt_rfc_email:
    db "--rfc-email", 0

str_help:
    db "Usage: date [OPTION]... [+FORMAT]", 10
    db "  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]", 10
    db "Display date and time in the given FORMAT.", 10
    db 10
    db "  -d, --date=STRING          display time described by STRING", 10
    db "  -I[FMT], --iso-8601[=FMT]  output date/time in ISO 8601 format.", 10
    db "  -R, --rfc-email            output date and time in RFC 5322 format.", 10
    db "  -u, --utc, --universal     print Coordinated Universal Time (UTC)", 10
    db "      --help        display this help and exit", 10
    db "      --version     output version information and exit", 10
    db 10
    db "GNU coreutils online help: <https://www.gnu.org/software/coreutils/>", 10
    db "Full documentation <https://www.gnu.org/software/coreutils/date>", 10
    db "or available locally via: info '(coreutils) date invocation'", 10
str_help_len equ $ - str_help

str_version:
    db "date (GNU coreutils) 9.7", 10
    db "Packaged by Debian (9.7-3)", 10
    db "Copyright (C) 2025 Free Software Foundation, Inc.", 10
    db "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.", 10
    db "This is free software: you are free to change and redistribute it.", 10
    db "There is NO WARRANTY, to the extent permitted by law.", 10
    db 10
    db "Written by David MacKenzie.", 10
str_version_len equ $ - str_version

str_try:
    db "Try 'date --help' for more information.", 10
str_try_len equ $ - str_try

str_err_prefix:
    db "date: unrecognized option '", 0
str_err_prefix_len equ $ - str_err_prefix - 1

str_err_post:
    db "'", 10, 0
str_err_post_len equ $ - str_err_post - 1

str_clock_err:
    db "date: cannot get time", 10
str_clock_err_len equ $ - str_clock_err

; Non-executable stack
section .note.GNU-stack noalloc noexec nowrite progbits