	clra		;zero_extendqihi: R:b -> R:d
	clra	;andqi(ZERO)
	andb	#2
	cmpd	#0
	beq	L%2
=
	bitb	#2
	beq	L%2

Ltext0:
=
Ltext0: lbra    _main
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;;; ___divhi3 - signed division
        ;;; Arguments: Dividend in X, divisor on the stack
        ;;; Returns result in X.
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        .area .text
        .globl _divhi3
_divhi3:
        ldd     2,s
        bne     do_div          ; check dividend
        ; SIGFPE
do_div:
        pshs    x
        jsr     _seuclid
        puls    x,pc
;;; ___mulhi3 - signed/unsigned multiply
;;; Called by GCC to implement 16x16 multiplication
;;; Arguments: Two 16-bit values, one in stack, one in X.
;;; Result: 16-bit result in X
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	.area .text
	.globl _mulhi3
_mulhi3:
	pshs	x
	lda   5,s   ; left msb * right lsb * 256
	ldb   ,s
	mul
	tfr   b,a
	clrb
	tfr   d,x
	ldb   1,s   ; left lsb * right msb * 256
	lda   4,s
	mul
	tfr   b,a
	clrb
	leax  d,x
	ldb   1,s   ; left lsb * right lsb
	lda   5,s
	mul
	leax  d,x
	puls	d,pc  ; kill D to remove initial push
	.area .text
	.globl ___clzhi2
	; Input: X = 16-bit unsigned integer
	; Output: X = number of leading zeros
	; This function destroys the value in D.
___clzhi2:
	pshs	x
	; Find the offset of the leftmost '1' bit in
	; the left half of the word.
	;
	; Bits are numbered in the table with 1 meaning the
	; LSB and 8 meaning the MSB.
	;
	; If nonzero, then clz is 8-a.
	tfr	x,d
	ldx	#___clz_tab
	tfr	a,b
	clra
	ldb	d,x
	bne	upper_bit_set
lower_bit_set:
	; If the upper byte is zero, then check the lower
	; half of the word.  Return 16-a.
	puls	d
	clra
	ldb	d,x
	negb
	addb	#16
	bra	done
upper_bit_set:
	negb
	addb	#8
	puls	x
done:
	tfr	d,x
	puls	pc
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;;; ___modhi3 - signed modulo
        ;;; Arguments: Dividend in X, divisor on the stack
        ;;; Returns result in X.
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        .area .text
	CARRY=1			; alias
	count=0			; byte
        .globl _modhi3
_modhi3:
        ldd     2,s
        bne     do_mod          ; check dividend
        ; SIGFPE
do_mod:
        pshs    x
        jsr     _seuclid
        leas    2,s
        tfr     d,x
        rts
        .area   .text
        .globl  _euclid
        left=5
        right=1                 ; word
        count=0                 ; byte
        CARRY=1                 ; alias
_euclid:
	leas	-3,s		; 2 local variables
	clr	count,s		; prescale divisor
	inc	count,s
	tsta
presc:
	bmi	presc_done
	inc	count,s
	aslb
	rola
	bra	presc
presc_done:
	std	right,s
	ldd	left,s
	clr	left,s		; quotient = 0
	clr	left+1,s
mod1:
	subd	right,s		; check subtract
	bcc	mod2
	addd	right,s
	andcc	#~CARRY
	bra	mod3
mod2:
	orcc	#CARRY
mod3:
	rol	left+1,s	; roll in carry
	rol	left,s
	lsr	right,s
	ror	right+1,s
	dec	count,s
	bne	mod1
	leas	3,s
	rts
;	signed euclidean division
;	calling: (left / right)
;		push left
;		ldd right
;		jsr _seuclid
;	quotient on the stack (left)
;	modulus in d
	.area	.text
	.globl	_seuclid
	left=6
	right=2
	quot_sign=1
	mod_sign=0
_seuclid:
	leas	-4,s		; 3 local variables
	std	right,s
	clr	mod_sign,s
	clr	quot_sign,s
	ldd	left,s
	bge	mod_abs
	inc	mod_sign,s	; sign(mod) = sign(left)
	inc	quot_sign,s
	bsr	negd		; abs(left) -> D
mod_abs:
	pshs	b,a		; push abs(left)
	ldd	right+2,s	; all references shifted by 2
	bge	quot_abs
	dec	quot_sign+2,s	; sign(quot) = sign(left) XOR sign(right)
	bsr	negd		; abs(right) -> D
quot_abs:
	jsr	_euclid		; call (unsigned) euclidean division
	std	right+2,s
	puls	a,b		; quot -> D
	tst	quot_sign,s	; all references no longer shifted
	beq	quot_done
	bsr	negd
quot_done:
	std	left,s		; quot -> left
	ldd	right,s
	tst	mod_sign,s
	beq	mod_done
	bsr	negd
mod_done:
	leas	4,s		; destroy stack frame
	rts
negd:				; self-explanatory !
	nega
	negb
	sbca	#0
	rts
	.globl	___clz_tab
___clz_tab:
	.byte	1
	.byte	2
	.byte	3
	.byte	3
	.byte	4
	.byte	4
	.byte	4
	.byte	4
	.byte	5
	.byte	5
	.byte	5
	.byte	5
	.byte	5
	.byte	5
	.byte	5
	.byte	5
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	6
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	7
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
	.byte	8
