! We define a function, decodeBinary, which converts a string
! representation of a binary string into its numeric value. This
! function does NOT handle overflows or negatives.
! Parameters: a pointer to the first character of a string
! Returns: a word representing the value. This should probably be
! 	interpreted as an unsigned integer (in case the number is big enough
!	to leak into the sign bit)

	.global decodeBinary
decodeBinary:
	save %sp, -96, %sp
	ldub [%i0], %l1		! %l1 will store the current
				! character
	clr %l0			! %l0 will store our result
	tst %l1			! see if we have an empty string
	bz done			! if so, return immediately
	inc %i0			! go onto the next character [DS]
loop:	sub %l1, '0', %l1	! convert to digit [DS]
	andcc %l1, ~1, %g0	! check if every bit but the
				! rightmost one is zero.
				! we're being very tricky here! since the
				! only valid characters are '0' and '1'
				! (0 and 1 when converted from ASCII), all
				! we care about is whether everything but
				! the rightmost bit is 1. You could do
				! this trick for base 8, but it would be
				! hard for base 10 or 16! Probably you
				! guys will have to do 'cmp's and things
				! to figure out of the character was in
				! range.
	bnz error
	sll %l0, 1, %l0		! multiply sum by the base (2 in this
				! case) [DS]
	add %l0, %l1, %l0	! add on the current digit
	ldub [%i0], %l1		! read in the next character
	tst %l1			! see if we've hit the end of
				! the string
	bnz loop		! if not null character, loop
	inc %i0			! move to the next character [DS]
done:	ret
	restore %l0, %g0, %o0	! return %l0
error:
	set errormsg, %o0
	call printf
	nop
	ret
	restore %g0, %g0, %o0	! return 0

	.data
errormsg:
	.asciz "bad number\n"
