Thompsons self reproducing program

Larry Cipriani lvc at cbscc.UUCP
Sat Aug 18 02:19:14 AEST 1984


In the August '84 issue of "Communications of the ACM" were
the Turing Award Lectures by Ritchie and Thompson [trademarks
of Bell Laboratories].  The article by Thompson showed how one
could write a program that when compiled and executed would
reproduce it's source.

I got it to work on our 3b5 and 3b20, and managed to make it
even smaller.  This program may require minor modifications on
other 32 bit machines, and will require major modifications on
16 bit machines.  The self reproducer is 325 characters long and
contains no excess whitespace.

As in Thompsons example, what is below is a program that
produces a self reproducing program (for ease of programming!).

int s[]={

'int ', '*p=s', ';pri', 'ntf(', '"int',

' s[]', '={")', ';whi', 'le(*', 'p)pr',

'intf', '("%d', ',",*', 'p++)', ';pri',

'ntf(', '"0};', 'main', '(){%', 's}",',

's); ', 0

} ;

main()
{
	int *p = s ;

	printf("int s[]={") ;

	while(*p)
		printf("%d,", *p++) ;

	printf("0};main(){%s}", s) ;
}

If anyone can come up with a self reproducer that is smaller
I'd be interested in seein it.  Have fun,

				Larry Cipriani
				cbscc!lvc



More information about the Comp.sources.unix mailing list