Divide 16bit number with a 16bit result

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Divide 16bit number with a 16bit result

2,293件の閲覧回数
Bloodhound
Contributor I
Hi all,
 
Coming from a couple of years programming on the 68332 I'm battling on the HC08 with trying to divide (by 4) a 16 bit number in RAM space and get a 16bit result. Anybody got any tips?
I did mess around a bit with the OPCode examples in the CPU08 manual but I don't seem to be getting valid results.
 
Thanks in advance,
Ross
ラベル(1)
0 件の賞賛
返信
2 返答(返信)

812件の閲覧回数
tonyp
Senior Contributor II
To divide by any power of 2, you only need to shift right as many times as needed.  For example,

Number    rmb    2
...
          lsr   Number
          ror   Number+1
          lsr   Number
          ror   Number+1

will divide a 16-bit number by 4 (two right shifts).

To divide by any 8-bit value, you can use the DIV instruction.  For example,

Number   rmb   2
...
         ldhx  #4      ;H=0 (always), X=divisor
         lda   Number
         div
         sta   Number
         lda   Number+1
         div
         sta   Number+1

(the example above writes over the original number. You could use the stack or other variable to save the quotient, instead).  Note: The divisor in this case does not need to be a power of two, it can be any value from 1 to 255 (with 0 giving an error.)

When done, register H will contain the final remainder of the multi-byte division.  You can use this method to divide any byte-lentgh numbers with an 8-bit number.

For larger divisor, you will have to use shift-and-subtract methods.


Message Edited by tonyp on 2007-12-10 12:35 PM
0 件の賞賛
返信

812件の閲覧回数
Bloodhound
Contributor I
Thanks TonyP, the first example worked perfect for me.
 
Cheers,
Ross
0 件の賞賛
返信