flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Quines : Programs that copy themselves

Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 4428
Location: vpcmpistri
bitRAKE 22 Jan 2026, 07:25
In fasmg not only does an empty file copy itself, but "file __FILE__" also copies itself. Moving beyond these trivial examples (305 bytes):
Code:
virtual
A::db 'virtual|A::db ~|end virtual|repeat sizeof A|load a:1 from A:%-1|if a=7Eh|load a:%% from A:0|db 27h,a,27h|else if a=7Ch|db 10|else|db a|end if|end repeat'
end virtual
repeat sizeof A
load a:1 from A:%-1
if a=7Eh
load a:%% from A:0
db 27h,a,27h
else if a=7Ch
db 10
else
db a
end if
end repeat    
... you'll need to use Unix (10) line endings. Basically, a tag character is used to self-copy within the string of bytes, and a few exceptions are handled. Greater explanation is probably best done by another's video (he also explains how trojans can be hidden in compilers).

(Of course, a smaller version is possible - down to 291.)

_________________
¯\(°_o)/¯ AI may [not] have aided with the above reply.


Last edited by bitRAKE on 01 Apr 2026, 02:09; edited 1 time in total
Post 22 Jan 2026, 07:25
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8524
Location: Kraków, Poland
Tomasz Grysztar 22 Jan 2026, 10:10
How about something more unique to fasmg?
Code:
a
macro a
calminstruction ?!n&
match,n
jyes x
take m,m
arrange m,n
stringify m
emit lengthof m,m
emit 1,10
jno x
assemble n
x:
end calminstruction
a
end macro    
Post 22 Jan 2026, 10:10
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4428
Location: vpcmpistri
bitRAKE 22 Jan 2026, 12:11
That's bonkers - yet, insightful.

Three distinctly different corner cases.
Post 22 Jan 2026, 12:11
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4428
Location: vpcmpistri
bitRAKE 21 Mar 2026, 02:12
A slight re-arrangement allows removing special handling for the empty line.
Code:
a
macro a
calminstruction ?!n&
take m,m
jno y
emit 1,10
y:
arrange m,n
stringify m
emit lengthof m,m
jno x
assemble n
x:
end calminstruction
a
end macro    
Note: the final newline is no longer needed (152 bytes), but also is allowed. Very Happy

_________________
¯\(°_o)/¯ AI may [not] have aided with the above reply.
Post 21 Mar 2026, 02:12
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8524
Location: Kraków, Poland
Tomasz Grysztar 21 Mar 2026, 09:34
I was hoping you would improve it! Smile
Post 21 Mar 2026, 09:34
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4428
Location: vpcmpistri
bitRAKE 21 Mar 2026, 21:33
Fold conditions together, reduce first line test/track (138 bytes):
Code:
a
macro a
calminstruction ?!n&
take,b
jno z
emit 1,10
assemble n
z:
stringify n
emit lengthof n,n
take b,n
end calminstruction
a
end macro    
... still feel I'm missing something.

It seems something like,
Code:
a
macro a
macro ?!b&
match,c
b
end match
c equ
c equ
db`b,10
end macro
a
end macro    
... would work. It finishes without the unconditional modifier on the ? macro, but only processes the "a".
Post 21 Mar 2026, 21:33
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4428
Location: vpcmpistri
bitRAKE 01 Apr 2026, 01:09
It just occurred to me there is no in-place replace -- that would be a very fasm/fasmg style solution.

Another potential would be larger integer manipulation.

_________________
¯\(°_o)/¯ AI may [not] have aided with the above reply.
Post 01 Apr 2026, 01:09
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4428
Location: vpcmpistri
bitRAKE 03 Apr 2026, 15:53
Code:
DB 'i\jkXikflk![Y79[Y7>9CXCJPCHGC>cfX[7XQ;7]ifd7G>CHGC>\mXc7jki`e^7XD?DH@YjnXg7;F;]]AIJ>'
LOAD A:$ FROM 0
EVAL STRING A-(-1)BSWAP $/$FF*23    
138 bytes, big integer filter eval. Very Happy

_________________
¯\(°_o)/¯ AI may [not] have aided with the above reply.


Last edited by bitRAKE on 03 Apr 2026, 16:32; edited 1 time in total
Post 03 Apr 2026, 15:53
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8524
Location: Kraków, Poland
Tomasz Grysztar 03 Apr 2026, 16:30
Nice!
I was thinking about using EVAL to distil the classic quine idea:
Code:
A='db "A=",39,A,39,10,"EVAL A"'
EVAL A    
Still, making use of fasmg's multi-pass feels more fun.
Post 03 Apr 2026, 16:30
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4428
Location: vpcmpistri
bitRAKE 03 Apr 2026, 17:04
You're right - I still had my head in the load/store in-place replace version.
Should just do:
Code:
A="[Y7>XT9>CXCJKCHGC>\mXc7jki`e^7XD?DH@YjnXg7LIF;]]AIJ>"
EVAL STRING A-(-1)BSWAP 52/$FF*23    
... focused on the theme, 90 bytes.
Post 03 Apr 2026, 17:04
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.