Jump to content

Fixed point multiplication in ASM


Neil Pate

Recommended Posts

Almost totally unrelated to LabVIEW, and is not really just a question more of just a ramble.

I have dusted off a PIC microcontroller that I have had lying around in my drawer for years, and am attempting to stop old-age brain rot my making it do stuff, but purposely limiting myself to assembler.

So far I have the (12-bit) ADC working, and sending the upper 8 bits back to the PC over RS-232 as an ASCII character.

Now all I want to do is display the analogue voltage on the PC. Should be simple. I am using the 5V supply as the reference, so a 12-bit ADC has a step size of 1.22 mV. Conversion from 12-bit hex value to human readable value should be reasonably straightforward: scale the ADC value by 1.22 mV, convert to say 5 digit BCD and chuck out over the serial port one character at a time. I figure the first step is to implement a fixed point multiplication, keeping things simple I can have the ADC reading as <+,16,0> and the scaling factor as <+16, 3>.

 This is the bit that is making my brain hurt... I have done some reasearch and it turns out that fixed point multiplication does not really care about the format of the number, only the bit-width. So you can use exactly the same algorithm to scale <+16,0> and <+16,3>. Anybody know if this is correct?

Now, I know I can take all sorts of shortcuts to get the ADC value in human readable format, I have decided for the purpose of the exercise I really want to figure this out using fixed point maths (I have no actual application in mind other than curiosity).

 

Link to comment

 

6 hours ago, Neil Pate said:

 This is the bit that is making my brain hurt... I have done some reasearch and it turns out that fixed point multiplication does not really care about the format of the number, only the bit-width. So you can use exactly the same algorithm to scale <+16,0> and <+16,3>. Anybody know if this is correct?

Correct :)

A few useful notes:

  • Your 16-bit fixed point number is stored in memory exactly like a 16-bit integer*.
  • Fixed point multiplication == Integer multiplication. The same algorithms apply to both.
  • The algorithms for integer multiplication don't depend on the value of the number. By extension, they don't depend on the representation of the number either.
    • e.g. Multiplying by 2 is always equivalent to shifting the bits to the left (assuming no overflow)

(* LabVIEW actually pads fixed point numbers to 64 or 72 bits, but I digress)

 

Anyway, to illustrate:

 Bit Pattern (MSB first) | Representation                | Value (decimal) 
=========================|===============================|=================
 0000 0010               | <+,8,8> (or equivalently, U8) | 2
 0000 0010               | <+,8,7>                       | 1
 0000 0010               | <+,8,6>                       | 0.5
-------------------------|-------------------------------|-----------------
 0000 0100               | <+,8,8> (or equivalently, U8) | 4
 0000 0100               | <+,8,7>                       | 2
 0000 0100               | <+,8,6>                       | 1

 

 

6 hours ago, Neil Pate said:

  Almost totally unrelated to LabVIEW, and is not really just a question more of just a ramble.

That's borderline blasphemy... ;) You can redeem yourself by getting LabVIEW involved:

FXP Study.png

Edited by JKSH
Link to comment

Getting there slowly.. :-)

I have the 16-bit x 16-bit multiplication working using the 8-bit hardware multiplier.

Also I have fleshed out my LabVIEW demo to show how to solve the rest of my problem.

 

Convert (+,16,4) Fixed Point Number To String.vi

Convert First Decimal Place Of (+,16,4) Fixed Point Number To String.vi

Fixed Point Maths.vi

Capture.PNG

Edited by Neil Pate
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.