miaou — le blogv1.0.3

adresses.hex2

← Le code · src/amorce/adresses.hex2 · 618 lignes

# adresses.hex2 : source de l'étage 3a, l'assembleur hex2, écrit en hex2 avec de vrais noms (Mach-O, macOS ARM64).
# Construction : adresses.macos-arm64 < adresses.hex2 | signeur.macos-arm64 > adresses.macos-arm64
# (point fixe : les mêmes octets que depuis son image adresses.hex1, assemblée par l'étage 1).
# Les champs de l'en-tête Mach-O (tailles, positions) sont calculés par '&a' et '&a>b', les remplissages par '!N'.
# Restent écrits à la main : les champs gros-boutistes de la signature (hex2 n'écrit que du petit-boutiste)
# et les constantes du format (numéros de commande, versions, drapeaux).
 
# ---------------------------------------------------------------------------
# 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, x27 = 1 si un chiffre haut attend, x28 ce chiffre, x29 zone de lecture (case de lecture en +0, nom courant en +8),
# 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, lecture), registres, positions à zéro
ff ab 44 d1              # sub  sp, sp, #0x12a, 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 83 40 91              # add  x29, x24, #0x20, lsl #12
14 00 80 d2              # mov  x20, #0
19 00 80 d2              # mov  x25, #0
1a 00 80 d2              # mov  x26, #0
1b 00 80 d2              # mov  x27, #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 et le trier
:boucle
00 00 00 94 @octet_suivant           # bl   octet_suivant
0b 00 00 b5 @fin           # cbnz x11, fin
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 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 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
a0 c2 00 51              # sub  w0, w21, #0x30
1f 24 00 71              # cmp  w0, #0x9
09 00 00 54 @chiffre           # b.ls chiffre
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 @mal_forme           # b.hi mal_forme
00 28 00 11              # add  w0, w0, #0xa
 
# chiffre : le premier chiffre attend, le second complète l'octet
:chiffre
1b 00 00 b5 @second           # cbnz x27, second
fc 03 00 aa              # mov  x28, x0
3b 00 80 d2              # mov  x27, #0x1
00 00 00 14 @boucle           # b    boucle
:second
00 10 1c 0b              # add  w0, w0, w28, lsl #4
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
1b 00 80 d2              # mov  x27, #0
00 00 00 14 @boucle           # b    boucle
 
# blanc : un séparateur ; un octet ne se coupe pas en deux
:blanc
1b 00 00 b5 @impair           # cbnz x27, impair
0a 00 80 d2              # mov  x10, #0
00 00 00 14 @boucle           # b    boucle
# commentaire : jusqu'à la fin de la ligne
:commentaire
1b 00 00 b5 @impair           # cbnz x27, impair
: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
 
# définir : ':' et un nom ; l'étiquette prend la position de sortie courante
:definir
1b 00 00 b5 @impair           # cbnz x27, impair
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
1b 00 00 b5 @impair           # cbnz x27, impair
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
1b 00 00 b5 @impair           # cbnz x27, impair
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
1b 00 00 b5 @impair           # cbnz x27, impair
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
1b 00 00 b5 @impair           # cbnz x27, impair
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_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 15  # magic EMBEDDED_SIGNATURE, length 277
00 00 00 01              # count 1
00 00 00 00 00 00 00 14  # type CODEDIRECTORY, offset 20
# CodeDirectory (257 octets)
fa de 0c 02 00 00 01 01  # magic CODEDIRECTORY, length 257
00 02 04 00              # version 0x20400
00 02 00 02              # flags ADHOC LINKER_SIGNED
00 00 00 61 00 00 00 58  # hashOffset 97, 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
61 64 72 65 73 73 65 73 00  # identifiant "adresses"
# 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