macros.hex2
← Le code · src/amorce/macros.hex2 · 815 lignes
# macros.hex2 : image de l'étage 3b, l'assembleur à macros (façon M1 de stage0), écrite en hex2 (Mach-O, macOS ARM64).
# Construction : adresses.macos-arm64 < macros.hex2 | signeur.macos-arm64 > macros.macos-arm64.
# Langage lu : hex2 lu par jetons séparés par des blancs, plus les macros. '=nom valeur' définit une macro (nom de 1 à 48
# caractères non blancs commençant par une lettre et pas seulement hexadécimal ; valeur hexadécimale de 1 à 12 octets).
# Un jeton nu fait seulement de chiffres hexadécimaux donne ses octets par paires ; tout autre jeton nu est remplacé par la
# valeur de la macro du même nom, définie plus haut. '#' et ';' n'ouvrent un commentaire qu'en début de jeton : un nom de
# macro peut donc s'écrire comme otool imprime l'instruction (ldr_w2,[x23,x0,lsl_#2]).
# Codes de sortie : 0 fini, 1 lecture ou écriture, 2 nombre impair de chiffres, 3 nom défini deux fois, 4 nom inconnu,
# 5 champ invalide, 6 plein (sortie, renvois, noms, 1 024 macros), 7 mal formé.
# ---------------------------------------------------------------------------
# En-tête Mach-O (32 octets)
:entete
cf fa ed fe # magic MH_MAGIC_64
0c 00 00 01 # cputype ARM64
00 00 00 00 # cpusubtype ALL
02 00 00 00 # filetype MH_EXECUTE
0b 00 00 00 # ncmds = 11
&fin_commandes>commandes # sizeofcmds
85 00 20 00 # flags NOUNDEFS DYLDLINK TWOLEVEL PIE
00 00 00 00 # reserved
:commandes
# LC_SEGMENT_64 __PAGEZERO : 4 Gio sans accès, attrape les pointeurs nuls
:pagezero
19 00 00 00 &fin_pagezero>pagezero # cmd, cmdsize
5f 5f 50 41 47 45 5a 45 52 4f 00 00 00 00 00 00 # "__PAGEZERO"
00 00 00 00 00 00 00 00 # vmaddr 0
00 00 00 00 01 00 00 00 # vmsize 0x100000000
00 00 00 00 00 00 00 00 # fileoff 0
00 00 00 00 00 00 00 00 # filesize 0
00 00 00 00 00 00 00 00 # maxprot 0, initprot 0
00 00 00 00 00 00 00 00 # nsects 0, flags 0
:fin_pagezero
# LC_SEGMENT_64 __TEXT (une section) : en-tête et code, lecture + exécution
:texte
19 00 00 00 &fin_texte>texte # cmd, cmdsize
5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 # "__TEXT"
&entete 01 00 00 00 # vmaddr 0x100000000
&linkedit 00 00 00 00 # vmsize : jusqu'à __LINKEDIT
&entete 00 00 00 00 # fileoff 0
&linkedit 00 00 00 00 # filesize : jusqu'à __LINKEDIT
05 00 00 00 05 00 00 00 # maxprot r-x, initprot r-x
01 00 00 00 00 00 00 00 # nsects 1, flags 0
# section __text : là où vit le code
5f 5f 74 65 78 74 00 00 00 00 00 00 00 00 00 00 # "__text"
5f 5f 54 45 58 54 00 00 00 00 00 00 00 00 00 00 # "__TEXT"
&code 01 00 00 00 # addr 0x100000000 + position du code
&fin_code>code 00 00 00 00 # size
&code 04 00 00 00 # offset, align 2^4
00 00 00 00 00 00 00 00 # reloff 0, nreloc 0
00 04 00 80 00 00 00 00 # flags PURE_INSTRUCTIONS SOME_INSTRUCTIONS, reserved1
00 00 00 00 00 00 00 00 # reserved2, reserved3
:fin_texte
# LC_SEGMENT_64 __LINKEDIT : données de dyld et signature, lecture seule
:segment_linkedit
19 00 00 00 &fin_segment_linkedit>segment_linkedit # cmd, cmdsize
5f 5f 4c 49 4e 4b 45 44 49 54 00 00 00 00 00 00 # "__LINKEDIT"
&linkedit 01 00 00 00 # vmaddr 0x100000000 + position de __LINKEDIT
00 40 00 00 00 00 00 00 # vmsize 0x4000 (une page)
&linkedit 00 00 00 00 # fileoff (aligné sur une page, sinon le noyau tue le processus)
&fin_fichier>linkedit 00 00 00 00 # filesize
01 00 00 00 01 00 00 00 # maxprot r--, initprot r--
00 00 00 00 00 00 00 00 # nsects 0, flags 0
:fin_segment_linkedit
# LC_DYLD_CHAINED_FIXUPS : correctifs vides, rien à lier
:commande_correctifs
34 00 00 80 &fin_commande_correctifs>commande_correctifs # cmd, cmdsize
&linkedit &fin_correctifs>linkedit # dataoff, datasize
:fin_commande_correctifs
# LC_SYMTAB : aucun symbole
:symtab
02 00 00 00 &fin_symtab>symtab # cmd, cmdsize
00 00 00 00 00 00 00 00 # symoff 0, nsyms 0
&fin_correctifs 00 00 00 00 # stroff, strsize 0
:fin_symtab
# LC_DYSYMTAB : tout à zéro
:dysymtab
0b 00 00 00 &fin_dysymtab>dysymtab # cmd, cmdsize
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
:fin_dysymtab
# LC_LOAD_DYLINKER : le chargeur est /usr/lib/dyld
:dylinker
0e 00 00 00 &fin_dylinker>dylinker # cmd, cmdsize
&nom_dylinker>dylinker # position du nom
:nom_dylinker
2f 75 73 72 2f 6c 69 62 2f 64 79 6c 64 00 # "/usr/lib/dyld"
!8 # remplissage
:fin_dylinker
# LC_MAIN : point d'entrée = début du code
:main
28 00 00 80 &fin_main>main # cmd, cmdsize
&code 00 00 00 00 # entryoff
00 00 00 00 00 00 00 00 # stacksize 0 (par défaut)
:fin_main
# LC_LOAD_DYLIB : libSystem, exigée par Apple, rien n'y est importé
:dylib
0c 00 00 00 &fin_dylib>dylib # cmd, cmdsize
&nom_dylib>dylib 02 00 00 00 # position du nom, timestamp 2
03 64 27 05 00 00 01 00 # current_version 1319.100.3, compatibility_version 1.0.0
:nom_dylib
2f 75 73 72 2f 6c 69 62 2f 6c 69 62 53 79 73 74 65 6d 2e 42 2e 64 79 6c 69 62 00 # "/usr/lib/libSystem.B.dylib"
!8 # remplissage
:fin_dylib
# LC_BUILD_VERSION : macOS 11.0
:version
32 00 00 00 &fin_version>version # cmd, cmdsize
01 00 00 00 # platform MACOS
00 00 0b 00 00 00 0b 00 # minos 11.0, sdk 11.0
00 00 00 00 # ntools 0
:fin_version
# LC_CODE_SIGNATURE : où vit la signature
:commande_signature
1d 00 00 00 &fin_commande_signature>commande_signature # cmd, cmdsize
&signature &fin_fichier>signature # dataoff, datasize
:fin_commande_signature
:fin_commandes
!10 # remplissage jusqu'au code (aligné sur 16)
:code
# Code (ARM64). Les sauts sont écrits avec un champ à zéro suivi de @nom : l'assembleur calcule chaque distance.
# Registres : x19 sortie, x20 longueur de sortie, x21 octet courant, x22 table des noms (32 octets par nom),
# x23 positions (4 octets par nom, position + 1, 0 = pas encore définie), x24 renvois (16 octets chacun), x25 nombre de renvois,
# x26 nombre de noms, x17 nombre de macros (table de 64 octets par macro en x24 + 0x20000), x29 zone de lecture
# (case de lecture en +0, nom courant en +8, jeton courant en +0x30),
# zones : macros en x24 + 0x20000, lecture en x24 + 0x30000 (4 Kio) ; x10 = 1 si un séparateur est exigé, x11 = 1 en fin d'entrée, x12 = 1 si l'octet courant attend d'être relu,
# x13 index du nom, x14 = 1 pour '%', x15 index du second nom de '&a>b' (0xffff s'il n'y en a pas), x9 retour de lire_nom.
# Renvoi (16 octets) : position (4), index du nom (2), index du second nom (2), sorte (1 : imm26, 2 : imm19, 3 : adr, 4 : position).
# début : zones de pile (sortie 1 Mio, noms 32 Kio, positions 4 Kio, renvois 128 Kio, macros 64 Kio, lecture), registres, positions à zéro
ff eb 44 d1 # sub sp, sp, #0x13a, lsl #12
f3 03 00 91 # mov x19, sp
76 02 44 91 # add x22, x19, #0x100, lsl #12
d7 22 40 91 # add x23, x22, #0x8, lsl #12
f8 06 40 91 # add x24, x23, #0x1, lsl #12
1d c3 40 91 # add x29, x24, #0x30, lsl #12
14 00 80 d2 # mov x20, #0
19 00 80 d2 # mov x25, #0
1a 00 80 d2 # mov x26, #0
11 00 80 d2 # mov x17, #0
0a 00 80 d2 # mov x10, #0
0b 00 80 d2 # mov x11, #0
0c 00 80 d2 # mov x12, #0
00 00 80 d2 # mov x0, #0
:mise_a_zero
ff 7a 20 b8 # str wzr, [x23, x0, lsl #2]
00 04 00 91 # add x0, x0, #1
1f 00 10 f1 # cmp x0, #0x400
01 00 00 54 @mise_a_zero # b.ne mise_a_zero
# boucle : lire un octet entre deux jetons et le trier
:boucle
00 00 00 94 @octet_suivant # bl octet_suivant
0b 00 00 b5 @fin # cbnz x11, fin
bf 82 00 71 # cmp w21, #0x20 (' ')
00 00 00 54 @blanc # b.eq blanc
bf 2a 00 71 # cmp w21, #0xa
00 00 00 54 @blanc # b.eq blanc
bf 26 00 71 # cmp w21, #0x9
00 00 00 54 @blanc # b.eq blanc
bf 36 00 71 # cmp w21, #0xd
00 00 00 54 @blanc # b.eq blanc
0a 00 00 b5 @mal_forme # cbnz x10, mal_forme
bf 8e 00 71 # cmp w21, #0x23 ('#')
00 00 00 54 @commentaire # b.eq commentaire
bf ee 00 71 # cmp w21, #0x3b (';')
00 00 00 54 @commentaire # b.eq commentaire
bf ea 00 71 # cmp w21, #0x3a (':')
00 00 00 54 @definir # b.eq definir
bf 02 01 71 # cmp w21, #0x40 ('@')
00 00 00 54 @renvoi_branche # b.eq renvoi_branche
bf 96 00 71 # cmp w21, #0x25 ('%')
00 00 00 54 @renvoi_adresse # b.eq renvoi_adresse
bf 9a 00 71 # cmp w21, #0x26 ('&')
00 00 00 54 @position # b.eq position
bf 86 00 71 # cmp w21, #0x21 ('!')
00 00 00 54 @aligner # b.eq aligner
bf f6 00 71 # cmp w21, #0x3d ('=')
00 00 00 54 @definir_macro # b.eq definir_macro
00 00 00 14 @jeton # b jeton
# blanc : un séparateur entre deux jetons
:blanc
0a 00 80 d2 # mov x10, #0
00 00 00 14 @boucle # b boucle
# commentaire : seulement en début de jeton, jusqu'à la fin de la ligne
:commentaire
:boucle_commentaire
00 00 00 94 @octet_suivant # bl octet_suivant
0b 00 00 b5 @fin # cbnz x11, fin
bf 2a 00 71 # cmp w21, #0xa
01 00 00 54 @boucle_commentaire # b.ne boucle_commentaire
0a 00 80 d2 # mov x10, #0
00 00 00 14 @boucle # b boucle
# jeton : lu en entier jusqu'au blanc ; fait de chiffres hexadécimaux, ce sont des octets (par paires) ; sinon, une macro
:jeton
00 00 00 94 @lire_jeton # bl lire_jeton
06 00 80 d2 # mov x6, #0
:jeton_verifier
df 00 07 eb # cmp x6, x7
02 00 00 54 @jeton_hex # b.hs jeton_hex
a0 c3 00 91 # add x0, x29, #0x30
00 68 66 38 # ldrb w0, [x0, x6]
00 00 00 94 @valeur_hex # bl valeur_hex
1f 40 00 71 # cmp w0, #0x10
02 00 00 54 @macro_emploi # b.hs macro_emploi
c6 04 00 91 # add x6, x6, #0x1
00 00 00 14 @jeton_verifier # b jeton_verifier
:jeton_hex
ff 00 40 f2 # tst x7, #0x1
01 00 00 54 @impair # b.ne impair
06 00 80 d2 # mov x6, #0
:jeton_ecrire
df 00 07 eb # cmp x6, x7
02 00 00 54 @jeton_fini # b.hs jeton_fini
a0 c3 00 91 # add x0, x29, #0x30
00 68 66 38 # ldrb w0, [x0, x6]
00 00 00 94 @valeur_hex # bl valeur_hex
e1 03 00 2a # mov w1, w0
c6 04 00 91 # add x6, x6, #0x1
a0 c3 00 91 # add x0, x29, #0x30
00 68 66 38 # ldrb w0, [x0, x6]
00 00 00 94 @valeur_hex # bl valeur_hex
00 10 01 0b # add w0, w0, w1, lsl #4
c6 04 00 91 # add x6, x6, #0x1
01 02 a0 d2 # mov x1, #0x100000
9f 02 01 eb # cmp x20, x1
02 00 00 54 @plein # b.hs plein
60 6a 34 38 # strb w0, [x19, x20]
94 06 00 91 # add x20, x20, #0x1
00 00 00 14 @jeton_ecrire # b jeton_ecrire
:jeton_fini
0a 00 80 d2 # mov x10, #0
00 00 00 14 @boucle # b boucle
# macro_emploi : remplacer le jeton par la valeur de la macro du même nom (définie plus haut)
:macro_emploi
ff c0 00 f1 # cmp x7, #0x30
08 00 00 54 @mal_forme # b.hi mal_forme
00 00 00 94 @chercher_macro # bl chercher_macro
05 00 00 b4 @inconnue # cbz x5, inconnue
65 c0 40 39 # ldrb w5, [x3, #0x30]
06 00 80 d2 # mov x6, #0
:macro_ecrire
df 00 05 eb # cmp x6, x5
02 00 00 54 @jeton_fini # b.hs jeton_fini
67 c4 00 91 # add x7, x3, #0x31
e0 68 66 38 # ldrb w0, [x7, x6]
01 02 a0 d2 # mov x1, #0x100000
9f 02 01 eb # cmp x20, x1
02 00 00 54 @plein # b.hs plein
60 6a 34 38 # strb w0, [x19, x20]
94 06 00 91 # add x20, x20, #0x1
c6 04 00 91 # add x6, x6, #0x1
00 00 00 14 @macro_ecrire # b macro_ecrire
# definir_macro : '=nom valeur' ; le nom commence par une lettre et n'est pas fait que de chiffres hexadécimaux,
# la valeur est un jeton hexadécimal de 1 à 12 octets. Entrée de la table (64 octets) : nom (48), longueur (1), valeur (12).
:definir_macro
00 00 00 94 @octet_suivant # bl octet_suivant
0b 00 00 b5 @mal_forme # cbnz x11, mal_forme
a0 02 1b 32 # orr w0, w21, #0x20
00 84 01 51 # sub w0, w0, #0x61
1f 64 00 71 # cmp w0, #0x19
08 00 00 54 @mal_forme # b.hi mal_forme
00 00 00 94 @lire_jeton # bl lire_jeton
ff c0 00 f1 # cmp x7, #0x30
08 00 00 54 @mal_forme # b.hi mal_forme
06 00 80 d2 # mov x6, #0
:macro_nom_verifier
df 00 07 eb # cmp x6, x7
02 00 00 54 @mal_forme # b.hs mal_forme
a0 c3 00 91 # add x0, x29, #0x30
00 68 66 38 # ldrb w0, [x0, x6]
00 00 00 94 @valeur_hex # bl valeur_hex
1f 40 00 71 # cmp w0, #0x10
02 00 00 54 @macro_nom_valide # b.hs macro_nom_valide
c6 04 00 91 # add x6, x6, #0x1
00 00 00 14 @macro_nom_verifier # b macro_nom_verifier
:macro_nom_valide
00 00 00 94 @chercher_macro # bl chercher_macro
05 00 00 b5 @doublon # cbnz x5, doublon
3f 02 10 f1 # cmp x17, #0x400
02 00 00 54 @plein # b.hs plein
e8 03 03 aa # mov x8, x3
00 05 00 a9 # stp x0, x1, [x8]
02 35 01 a9 # stp x2, x13, [x8, #0x10]
0e 3d 02 a9 # stp x14, x15, [x8, #0x20]
:macro_blanc
00 00 00 94 @octet_suivant # bl octet_suivant
0b 00 00 b5 @mal_forme # cbnz x11, mal_forme
bf 82 00 71 # cmp w21, #0x20
00 00 00 54 @macro_blanc # b.eq macro_blanc
bf 26 00 71 # cmp w21, #0x9
00 00 00 54 @macro_blanc # b.eq macro_blanc
bf 2a 00 71 # cmp w21, #0xa
00 00 00 54 @macro_blanc # b.eq macro_blanc
bf 36 00 71 # cmp w21, #0xd
00 00 00 54 @macro_blanc # b.eq macro_blanc
00 00 00 94 @lire_jeton # bl lire_jeton
ff 60 00 f1 # cmp x7, #0x18
08 00 00 54 @mal_forme # b.hi mal_forme
ff 00 40 f2 # tst x7, #0x1
01 00 00 54 @impair # b.ne impair
06 00 80 d2 # mov x6, #0
:macro_valeur
df 00 07 eb # cmp x6, x7
02 00 00 54 @macro_definie # b.hs macro_definie
a0 c3 00 91 # add x0, x29, #0x30
00 68 66 38 # ldrb w0, [x0, x6]
00 00 00 94 @valeur_hex # bl valeur_hex
1f 40 00 71 # cmp w0, #0x10
02 00 00 54 @mal_forme # b.hs mal_forme
e1 03 00 2a # mov w1, w0
c6 04 00 91 # add x6, x6, #0x1
a0 c3 00 91 # add x0, x29, #0x30
00 68 66 38 # ldrb w0, [x0, x6]
00 00 00 94 @valeur_hex # bl valeur_hex
1f 40 00 71 # cmp w0, #0x10
02 00 00 54 @mal_forme # b.hs mal_forme
00 10 01 0b # add w0, w0, w1, lsl #4
c2 fc 41 d3 # lsr x2, x6, #1
03 c5 00 91 # add x3, x8, #0x31
60 68 22 38 # strb w0, [x3, x2]
c6 04 00 91 # add x6, x6, #0x1
00 00 00 14 @macro_valeur # b macro_valeur
:macro_definie
e2 fc 41 d3 # lsr x2, x7, #1
02 c1 00 39 # strb w2, [x8, #0x30]
31 06 00 91 # add x17, x17, #0x1
0a 00 80 d2 # mov x10, #0
00 00 00 14 @boucle # b boucle
# définir : ':' et un nom ; l'étiquette prend la position de sortie courante
:definir
00 00 00 94 @lire_nom # bl lire_nom
e1 0a 00 8b # add x1, x23, x0, lsl #2
22 00 40 b9 # ldr w2, [x1]
02 00 00 35 @doublon # cbnz w2, doublon
82 06 00 11 # add w2, w20, #0x1
22 00 00 b9 # str w2, [x1]
2a 00 80 d2 # mov x10, #0x1
00 00 00 14 @boucle # b boucle
# renvoi : '@' (branche) ou '%' (adresse) corrige les 4 octets qui viennent d'être écrits
:renvoi_branche
0e 00 80 d2 # mov x14, #0
00 00 00 14 @renvoi # b renvoi
:renvoi_adresse
2e 00 80 d2 # mov x14, #0x1
:renvoi
00 00 00 94 @lire_nom # bl lire_nom
ed 03 00 aa # mov x13, x0
9f 12 00 f1 # cmp x20, #0x4
03 00 00 54 @champ_invalide # b.lo champ_invalide
9f 06 40 f2 # tst x20, #0x3
01 00 00 54 @champ_invalide # b.ne champ_invalide
81 12 00 d1 # sub x1, x20, #0x4
62 6a 61 b8 # ldr w2, [x19, x1]
0e 00 00 b5 @renvoi_adr # cbnz x14, renvoi_adr
# '@' : b ou bl (imm26 à zéro) ?
43 10 06 12 # and w3, w2, #0x7c000000
04 80 a2 52 # mov w4, #0x14000000
7f 00 04 6b # cmp w3, w4
01 00 00 54 @pas_b # b.ne pas_b
43 64 00 12 # and w3, w2, #0x3ffffff
03 00 00 35 @champ_invalide # cbnz w3, champ_invalide
25 00 80 d2 # mov x5, #0x1
00 00 00 14 @enregistrer # b enregistrer
# b.cond (octet haut 0x54, bit 4 à zéro) ?
:pas_b
43 7c 18 53 # lsr w3, w2, #24
7f 50 01 71 # cmp w3, #0x54
01 00 00 54 @pas_bcond # b.ne pas_bcond
43 00 1c 12 # and w3, w2, #0x10
03 00 00 35 @champ_invalide # cbnz w3, champ_invalide
00 00 00 14 @champ19 # b champ19
# cbz ou cbnz (32 ou 64 bits) ?
:pas_bcond
43 14 07 12 # and w3, w2, #0x7e000000
04 80 a6 52 # mov w4, #0x34000000
7f 00 04 6b # cmp w3, w4
01 00 00 54 @champ_invalide # b.ne champ_invalide
:champ19
43 48 1b 12 # and w3, w2, #0xffffe0
03 00 00 35 @champ_invalide # cbnz w3, champ_invalide
45 00 80 d2 # mov x5, #0x2
00 00 00 14 @enregistrer # b enregistrer
# '%' : adr (octet haut 0x10 : op = 0 et immlo à zéro, immhi à zéro)
:renvoi_adr
43 7c 18 53 # lsr w3, w2, #24
7f 40 00 71 # cmp w3, #0x10
01 00 00 54 @champ_invalide # b.ne champ_invalide
43 48 1b 12 # and w3, w2, #0xffffe0
03 00 00 35 @champ_invalide # cbnz w3, champ_invalide
65 00 80 d2 # mov x5, #0x3
# enregistrer : ajouter le renvoi (8 192 au plus) ; x1 position, x13 nom, x15 second nom, x5 sorte
:enregistrer
3f 0b 40 f1 # cmp x25, #0x2, lsl #12
02 00 00 54 @plein # b.hs plein
03 13 19 8b # add x3, x24, x25, lsl #4
61 00 00 b9 # str w1, [x3]
6d 08 00 79 # strh w13, [x3, #0x4]
6f 0c 00 79 # strh w15, [x3, #0x6]
65 20 00 39 # strb w5, [x3, #0x8]
39 07 00 91 # add x25, x25, #0x1
2a 00 80 d2 # mov x10, #0x1
00 00 00 14 @boucle # b boucle
# position : '&a' ou '&a>b' écrit 4 octets à zéro, remplis à la fin par la position de a (moins celle de b)
:position
00 00 00 94 @lire_nom # bl lire_nom
ed 03 00 aa # mov x13, x0
ef ff 9f d2 # mov x15, #0xffff
0c 00 00 b4 @position_ecrire # cbz x12, position_ecrire
bf fa 00 71 # cmp w21, #0x3e ('>')
01 00 00 54 @position_ecrire # b.ne position_ecrire
0c 00 80 d2 # mov x12, #0
00 00 00 94 @lire_nom # bl lire_nom
ef 03 00 aa # mov x15, x0
:position_ecrire
01 02 a0 d2 # mov x1, #0x100000
21 10 00 d1 # sub x1, x1, #0x4
9f 02 01 eb # cmp x20, x1
08 00 00 54 @plein # b.hi plein
7f 6a 34 b8 # str wzr, [x19, x20]
e1 03 14 aa # mov x1, x20
94 12 00 91 # add x20, x20, #0x4
85 00 80 d2 # mov x5, #0x4
00 00 00 14 @enregistrer # b enregistrer
# aligner : '!N' (N hexadécimal, puissance de deux, au plus 0x10000) écrit des zéros jusqu'à un multiple de N
:aligner
06 00 80 d2 # mov x6, #0
07 00 80 d2 # mov x7, #0
:aligner_chiffre
00 00 00 94 @octet_suivant # bl octet_suivant
0b 00 00 b5 @aligner_nombre # cbnz x11, aligner_nombre
a0 c2 00 51 # sub w0, w21, #0x30
1f 24 00 71 # cmp w0, #0x9
09 00 00 54 @aligner_ajouter # b.ls aligner_ajouter
a0 02 1b 32 # orr w0, w21, #0x20
00 84 01 51 # sub w0, w0, #0x61
1f 14 00 71 # cmp w0, #0x5
08 00 00 54 @aligner_fin_nombre # b.hi aligner_fin_nombre
00 28 00 11 # add w0, w0, #0xa
:aligner_ajouter
ff 14 00 f1 # cmp x7, #0x5
02 00 00 54 @mal_forme # b.hs mal_forme
06 10 06 8b # add x6, x0, x6, lsl #4
e7 04 00 91 # add x7, x7, #0x1
00 00 00 14 @aligner_chiffre # b aligner_chiffre
:aligner_fin_nombre
2c 00 80 d2 # mov x12, #0x1
:aligner_nombre
07 00 00 b4 @mal_forme # cbz x7, mal_forme
06 00 00 b4 @champ_invalide # cbz x6, champ_invalide
c1 04 00 d1 # sub x1, x6, #0x1
df 00 01 ea # tst x6, x1
01 00 00 54 @champ_invalide # b.ne champ_invalide
21 00 a0 d2 # mov x1, #0x10000
df 00 01 eb # cmp x6, x1
08 00 00 54 @champ_invalide # b.hi champ_invalide
:aligner_boucle
c1 04 00 d1 # sub x1, x6, #0x1
9f 02 01 ea # tst x20, x1
00 00 00 54 @aligner_fini # b.eq aligner_fini
01 02 a0 d2 # mov x1, #0x100000
9f 02 01 eb # cmp x20, x1
02 00 00 54 @plein # b.hs plein
7f 6a 34 38 # strb wzr, [x19, x20]
94 06 00 91 # add x20, x20, #0x1
00 00 00 14 @aligner_boucle # b aligner_boucle
:aligner_fini
2a 00 80 d2 # mov x10, #0x1
00 00 00 14 @boucle # b boucle
# fin : aucun octet à moitié écrit, puis résoudre tous les renvois avant de rien écrire
:fin
06 00 80 d2 # mov x6, #0
:resoudre
df 00 19 eb # cmp x6, x25
02 00 00 54 @ecrire # b.hs ecrire
03 13 06 8b # add x3, x24, x6, lsl #4
61 00 40 b9 # ldr w1, [x3]
60 08 40 79 # ldrh w0, [x3, #0x4]
65 20 40 39 # ldrb w5, [x3, #0x8]
e2 7a 60 b8 # ldr w2, [x23, x0, lsl #2]
02 00 00 34 @inconnue # cbz w2, inconnue
42 04 00 51 # sub w2, w2, #0x1
bf 10 00 f1 # cmp x5, #0x4
00 00 00 54 @resoudre_position # b.eq resoudre_position
47 00 01 cb # sub x7, x2, x1
68 6a 61 b8 # ldr w8, [x19, x1]
bf 0c 00 f1 # cmp x5, #0x3
00 00 00 54 @resoudre_adr # b.eq resoudre_adr
ff 04 40 f2 # tst x7, #0x3
01 00 00 54 @champ_invalide # b.ne champ_invalide
e7 fc 42 93 # asr x7, x7, #2
bf 04 00 f1 # cmp x5, #0x1
01 00 00 54 @resoudre19 # b.ne resoudre19
# imm26 : -2^25 <= écart < 2^25
e9 fc 59 93 # asr x9, x7, #25
29 05 00 91 # add x9, x9, #0x1
3f 05 00 f1 # cmp x9, #0x1
08 00 00 54 @champ_invalide # b.hi champ_invalide
e7 64 00 12 # and w7, w7, #0x3ffffff
08 01 07 2a # orr w8, w8, w7
00 00 00 14 @ranger # b ranger
# imm19 : -2^18 <= écart < 2^18
:resoudre19
e9 fc 52 93 # asr x9, x7, #18
29 05 00 91 # add x9, x9, #0x1
3f 05 00 f1 # cmp x9, #0x1
08 00 00 54 @champ_invalide # b.hi champ_invalide
e7 48 00 12 # and w7, w7, #0x7ffff
08 15 07 2a # orr w8, w8, w7, lsl #5
00 00 00 14 @ranger # b ranger
# adr : -2^20 <= écart < 2^20, immlo = bits 1:0 vers 30:29, immhi = bits 20:2 vers 23:5
:resoudre_adr
e9 fc 54 93 # asr x9, x7, #20
29 05 00 91 # add x9, x9, #0x1
3f 05 00 f1 # cmp x9, #0x1
08 00 00 54 @champ_invalide # b.hi champ_invalide
e9 04 00 12 # and w9, w7, #0x3
08 75 09 2a # orr w8, w8, w9, lsl #29
e9 50 02 53 # ubfx w9, w7, #2, #19
08 15 09 2a # orr w8, w8, w9, lsl #5
00 00 00 14 @ranger # b ranger
# position : la position de a, moins celle de b s'il y en a une (jamais négative)
:resoudre_position
60 0c 40 79 # ldrh w0, [x3, #0x6]
e4 ff 9f d2 # mov x4, #0xffff
1f 00 04 eb # cmp x0, x4
00 00 00 54 @ranger_position # b.eq ranger_position
e4 7a 60 b8 # ldr w4, [x23, x0, lsl #2]
04 00 00 34 @inconnue # cbz w4, inconnue
84 04 00 51 # sub w4, w4, #0x1
42 00 04 eb # subs x2, x2, x4
03 00 00 54 @champ_invalide # b.lo champ_invalide
:ranger_position
e8 03 02 2a # mov w8, w2
:ranger
68 6a 21 b8 # str w8, [x19, x1]
c6 04 00 91 # add x6, x6, #0x1
00 00 00 14 @resoudre # b resoudre
# écrire : envoyer toute la sortie sur la sortie standard (les écritures peuvent être partielles)
:ecrire
1a 00 80 d2 # mov x26, #0
:boucle_ecrire
5f 03 14 eb # cmp x26, x20
02 00 00 54 @succes # b.hs succes
20 00 80 d2 # mov x0, #0x1
61 02 1a 8b # add x1, x19, x26
82 02 1a cb # sub x2, x20, x26
90 00 80 d2 # mov x16, #0x4 (écriture)
01 10 00 d4 # svc #0x80
02 00 00 54 @echec_lecture_ecriture # b.hs echec_lecture_ecriture
00 00 00 b4 @echec_lecture_ecriture # cbz x0, echec_lecture_ecriture
5a 03 00 8b # add x26, x26, x0
00 00 00 14 @boucle_ecrire # b boucle_ecrire
# sorties : 0 succès, 1 lecture/écriture, 2 chiffre seul, 3 nom en double, 4 nom inconnu, 5 champ invalide, 6 plein, 7 mal formé
:succes
00 00 80 d2 # mov x0, #0
00 00 00 14 @sortir # b sortir
:echec_lecture_ecriture
20 00 80 d2 # mov x0, #0x1
00 00 00 14 @sortir # b sortir
:impair
40 00 80 d2 # mov x0, #0x2
00 00 00 14 @sortir # b sortir
:doublon
60 00 80 d2 # mov x0, #0x3
00 00 00 14 @sortir # b sortir
:inconnue
80 00 80 d2 # mov x0, #0x4
00 00 00 14 @sortir # b sortir
:champ_invalide
a0 00 80 d2 # mov x0, #0x5
00 00 00 14 @sortir # b sortir
:plein
c0 00 80 d2 # mov x0, #0x6
00 00 00 14 @sortir # b sortir
:mal_forme
e0 00 80 d2 # mov x0, #0x7
:sortir
30 00 80 d2 # mov x16, #0x1 (sortie)
01 10 00 d4 # svc #0x80
# lire_jeton : lit un jeton (1 à 3 840 caractères non blancs, le premier est déjà dans w21) dans la zone x29+0x30,
# dont les 48 premiers octets sont mis à zéro (un nom de macro en a 48 au plus) ;
# x7 = sa longueur ; le blanc qui le termine est consommé ; x9 garde l'adresse de retour.
:lire_jeton
e9 03 1e aa # mov x9, x30
bf 7f 03 a9 # stp xzr, xzr, [x29, #0x30]
bf 7f 04 a9 # stp xzr, xzr, [x29, #0x40]
bf 7f 05 a9 # stp xzr, xzr, [x29, #0x50]
07 00 80 d2 # mov x7, #0
:jeton_boucle
ff 00 3c f1 # cmp x7, #0xf00
02 00 00 54 @mal_forme # b.hs mal_forme
a0 c3 00 91 # add x0, x29, #0x30
15 68 27 38 # strb w21, [x0, x7]
e7 04 00 91 # add x7, x7, #0x1
00 00 00 94 @octet_suivant # bl octet_suivant
0b 00 00 b5 @jeton_lu # cbnz x11, jeton_lu
bf 82 00 71 # cmp w21, #0x20
00 00 00 54 @jeton_lu # b.eq jeton_lu
bf 2a 00 71 # cmp w21, #0xa
00 00 00 54 @jeton_lu # b.eq jeton_lu
bf 26 00 71 # cmp w21, #0x9
00 00 00 54 @jeton_lu # b.eq jeton_lu
bf 36 00 71 # cmp w21, #0xd
00 00 00 54 @jeton_lu # b.eq jeton_lu
00 00 00 14 @jeton_boucle # b jeton_boucle
:jeton_lu
20 01 5f d6 # ret x9
# chercher_macro : cherche le nom de la zone x29+0x30 (six mots, gardés dans x0, x1, x2, x13, x14, x15) ;
# x5 = 1 et x3 = son entrée s'il existe, sinon x5 = 0 et x3 = la première entrée libre.
:chercher_macro
a0 07 43 a9 # ldp x0, x1, [x29, #0x30]
a2 37 44 a9 # ldp x2, x13, [x29, #0x40]
ae 3f 45 a9 # ldp x14, x15, [x29, #0x50]
03 83 40 91 # add x3, x24, #0x20, lsl #12
04 00 80 d2 # mov x4, #0
:macro_boucle
9f 00 11 eb # cmp x4, x17
02 00 00 54 @macro_absente # b.hs macro_absente
65 18 40 a9 # ldp x5, x6, [x3]
bf 00 00 eb # cmp x5, x0
01 00 00 54 @macro_suivante # b.ne macro_suivante
df 00 01 eb # cmp x6, x1
01 00 00 54 @macro_suivante # b.ne macro_suivante
65 18 41 a9 # ldp x5, x6, [x3, #0x10]
bf 00 02 eb # cmp x5, x2
01 00 00 54 @macro_suivante # b.ne macro_suivante
df 00 0d eb # cmp x6, x13
01 00 00 54 @macro_suivante # b.ne macro_suivante
65 18 42 a9 # ldp x5, x6, [x3, #0x20]
bf 00 0e eb # cmp x5, x14
01 00 00 54 @macro_suivante # b.ne macro_suivante
df 00 0f eb # cmp x6, x15
01 00 00 54 @macro_suivante # b.ne macro_suivante
25 00 80 d2 # mov x5, #0x1
c0 03 5f d6 # ret
:macro_suivante
84 04 00 91 # add x4, x4, #0x1
63 00 01 91 # add x3, x3, #0x40
00 00 00 14 @macro_boucle # b macro_boucle
:macro_absente
05 00 80 d2 # mov x5, #0
c0 03 5f d6 # ret
# valeur_hex : w0 = la valeur du chiffre hexadécimal w0 (0 à 15), ou 16 si ce n'en est pas un (emploie w3)
:valeur_hex
03 c0 00 51 # sub w3, w0, #0x30
7f 24 00 71 # cmp w3, #0x9
09 00 00 54 @chiffre_valide # b.ls chiffre_valide
03 00 1b 32 # orr w3, w0, #0x20
63 84 01 51 # sub w3, w3, #0x61
7f 14 00 71 # cmp w3, #0x5
08 00 00 54 @pas_un_chiffre # b.hi pas_un_chiffre
63 28 00 11 # add w3, w3, #0xa
:chiffre_valide
e0 03 03 2a # mov w0, w3
c0 03 5f d6 # ret
:pas_un_chiffre
00 02 80 52 # mov w0, #0x10
c0 03 5f d6 # ret
# lire_nom : lit un nom (1 à 32 caractères parmi 0-9 A-Z a-z _ .) dans la zone x29+8, le cherche ou l'ajoute ; x0 = son index.
# Le caractère qui suit le nom reste à relire (x12 = 1) ; x9 garde l'adresse de retour (lire_nom appelle octet_suivant).
:lire_nom
e9 03 1e aa # mov x9, x30
bf ff 00 a9 # stp xzr, xzr, [x29, #0x8]
bf ff 01 a9 # stp xzr, xzr, [x29, #0x18]
07 00 80 d2 # mov x7, #0
:lire_nom_boucle
00 00 00 94 @octet_suivant # bl octet_suivant
0b 00 00 b5 @nom_fini # cbnz x11, nom_fini
a0 c2 00 51 # sub w0, w21, #0x30
1f 24 00 71 # cmp w0, #0x9
09 00 00 54 @nom_ajouter # b.ls nom_ajouter
bf 7e 01 71 # cmp w21, #0x5f ('_')
00 00 00 54 @nom_ajouter # b.eq nom_ajouter
bf ba 00 71 # cmp w21, #0x2e ('.')
00 00 00 54 @nom_ajouter # b.eq nom_ajouter
a0 02 1b 32 # orr w0, w21, #0x20
00 84 01 51 # sub w0, w0, #0x61
1f 64 00 71 # cmp w0, #0x19
08 00 00 54 @nom_attente # b.hi nom_attente
:nom_ajouter
ff 80 00 f1 # cmp x7, #0x20
02 00 00 54 @mal_forme # b.hs mal_forme
a0 23 00 91 # add x0, x29, #0x8
15 68 27 38 # strb w21, [x0, x7]
e7 04 00 91 # add x7, x7, #0x1
00 00 00 14 @lire_nom_boucle # b lire_nom_boucle
:nom_attente
2c 00 80 d2 # mov x12, #0x1
:nom_fini
07 00 00 b4 @mal_forme # cbz x7, mal_forme
a0 87 40 a9 # ldp x0, x1, [x29, #0x8]
a2 8f 41 a9 # ldp x2, x3, [x29, #0x18]
04 00 80 d2 # mov x4, #0
e5 03 16 aa # mov x5, x22
:chercher
9f 00 1a eb # cmp x4, x26
02 00 00 54 @nom_nouveau # b.hs nom_nouveau
a6 1c 40 a9 # ldp x6, x7, [x5]
df 00 00 eb # cmp x6, x0
01 00 00 54 @chercher_suivant # b.ne chercher_suivant
ff 00 01 eb # cmp x7, x1
01 00 00 54 @chercher_suivant # b.ne chercher_suivant
a6 1c 41 a9 # ldp x6, x7, [x5, #0x10]
df 00 02 eb # cmp x6, x2
01 00 00 54 @chercher_suivant # b.ne chercher_suivant
ff 00 03 eb # cmp x7, x3
01 00 00 54 @chercher_suivant # b.ne chercher_suivant
e0 03 04 aa # mov x0, x4
20 01 5f d6 # ret x9
:chercher_suivant
84 04 00 91 # add x4, x4, #0x1
a5 80 00 91 # add x5, x5, #0x20
00 00 00 14 @chercher # b chercher
:nom_nouveau
5f 03 10 f1 # cmp x26, #0x400
02 00 00 54 @plein # b.hs plein
a0 04 00 a9 # stp x0, x1, [x5]
a2 0c 01 a9 # stp x2, x3, [x5, #0x10]
e0 03 1a aa # mov x0, x26
5a 07 00 91 # add x26, x26, #0x1
20 01 5f d6 # ret x9
# octet_suivant : w21 = octet suivant (ou l'octet en attente), ou x11 = 1 en fin d'entrée
:octet_suivant
0c 00 00 b4 @lire_octet # cbz x12, lire_octet
0c 00 80 d2 # mov x12, #0
c0 03 5f d6 # ret
:lire_octet
00 00 80 d2 # mov x0, #0
e1 03 1d aa # mov x1, x29
22 00 80 d2 # mov x2, #0x1
70 00 80 d2 # mov x16, #0x3 (lecture)
01 10 00 d4 # svc #0x80
02 00 00 54 @echec_lecture_ecriture # b.hs echec_lecture_ecriture
00 00 00 b4 @fin_entree # cbz x0, fin_entree
b5 03 40 39 # ldrb w21, [x29]
c0 03 5f d6 # ret
:fin_entree
2b 00 80 d2 # mov x11, #0x1
c0 03 5f d6 # ret
:fin_code
# ---------------------------------------------------------------------------
!4000 # zéros jusqu'à 0x4000 : __LINKEDIT doit commencer sur une frontière de page de 16 Kio
:linkedit
# Correctifs chaînés vides (56 octets) : en-tête, puis 3 segments sans correctif.
00 00 00 00 20 00 00 00 # fixups_version 0, starts_offset 32
30 00 00 00 30 00 00 00 # imports_offset 48, symbols_offset 48
00 00 00 00 01 00 00 00 # imports_count 0, imports_format 1
00 00 00 00 00 00 00 00 # symbols_format 0, remplissage
03 00 00 00 00 00 00 00 # seg_count 3, seg_info_offset[0]
00 00 00 00 00 00 00 00 # seg_info_offset[1], seg_info_offset[2]
00 00 00 00 00 00 00 00 # remplissage
:fin_correctifs
00 00 00 00 00 00 00 00 # remplissage jusqu'à la signature
# ---------------------------------------------------------------------------
# Signature ad hoc (gros-boutiste, écrite à la main) : un SuperBlob qui contient un CodeDirectory. Cases d'empreinte à zéro : le signeur les remplit.
:signature
fa de 0c c0 00 00 01 13 # magic EMBEDDED_SIGNATURE, length 275
00 00 00 01 # count 1
00 00 00 00 00 00 00 14 # type CODEDIRECTORY, offset 20
# CodeDirectory (255 octets)
fa de 0c 02 00 00 00 ff # magic CODEDIRECTORY, length 255
00 02 04 00 # version 0x20400
00 02 00 02 # flags ADHOC LINKER_SIGNED
00 00 00 5f 00 00 00 58 # hashOffset 95, identOffset 88
00 00 00 00 00 00 00 05 # nSpecialSlots 0, nCodeSlots 5
00 00 40 40 # codeLimit 0x4040 (tout ce qui précède la signature)
20 02 00 0c # hashSize 32, hashType SHA-256, platform 0, pageSize 2^12
00 00 00 00 # spare2
00 00 00 00 00 00 00 00 00 00 00 00 # scatterOffset, teamOffset, spare3
00 00 00 00 00 00 00 00 # codeLimit64
00 00 00 00 00 00 00 00 # execSegBase 0
00 00 00 00 00 00 40 00 # execSegLimit 0x4000
00 00 00 00 00 00 00 01 # execSegFlags MAIN_BINARY
6d 61 63 72 6f 73 00 # identifiant "macros"
# cinq cases SHA-256 (pages 0x0000, 0x1000, 0x2000, 0x3000, 0x4000-0x403f), remplies par le signeur
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
!10 # remplissage jusqu'à la fin du fichier (multiple de 16)
:fin_fichier