MC9S12GC32 Assembly Code Division

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MC9S12GC32 Assembly Code Division

1,596 Views
uCRFun
Contributor I

I'm working on an assembly project where I need to do division and make use of the remainder when the remainder is less than 1.  For example 5113/5 which "on paper" equal 1022.6.  However the result of this division inside the micro is Y=3FE (1022) and D=3 (which represents .6)  I'm aware that the fractional portion of a quotient is stored as a binary weighted value but I' trying to understand how I can manipulate the 3, in this case, in such a way that it gets stored as an integer 6 so that I can ultimately effectively multiply the result of 1022.6 by 10 to get 10226.  Anyone have any ideas?

 

Thanks,

uCRFun

Labels (1)
0 Kudos
Reply
3 Replies

579 Views
kef
Specialist I

Something is not right here. 5113/5 should return 0x9999 for binary remainder, not 3. The meaning of 0x9999 is that 0x9999 / (2^16) gives about 0.6.

 

To convert 0x9999 into 6 you should multiply 0x9999 by decimal 10 and divide by 2^16. 0x9999 * 10 = 0x5FFFA. In this case you will get 5, not 6. It's becouse of rounding. 0xFFFA (0.9999) is very close to 2^16 (1.0). To round you can add 0x8000 (0.5) to product before dividing it by 0x10000.

0 Kudos
Reply

579 Views
uCRFun
Contributor I

Thanks for the response KEF.  Prior to posting this I had submitted a service request with Freescale (I needed a solution as quickly as possible) and before I could respond to your response someone from tech support contacted me.

 

I don't quite follow all of what you've suggested in terms of the remainder being 0x9999 not 3 because 3 is what I see in the D register when I step through that code segment.  I'm not doubting what you say, I just don't follow completely. 

 

On the other hand, tech support indicated that 3 is what should be in the D register and to convert that result to an integer I can work with I could multiply it by 10 and the divide it by 5.  The end result of doing that is 6. Yeah!!  It's easy enough now to multiply the original quotient by 10 and add 6 to get 10226

 

Thanks

0 Kudos
Reply

579 Views
kef
Specialist I

Sorry, I mixed remainder with fractional part few times. I see why you have 3 now :smileyhappy:.

 

So you do integer division 5113/5 and get 3FE and remainder 3. Fractional part, 0.6 is 3/5. You should do one more division to get that fractional part. Binary weighted fraction in this case is  3 * 0x10000 / 5. It's 0x9999 I mentioned before.

 

It was not clear why were you talking about weighted fraction. Of course you can simply rearrange order of operations, multiply by 10 then divide instead of divide then multiply by 10.

0 Kudos
Reply