# [Coco] Mod10 Suggestions

msmcdoug msmcdoug at iinet.net.au
Sat Feb 18 02:43:32 EST 2017

```
Actually I'm surprised noone has suggested bcd arithmetic on the result to eliminate divide by 10 loop

Sent from my Samsung GALAXY S5

-------- Original message --------
From: William Astle <lost at l-w.ca>
Date: 18/02/2017  4:52 PM  (GMT+10:00)
To: coco at maltedmedia.com
Subject: Re: [Coco] Mod10 Suggestions

On 2017-02-17 07:48 PM, Brett Gordon wrote:
>    tfr a,b            no aax ;(
>   abx            accum even byte

A couple of points:

Since you know that A is going to be in the range of 0...9, you don't
need to use abx. You can simply use "leax a,x". Which also has the
benefit of being 1 byte shorter and 4 cycles faster.

Also, since the maximum possible sum from the algorithm is 216, you
should be able to do the mod calculation on B only. The only difference
would be that you'd be looking for a carry instead of negative after the
subtractions. That will be a real saving considering that that mod
calculation loop could run 21 times. (saves 1 cycle for each subtraction).

Here's how I arrived at 216: 8 of the digits are added as is and 8 of
them are doubled and added. Since no digit can be higher than 9, the
maximum value is 24*9 which is 216.

If doing position independent code (PIC), you can't use the "CMPY
#num+16" method of ending the loop. There is no reasonable way simulate
that. You would need to use a counter of some kind in that case. The
most efficient way to do that is a single byte counter somewhere,
possibly on the stack.

If you do decide to use absolute addresses for the data area, then I
would recommend using U for the pointer instead of Y just to save a byte
in the setup.

In case you're wondering, the typical priority order for using the index
registers is:

X
U
Y

Don't let the "user stack pointer" name for "U" make you think you
shouldn't use U as an index register. There is nothing magical about U
that will cause it to suddenly be used as a stack without your direct
intervention as the programmer.

The reason for the listed ordering is as follows. All operations on X
have a single byte opcode. LEAU, LDU and STU have single byte opcodes.
All operations on Y except LEAY have double byte opcodes. There is no
difference in size or speed when using any of the four pointer registers
(S is also a pointer register) in an indexed operand.

X or Y can both be used as 16 bit counters because LEAX and LEAY will
set Z. (LEAU and LEAS do not.) X would be preferred there in most cases
but if you have tension between index registers and counters, it comes
down to judgement.

X also has the ABX operation which can be useful so that can affect how
you allocate registers, too.

--
Coco mailing list
Coco at maltedmedia.com
https://pairlist5.pair.net/mailman/listinfo/coco
```