; quine.iso / quine.asm
;
; This file is a polyglot double-quine!
;
; It's a polyglot because it's simultaneously a valid ISO disc image,
; and NASM assembler source code. When read as an ISO, on it are two
; identical copies of itself named "quine.iso" and "quine.asm". When
; assembled with NASM, the output is also itself. A double-quine!
;
; By Sijmen J. Mulder <ik@sjmulder.nl>, 2026

%define be16(n)		(n&0xFF)<<8 | (n&0xFF00)>>8
%define be32(n)		(n&0xFF)<<24 | (n&0xFF00)<<8 | \
			(n&0xFF0000)>>8 | (n&0xFF000000)>>24

%define lb16(n)		n, be16(n)
%define lb32(n)		n, be32(n)

%define cdiv(x,n)	(x+n-1)/n
%define lba(n)		n/BLOCK_SZ

BLOCK_SZ:	equ 0x800
SECTOR_SZ:	equ 0x800
ASM_SZ:		equ 0x1000

VD_PRIMARY:	equ 0x01
VD_TERMINATOR:	equ 0xFF

ID_SELF:	equ 0x00
ID_PARENT:	equ 0x01

FF_DIRECTORY:	equ 1<<1

; Generates a directory entry
;
; Arguments:
;  - varable name of the extent (also define %_sz)
;  - identifier
;  - flags
%macro dir_ent 3
%%ent:	db %%sz			; entry size
	db 0			; atribute record length
	dd lb32(lba(%1))	; location
	dd lb32(%1_sz)		; size
	times 7 db 0		; date
	db %3			; flags
	db 0			; interleaved unit size
	db 0			; interleaved gap size
	dw lb16(1)		; volume
	db %%idzs		; ID length
%%id:	db %2			; ID
%%idzs:	equ $-%%id
	alignb 2
%%sz:	equ $-%%ent
%endmacro

file:	equ 0

	incbin __?FILE?__, 0, ASM_SZ
%if $-$$ < ASM_SZ
	db `\n%if 0\n`
	times ASM_SZ-($-$$) db 0
%endif

	alignb BLOCK_SZ
le_paths:	equ $-$$
	db 1			; ID length
	db 0			; extended attribute length
	dd lba(root_ext)	; location
	dw 1			; parent number (self)
	db ID_SELF		; ID
	alignb 2
le_paths_sz:	equ $-$$-le_paths

	alignb BLOCK_SZ
be_paths:	equ $-$$
	db 1			; ID length
	db 0			; extended attribute length
	dd be32(lba(root_ext))	; location
	dw be16(1)		; parent number (self)
	db ID_SELF		; ID
	alignb 2
be_paths_sz:	equ $-$$-be_paths

	alignb BLOCK_SZ
root_ext:	equ $-$$
	dir_ent root_ext, ID_SELF, FF_DIRECTORY
	dir_ent root_ext, ID_PARENT, FF_DIRECTORY
	dir_ent file, "QUINE.ISO;1", 0
	dir_ent file, "QUINE.ASM;1", 0
root_ext_sz:	equ $-$$-root_ext

	times 0x8000-($-$$) db 0
	db VD_PRIMARY
	db 'CD001'
	db 0x01			; version
	db 0x00			; unused
	times 32 db  ' '	; boot system identifier
.id:	db 'QUINE'		; volume identifier
	times 32-($-.id) db ' '
	times 8 db 0		; unused
	dd lb32(cdiv(file_sz, BLOCK_SZ)) ; volume space size
	times 32 db 0		; unused
	dw lb16(1)		; volume set size
	dw lb16(1)		; volume sequence number
	dw lb16(BLOCK_SZ)	; block size
	dd lb32(le_paths_sz)	; path table size
	dd lba(le_paths)	; path table (LE)
	dd 0			; optional path table (LE)
	dd be32(lba(be_paths))	; path table (BE)
	dd be32(0)		; optional path table (BE)
	dir_ent root_ext, ID_SELF, FF_DIRECTORY
.vsid:	db 'QUINE'		; volume set ID
	times 128-($-.vsid) db ' '
.pid:	db 'SJ_MULDER'		; publisher
	times 128-($-.pid) db ' '
	times 128 db ' '	; data preparer ID
	times 128 db ' '	; app ID
	times 37 db ' '		; copyright file
	times 37 db ' '		; abstract file
	times 37 db ' '		; bibliographic file
	db '0000000000000000',0	; creation date
	db '0000000000000000',0	; modification date
	db '0000000000000000',0	; expiration date
	db '0000000000000000',0	; effective date
	db 0x01			; structure version
	db 0			; unused
	times 512 db 0		; application used

	alignb SECTOR_SZ
	db VD_TERMINATOR
	db 'CD001'
	db 0x01			; version

	times 0x9000-8-($-$$) db 0
	db `\n%endif\n`

file_sz:	equ $-$$

%if 0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   "       œ      œ            "       œ      œ           ,                           QUINE.ISO;1,                           QUINE.ASM;1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    CD001                                 QUINE                                                                               
      
              "       œ      œ            QUINE                                                                                                                           SJ_MULDER                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      0000000000000000 0000000000000000 0000000000000000 0000000000000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ÿCD001                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
%endif
