Assembly for x86 Linux, 106 bytes
BITS 32
org 0x2E620000
db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
dd 0, 0
dw 2 ; e_type
dw 3 ; e_machine
dd 1 ; e_version
dd _start ; e_entry
dd phdr - $$ ; e_phoff
dd 0 ; e_shoff
dd 0 ; e_flags
dw 0x34 ; e_ehsize
dw 0x20 ; e_phentsize
phdr: dd 1 ; e_phnum ; p_type
; e_shentsize
dd 0 ; e_shnum ; p_offset
; e_shstrndx
dd $$ ; p_vaddr
fname equ $ - 2
db 'out', 0 ; p_paddr
dd filesize ; p_filesz
dd filesize ; p_memsz
dd 5 ; p_flags
dd 0x1000 ; p_align
_start: mov al, 5 ; 5 = open syscall
mov ebx, fname
mov cl, 65 ; 65 = O_WRONLY | O_CREAT
mov dx, 666q
int 0x80
lea edx, [byte ecx + filesize - 65]
xchg eax, ebx
xchg eax, ecx
mov cl, 0
mov al, 4 ; 4 = write syscall
int 0x80
mov al, 1 ; 1 = exit syscall
int 0x80
filesize equ $ - $$
This is for the nasm assembler. Build the binary with the command line: nasm -f bin -o a.out selfrep.asm && chmod +x a.out
Here's the same file as a hex dump: 7F 45 4C 46 01 01 01 00 00 00 00 00 00 00 00 00 02 00 03 00 01 00 00 00 4C 00 62 2E 2C 00 00 00 00 00 00 00 00 00 00 00 34 00 20 00 01 00 00 00 00 00 00 00 00 00 62 2E 6F 75 74 00 6A 00 00 00 6A 00 00 00 05 00 00 00 00 10 00 00 B0 05 BB 36 00 62 2E B1 41 66 BA B6 01 CD 80 8D 51 29 93 91 B1 00 B0 04 CD 80 B0 01 CD 80
As requested, the program copies itself to a separate file. (The program could have been significantly shorter if it had been allowed to just write to stdout and let the user redirect to a file.)
I avoided using any borderline tricks to reduce the size. This should be a fully conformant 32-bit ELF binary.
Edited to add: In the above version the created file is just a plain file, but it occurs to me that for a couple of bytes (and a tiny bend of the rules), you can create something a little more interesting. This version is only two bytes longer, at 108 bytes:
BITS 32
org 0x00010000
db 0x7F, "ELF", 1, 1, 1, 0 ; e_ident
dd 0, 0
dw 2 ; e_type
dw 3 ; e_machine
dd 1 ; e_version
dd _start ; e_entry
dd phdr - $$ ; e_phoff
dd 0 ; e_shoff
dd 0 ; e_flags
dw 0x34 ; e_ehsize
dw 0x20 ; e_phentsize
phdr: dd 1 ; e_phnum ; p_type
; e_shentsize
dd 0 ; e_shnum ; p_offset
; e_shstrndx
dd $$ ; p_vaddr
fname: db 'asr', 0 ; p_paddr
dd filesize ; p_filesz
dd filesize ; p_memsz
dd 7 ; p_flags
dd 0x1000 ; p_align
_start: mov al, 5 ; 5 = open syscall
mov ebx, fname
inc byte [ebx]
mov cl, 65 ; 65 = O_WRONLY | O_CREAT
mov dx, 777q
int 0x80
lea edx, [byte ecx + filesize - 65]
xchg eax, ebx
xchg eax, ecx
mov cl, 0
mov al, 4 ; 4 = write syscall
int 0x80
mov al, 1 ; 1 = exit syscall
int 0x80
filesize equ $ - $$
Name this version asr
, for "a self-replicator": nasm -f bin -o asr asr.asm && chmod +x asr
Hex dump version for the nasm-impaired:
7F 45 4C 46 01 01 01 00 00 00 00 00 00 00 00 00 02 00 03 00 01 00 00 00 4C 00 01 00 2C 00 00 00 00 00 00 00 00 00 00 00 34 00 20 00 01 00 00 00 00 00 00 00 00 00 01 00 61 73 72 00 6C 00 00 00 6C 00 00 00 07 00 00 00 00 10 00 00 B0 05 BB 38 00 01 00 FE 03 B1 41 66 BA FF 01 CD 80 8D 51 2B 93 91 B1 00 B0 04 CD 80 B0 01 CD 80
When you run it, it creates an almost-identical file named bsr
, but one that is itself executable. Running it will create another binary file named csr
. And so on.
(Note that annoying things start to happen after zsr
. I considered making a version that would cascade the name change to atr
and so on, but I think most people will get bored well before then, so it probably isn't worth all the extra bytes.)
6This seems to vary only trivially from the existing [quine] challenges. Or have I misunderstood? – dmckee --- ex-moderator kitten – 2011-06-22T06:06:12.327
1
@dmckee I saw Assembly Language Quine and Self replicating Hello World program accepting copying, but I couldn't find program writes itself, not its code.
– JiminP – 2011-06-22T06:19:43.040For scripting languages (like say the three top answers to Golf you a quine for great good! ) there is no distinction between code and program, and the basic technique for building quines extends exactly to embedding compiled binaries instead of the source code.
– dmckee --- ex-moderator kitten – 2011-06-22T06:23:38.9101... but I want to see how it actually works!.. I don't know how the idea can be extended with binary codes.. even though I read Quine article at Wikipedia. PS. no compilers for replicating and no script languages are allowed... :/ – JiminP – 2011-06-22T06:26:04.883
Alright, I have some ideas, but I don't think that this problem isn't quite as easy as quine.. – JiminP – 2011-06-22T06:36:26.873
4Any problem can be made slightly harder and uglier by adding more constraints to it. I find this problem a trivial extension of the quine problem. – Alexandru – 2011-06-22T10:22:40.740
I edited your question. Scripts are executables (even .bat, .wsh and .js-Files are executable on Windows). You meant compiled files, native executable, binary files, which are, indeed, a different beast. Maybe you don't like JVM-bytecode? Then feel free to reedit. – user unknown – 2011-06-22T13:48:11.513
This question should not make @JiminP quite embarrassed as stays on the speech "Reflections on Trusting Trust" by Ken Thompson as a good practice.
– H_7 – 2011-10-01T14:45:30.9471If smallest executable size wins, shouldn't we also factor in the interpreter code as part of the executable size for this to be fair to users of compiled languages? – Thomas Dignan – 2011-12-18T19:16:04.790