From: cclist at
sydex.com
Date: Mon, 21 Jan 2008 18:30:10 -0800
From: dwight elvey
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.
Hi Chuck
This always confuses me. I guess the Z80 does the compliment
and +1 to the F6. That wouldn't create a carry. I always
get this mixed up. I usually just run the code a few times
with different values to see what it does.
I'll take your word on this one.
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".
That is most likely why I get mixed up. I've done a lot
of 6502 and 6800 stuff.
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.
Ok
Here goes with using your idea to double use the a register. Except
If I start it with 2, I can use the carry instead of the sign bit. What fun!
Div10:
ld a,#2
ld de,#-640d
divloop:
add hl,de
jr c, div1
sbc hl,de
div1:
rl a
jr nc,divloop
add hl,hl
ret
_________________________________________________________________
Climb to the top of the charts!?Play the word scramble challenge with star power.
http://club.live.com/star_shuffle.aspx?icid=starshuffle_wlmailtextlink_jan