Jump to content
IGNORED

How to do a signed div?


Perra

Recommended Posts

I'm using a variant of the following code

 

<code>

 

;normal binary division
; 8bit/8bit
TEMPH .EQU	$FF
TEMPL .EQU	$FE
;---------------------------------------------
; DIVIDE -- TEMPL remainder A = X/Y
; from public domain code by Todd P. Whitesel
;---------------------------------------------
DIVIDE
STY	 TEMPH
STX	 TEMPL
LDX	 #$08
LDA	 #$00
ASL	 TEMPL
DIV1
ROL	 A
CMP	 TEMPH
BCC	 DIV2
SBC	 TEMPH
DIV2
ROL	 TEMPL
DEX
BNE	 DIV1

 

 

However I'm now in need of a signed division. I could do some logic around this division to two complement it, keeping track of negative numbers and convert them to positives. I'm my case I also knows that the divisor is always positive.

Link to comment
Share on other sites

Looked at the link

Large Multiply & Divide, looks like it is using unsigned numbers (looked into the code)

Division (32-bit), according to comments in the code its unsigned

FIG FORTH 6502 has a unsigned 16 bit division in source. However its poorly commented and I cannot find if there is a signed div that uses the unsigned part for the calculation.

 

And the rest of the sources doesn't look relevant, so no luck there. However I might have missed something.

 

I could use my routine and just check the first bit and if its set then two complement it and remember to two complement the result. Would work, and I would be glad if someone told me that that's the way to go. (I'm planning to do up to 32 divs per frame and currently up to over 70 scan lines for the divs, adding the signed part will increase the division time)

 

BR,

Perra

Link to comment
Share on other sites

If you used a melody board (e.g. Harmony) on the 2600 you could do the divides on the ARM at 70Mhz. Although ARM7 doesn't have a divide instruction it'll compute the results far, far quicker than the 6502.

 

If you want to stick with pure 6502 have you thought about different algorithms or approaches that could use table based computations instead?

Link to comment
Share on other sites

In my routine I know that only the dividend could be negative. Using the ARM is kind of cheating at this moment. I have considering table driven, however I still dont know any algorithms.

 

However I wrote the following code as an addition to the code I found.

 

 

;idea for signed (only checks the dividend since I know thats the only that could be signed in my case)

 stx temp
 txa
 bpl .not_singed
.signed

 eor #$ff
 tax
 inx
.not_singed

;the division routine from above
STX TEMPL
[...]
BNE DIV1

 ldx temp
 bpl .no_twocompl
 eor #$ff
   ;clc should not be needed, the last ROL should roll in a 0
 adc #$01
.no_twocompl

 

If the divident is positive the routine adds 14 cycles, and another 8 cycles if the dividen is negative.

 

I'm not that picky with the result, so I can remove the adc, given the average addition of 17 cycles (14+6/2) on my division. That's fine for now, I thought the logic around it should have taken more time...

 

The code above is untested, ill have to test it at home later...

Link to comment
Share on other sites

Worked like a charm...

 

It should be possible to extend it to check if both dividend and divisor is negative, then two complements them, and two complements the result unless both were positive or negative.

 

But I got my problem solved, and its up to someone else if they want to finish the example above to a fully working signed division.

 

My Project is a bit of a secret at the moment, but I plan to release something for the Silly venture party, December 2012. Hopefully I will release a Demo with source.

 

BR,

Perra

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...