|
-
Dec 20th, 2001, 05:51 AM
#1
Thread Starter
Hyperactive Member
Anybody want to make this algorithm?...
I could probably do this myself but I'm feeling lazy so does anyone want to code this algorithm for me? 
I have two byte arrays and I want to implement long division, storing the result in a third byte array. Each element of the array contains a single digit (0-9).
Unfortunately the arrays are of variable size, so the length of each is held in a Long variable.
This probably involes carrying etc and I don't feel up to it right now. Post if you can think of a quick solution/shortcuts or complete solution.
There are 10 types of people in the world - those that understand binary, and those that don't.
-
Jan 2nd, 2002, 05:35 AM
#2
Addicted Member
I don't know if this will help any. It comes from a book I bought about 20 years ago when registers had 8 bits and there were only 8 of them. The code is zilog assembler (similar to intel - they just changed the mnemonics).
I have always thought this was clever, the way that the result is passed into the registers that originally held the dividend and the divisor - this may change your thinking on using a third byte array.
Perhaps this could be adapted to your needs?, although I suspect what you are looking for is a coded way of 'working it out by hand'.
DDBYE - 8 bit unsigned division
Subroutines - None.
Length - 19.
Stack - 4.
Input - Dividend in D, divisor in E.
Output - Quotient in D, remainder in E.
Registers changed - D E.
Method - Binary long division by shifting and subtraction at the appropriate place. The dividend is shifted left out of the result register D as the partial Quotient is shifted in from the right. No check is made for division by zero.
DDBYE: PUSH AF :save registers
PUSH BC :use B as 8-bit loop
LD B, 8 :counter and clear A
XOR A :for dividend shift -in.
DDELP: SLA D :multiply part quotient by
RLA :two, next dividend bit moved
CP E :into A. Skip if divisor
JR C, DDELPC ;won't go else subtract and
SUB E ;set bit in partial quotient
INC D ;at correct place.
DDELPC: DJNZ DDELP ;repeat for all places.
LD E,A ;move remainder to E.
POP BC ;restore
POP AF ;registers
RET ;and return.
argh! it's made a mess of my neat columns.
There are three labels down the left hand side (col 1)
col2 goes 'push af/ push bc/ ld b,8/xor a/sla d etc.
col3 starts with the colon before the comments.
-
Jan 2nd, 2002, 10:14 PM
#3
Frenzied Member
There is a post somewhere which describes the division algorithm used by the CPU. Search for it. It might give you some ideas.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|