Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site mit-athena.ARPA
Path: utzoo!linus!decvax!mit-athena!martillo
From: martillo@mit-athena.ARPA (Joaquim Martillo)
Newsgroups: net.math
Subject: Shortest Fib Routine in Machine Language
Message-ID: <248@mit-athena.ARPA>
Date: Thu, 16-Aug-84 17:55:15 EDT
Article-I.D.: mit-athe.248
Posted: Thu Aug 16 17:55:15 1984
Date-Received: Sun, 19-Aug-84 12:28:46 EDT
Organization: MIT, Project Athena, Cambridge, Ma.
Lines: 89

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