Skip to content
This repository was archived by the owner on Dec 23, 2025. It is now read-only.
This repository was archived by the owner on Dec 23, 2025. It is now read-only.

MC6800 improve __xdiveqc/uc, __xremeqc/uc, add divide8x8.s #177

@zu2

Description

@zu2

support6800/divide8x8.s

;
;	B = A / B unsigned
;	A = modulo
;
;
	.export div8x8
div8x8:
	stab @tmp3
	ldab #8
	stab @tmp3+1		; loop count
	tab
	clra
div88_0:
	rolb
	rola
	suba @tmp3
	bcc div88_1
	adda @tmp3
div88_1:dec @tmp3+1
	bne div88_0
	rolb
	comb
	rts			; B:result, A:modulo

support6800/__xdivequc.s

;
;
;
	.export __xdivequc
	.export __xremequc
;
;	(0,X) = (0,X) / B unsigned
;
__xremequc:
	ldaa 0,x		; check dividend sign
	jsr div8x8		; B = A / B ,  A:remainder
	tab
	stab 0,x		; return B and 0,X as remainder
	rts
;
;	(0,X) = (0,X)/B unsigned
;
__xdivequc:
	ldaa 0,x		; check diviend sign
	jsr div8x8		; B = A / B,  A: remainder
	stb 0,x			; return B and 0,X as quotient
	rts

support6800/__xdiveqc.s

;
;	(0,X) = (0,X) / B signed
;
;	The rules for signed divide are
;	Dividend and remainder have the same sign
;	Quotient is negative if signs disagree
;
;	So we do the maths in unsigned then fix up
;
	.export __xdiveqc
	.export __xremeqc
;
;	The sign of the remainder of a division is not defined until C99.
;	C99 says it's the sign of the dividend.
;
__xremeqc:
	ldaa 0,x		; check dividend sign
	bpl xremeqc_0
	nega			; make dividend positive
xremeqc_0:
	tstb			; check divisor sign
	bpl xremeqc_1
	negb			; make divisor positive
xremeqc_1:
	jsr div8x8		; B = A / B ,  A:remainder
	tab
	ldaa 0,x		; check dividend sign bit
	bpl xremeqc_2
	negb			; remainder has the same sign as the dividend
xremeqc_2:
	stab 0,x		; return B and 0,X as remainder
	rts
;
;	(0,X) = (0,X)/B signed
;
;	The sign of the result is positive if the inputs have the same
;	sign, otherwise negative
;
__xdiveqc:
	ldaa 0,x		; check diviend sign
	bpl xdiveqc_0
	nega			; make dividend positive
xdiveqc_0:
	tstb			; check divisor sign
	bpl xdiveqc_1
	negb			; make divisor positive
	com 0,x			; To reduce the size, use 0,X as the sign flag.
xdiveqc_1:
	jsr div8x8		; B = A / B,  A: remainder
	ldaa 0,x		; check sign flag
	bpl xdiveqc_2
	negb			; if signs disagree, negate quotient
xdiveqc_2:
	stb 0,x			; return B and 0,X as quotient
	rts

test program:

int	main(int argc, char **argv)
{
	signed char	a,b;
	unsigned char	c,d;

	a = 127;
	a /= -128;
	if (a!=0)
		return 1;
	a = -128;
	a /= -128;
	if (a!=1)
		return 2;
	b = -128;
	b /= -128;
	if (b!=1)
		return 3;
	b = 127;
	b /= -128;
	if (b!=0)
		return 4;
	b = 127;
	b /= -1;
	if (b!=-127)
		return 5;
	b = -128;
	b /= -1;
	if (b!=-128)
		return 6;


	c = 127;
	c /= 128;
	if (c!=0)
		return 11;
	c = 128;
	c /= 128;
	if (c!=1)
		return 12;
	d = 128;
	d /= 128;
	if (d!=1)
		return 13;
	d = 127;
	d /= 128;
	if (d!=0)
		return 14;
	d = 255;
	d /= 127;
	if (d!=2)
		return 15;
	d = 255;
	d /= 1;
	if (d!=255)
		return 16;

	a = 127;
	a %= -128;
	if (a!=127)
		return 21;
	a = -128;
	a %= -128;
	if (a!=0)
		return 22;

	b = -128;
	b %= 49;
	if (b!=-30)
		return 23;
	b = 127;
	b %= -128;
	if (b!=127)
		return 24;
	b = 127;
	b %= -1;
	if (b!=0)
		return 25;
	b = -128;
	b %= -1;
	if (b!=0)
		return 26;

	c = 127;
	c %= 128;
	if (c!=127)
		return 31;
	c = 128;
	c %= 128;
	if (c!=0)
		return 32;
	d = 128;
	d %= 127;
	if (d!=1)
		return 33;
	d = 127;
	d %= 49;
	if (d!=29)
		return 34;
	d = 255;
	d %= 127;
	if (d!=1)
		return 35;
	d = 255;
	d %= 1;
	if (d!=0)
		return 36;

	return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions