From: spc@pineal.math.fau.edu (Sean 'Captain Napalm' Conner)
Subject: Re: octal to decimal converter?
Organization: Florida Atlantic University, but who cares?
Date: Thu, 24 Dec 1992 21:07:41 GMT

In article <7766353@zl2tnm.gen.nz> don@zl2tnm.gen.nz (Don Stokes) writes:
>
>lasner@watsun.cc.columbia.edu (Charles Lasner) writes:
>
>> I suppose I could whip up something vaguely similar as long as we could
>> define some "ground rules" so that there is a level playing field.
>
>OK, I'll start.  Ground rules are:
>     o	Internal representation of strings etc is up to the programmer,
>	as long as he/she is consistent.
>     o	"Words" are the natural word size of the machine, ie 32bits for
>	VAX, 16 for pdp11, 12 for PDP-8, 8 for 6502, 36 for PDP-10 etc
>     o	The "operating system" has two system calls -- input & output.
>	input reads a single character from the console TTY and places it
>	in "the accumulator".  output writes the character in the 
>	accumulator onto the console TTY.  Characters are assumed to
>	occupy a whole word with bits above bit 6 cleared and are 
>	assumed to be ASCII.  The "accumulator" is the lowest numbered
>	register on multi-register machines (R0 for pdp11/VAX, D0 on 68k).
>	Routines should try not to use clever tricks or special
>	instructions.  (If a special instruction would make a dramatic
>	difference, demonstrate both.)  System calls use the shortest
>	available *subroutine* calling mechanism (again, be consistent).
>	The accumulator is always destroyed by a syscall.  <CR> is 
>	assumed to do a line feed also.  Input characters echo; you
>	don't need to echo yourself.
  So, for these two routines, would we have to use the conventions of the OS
on whatever machine we use (i.e. on an MS-DOS machine, something like 
	mov ah,_input
	int 21h
or just a simple 
	call input
)?

  I guess for the following examples, I'll assume two routines, one called
TTYINPUT and the other TTYOUTPUT, and use the native OS convention to call
these 'OS' routines 8-)

>     o	Routines must return to the "OS" using the same call/return 
>	mechanism as the syscalls.
>
>(I think this pretty provides a reasonable baseline for comparing toy
>problems on multiple architectures....)
>
>(1) Hello World.  Do whatever is required to put "Hello World!<CR>"
>on the console TTY.
>

----------------------------------------------------------

6809 code on the Tandy Color Computer:

HELLO	FCC	'HELLO WORLD!'	;TEXT TO OUTPUT
	FDB	$0D00

CODE10	JSR	[TTYINP]	;os routines are called via indirect
				;addressing
OUTP	LDA	,X+
	BNE	CODE10
	RTS			;this will return us to the OS 8-)
CODE	LDX	#HELLO
	BRA	OUTP
	
Instructions:	6
Code:		14 bytes
Data:		14 bytes
Total:		28 bytes, 28 8-bit words

---------------------------------------------------------

808[68] code on a typical PC (ick 8-)

hello	db	'Hello world!',13,0

code10:	int	21h
outp:	lodsb
	or	al,al
	jnz	code10
	ret
code:	mov	si,offset hello
	mov	ah,TTYINPUT
	jmps	outp

Instructions:	8
Code:		16 bytes
Data:		14 bytes
Total:		30 bytes, 15 16-bit words

-----------------------------------------------------------

68000 on a typical Amiga (Gee, I just love programming in Assembly, doesn't
everybody? 8-) 8-)

hello	dc.b	'Hello World!',13,0

code10	jsr	_LVOTTYINPUT(a6)	; call OS using Amiga convention
outp	move.b	(a0)+,d0
	bne.s	code10
	rts
code	move.l	4,a6			; get base address of OS
	lea	hello(pc),a0
	bra.s	outp

Instructions:	7
Code:		20 bytes
Data:		14 bytes
Total:		34 bytes, 17 16-bit words

------------------------------------------------------------

>
>(2) Octal to decimal converter.  Input as many octal digits as 
>required until CR.  Assume numbers smaller than word size are input,
>don't worry about invalid input.  Output decimal on TTY followed by CR.
>

6809 Version omitted because I'm too lazy to work out the DIVISION routine,
and the book that does have such a routine is at home, and I'm not 8-)

808[68] code on a typical PC

code:	xor	dx,dx
	mov	ah,TTYINPUT
code10:	int	21h
	cmp	al,13
	jz	code20
	and	al,07h
	add	dx,dx		;on a 286+, this can be replaced with a
	add	dx,dx		;shl dx,3, but for an 808[68], it's faster
	add	dx,dx		;with three adds!
	or	dl,al		;add in currently read digit
	jmps	code10		;continue ...
code20:	mov	ax,dx		;get value
	xor	cx,cx		;used to keep track of number of digits
	mov	bx,10		;base to use
code30:	xor	dx,dx		;clear out upper 16 bits
	div	bx		;32b/16b divide
	push	dx		;save character
	inc	cx		;count of chars
	or	ax,ax		;more?
	jnz	code30	
code40:	pop	ax		;get character
	add	al,'0'		;convert to ASCII
	mov	ah,TTYOUTPUT	;output
	int	21h
	loop	code40
	ret

Instructions:	26	(ick)
Code:		52 bytes (pretty sure there)
Data:		 0 bytes
Total:		52 bytes, 26 16 bit words

---------------------------------------------------------------------

68000 code, again on an Amiga ...

code	moveq	#0,d1
	move.l	4,a6
code10	jsr	_LVOTTYINPUT(a6)
	cmp.b	#13,d0
	beq.s	code20
	and.b	#7,d0
	lsl.w	#3,d1		;gee, I wish I had my 68K reference here 8-)
	or.b	d0,d1
	bra.s	code10
code20	moveq	#-1,d3
	move.w	#10,d2
code30	divu	d2,d1
	swap	d1
	move.w	d1,-(a7)
	addq.w	#1,d3
	sub.w	d1,d1
	swap	d1		;I'm hoping this affects condition codes 8-)
	bne.s	code30
code40	move.w	(a7)+,d0
	add.b	#'0',d0
	jsr	_LVOTTYOUTPUT
	dbf	d3,code40
	rts

Instructions:	22
Code:		62 bytes
Data:		 0 bytes
Total:		62 bytes, 31 16-bit words

------------------------------------------------------------------
>
>Anyone else want to show off their favourite architecture?  
>
>--
>Don Stokes, ZL2TNM (DS555)                        don@zl2tnm.gen.nz (home)
>Network Manager, Computing Services Centre            don@vuw.ac.nz (work)
>Victoria University of Wellington, New Zealand              +64-4-495-5052

  -spc (Disclaimer:  All this code is off the top of my head, written at the
	moment, and I'm sure that if given ample time, I may have done
	better (and may have even included some R3000 code 8-) 8-)

Newsgroups: alt.folklore.computers