Date: Mon, 21 Jan 2008 18:30:10 -0800
From: dwight elvey <dkelvey at hotmail.com>
I think the restore operation will cause a carry
meaning I need
to clear it here with something like:
ora a ; or other carry clearing instruction
I don't think so--let's consider a simple case, using 8 bits and -10.
-10 = 0xf6
Our code looks like:
add -10 to dividend
skip next instruction if carryout
subtract -10 from divident
The question is "what is the value of the carry bit here?"
Let's try some sample values.
0 + 0xF6 = 0xF6, no carry, subtract 0xF6 = 0, no carry/borrow
9 + 0xF6 = 0xFF, no carry, subtract 0xF6 = 9, no carry/borrow
10 + 0xF6 = 0x00, carry; skip the subtract
11 + 0xF6 = 0x01, carry; skip the subtract
So the "ora a" to clear the carry shouldn't be required. Saves a few
more cycles.
For those porting this method to other platforms, be aware that the
6502/PIC subtract works differently than the x80; the carry bit
represents "not borrow" instead of "borrow".
The same algorithm can be implemented nearly as efficiently on the
8085 by using the "undocumented" instructions, as it includes a
double-precision subtract (as well as a 16-bit arithmetic right
shift) as a 1-byte instruction. Note that the BC pair must be used
in place of DE to use this instruction.
It might be possible to implement the algorithm efficiently on the
8080 by letting BC = -DE and using an ADD HL,BC instead of a SBC
HL,DE. One is left without a loop counter register however--but then
one could "prime" the A register with a 1 and iterate until the 1
migrates to the sign bit of A, then mask it out as part of the exit
code.
Cheers,
Chuck