[net.math] Shortest Fib Routine in Machine Language

martillo@mit-athena.ARPA (Joaquim Martillo) (08/17/84)

A few years ago, I was  dared  to  write  a  fib  routine  with  as  few
instructions  of  pdp  11 machine code as possible.  I believe I wrote a
routine of seven or eight instructions.  Recently, the problem  came  up
again  and the routine I wrote took 9 instructions.  Here is the routine
and a c-language caller (so that it can be run on a vax).  Can anyone do
better?

Compile as 

	cc fib.c _fib.s -o fib


/* fib.c */

extern int input, output;

extern fib();


main()

{
	
	while(1)
	
	{
		
		printf("Enter Fib Value: ");
		
		if( scanf("%d", &input) < 1 )

		{

			exit();

		}

		fib();

		printf("Result is %d\n", output);
		
	}
	
}


/* _fib.s */

	.data
	.comm _input,4
	.comm _output,4

	.text
	.align	1
	.globl	_fib
	.align	2
_fib:	
	.word	0
	
	clrl	r1

	movl	_input, r0

	jsb	fib

	ret

fib:	decl	r0		;	1



	bleq	L0		;	2

	movl	r0, -(sp)	;	3

	jsb	fib		;	4

	jsb	fib		;	5

	movl	(sp)+, r0	;	6

	brb	L1		;	7

L0:	incl	r1		;	8

	movl	r1, _output	;	Just preset for C routine

L1:	rsb			;	9