Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/04/07 17:59
Modified:
  10/04/07 18:28

Read: times


 
Msg Score: +3
 +1 Informative
 +2 Good Answer/Helpful
#145409 - Log calculation in ASM
Responding to: ???'s previous message
1)Is there any way to caculate log (to the base 10) using assembly language in '51?

log2( x ) is easily calculated even in ASM, as follows.
Then, calculate
log10( x )
    = log2( x ) / log2( 10 )
    = log2( x ) * log10( 2 )
where log10( 2 ) = 0.30103

As of calculation of log2( x ), I'll show an example of the calculation of a 16-bit integer number, 1ABCH here.

First, represent the number in binary and find the left-most '1'.
1ABCH   0001 1010 1011 1100

The position (digit) of '1' is 12, where the right-most position is zero.
This number equals to the integer part of log2( x )
1000H   0001 0000 0000 0000   212
1ABCH   0001 1010 1011 1100
2000H   0010 0000 0000 0000   213

212 <     1ABCH     < 213
12  < log2( 1ABCH ) < 13

This number is also obtained by left shit until the carry gives '1'
Four times of left shit provides the carry of '1', in this case.
carry
      0001 1010 1011 1100  - 1ABCH
    0 0011 0101 0111 1000  - left-shift once
    0 0110 1010 1111 0000  - left-shift twice
    0 1101 0101 1110 0000  - left-shift third
    1 1010 1011 1100 0000  - left-shift fourth

Then, the position is calculated by subtracting the shift number from the number of total bits.
16 - 4 = 12


Now, calculate the decimal part of log2( x )
Assume decimal point after the carry of above result.
    1.1010 1011 1100 0000
     ^
   decimal point

This fixed point number equals to 1ABCH / 212
1ABCH * 24 / 216 = 1ABCH / 212

Also, log2 of this number gives the decimal part of log2( x )
log2( 1ABCH / 212 ) = log2( 1ABCH ) - 12

This number always falls between 1 and 2, regardless of the original number.
212 <    1ABCH    < 213
1   < 1ABCH / 212 < 2

Then, using look-up table, log2 of this number is obtained.
To apply look-up table, we don't need to use all digits of this number.
1) The first digit is always 1
2) Depending on the required precision, take the limited number of greater digits.

For example, the first eight digits of decimal part is applied to the look-up table.
1.1010 1011 1100 0000

The look-up table is given as
y = log2(x + 1) (0 <= x < 1)

Adding the above integer part and the result of look-up table (decimal part), the entire fixed point number of log2( x ) is obtained.

In this way, log10( x ) calculation is reduced to fixed point math using ASM.

Tsuneo

List of 30 messages in thread
TopicAuthorDate
1) Log computation, 2) LCD requirement            01/01/70 00:00      
   Yes            01/01/70 00:00      
      Why would you doubt it?            01/01/70 00:00      
      Yes            01/01/70 00:00      
         Doesn't have to be floating point            01/01/70 00:00      
            's the one            01/01/70 00:00      
               the good old Intel FP51 package            01/01/70 00:00      
         Should be in high level language, thanks..            01/01/70 00:00      
            you can steal it...            01/01/70 00:00      
               need floating-point log? - YES            01/01/70 00:00      
                  Fixed point ?            01/01/70 00:00      
                     RE: Fixed point ?            01/01/70 00:00      
                        And what is the input voltage range?            01/01/70 00:00      
                           RE: Aside: dB equivalent of voltage            01/01/70 00:00      
                              my point is...            01/01/70 00:00      
                                 Who remembers log tables?            01/01/70 00:00      
                              Already answered            01/01/70 00:00      
                        So use cB instead!            01/01/70 00:00      
                        Aside: dB equivalent of voltage            01/01/70 00:00      
   How to find log in assembly?            01/01/70 00:00      
      Hint            01/01/70 00:00      
      Math            01/01/70 00:00      
   Log calculation in ASM            01/01/70 00:00      
      Implementation and precision            01/01/70 00:00      
         Correct precision            01/01/70 00:00      
            Brilliant work            01/01/70 00:00      
               Absolutely!            01/01/70 00:00      
   Fixed-point log2, without lookup table.            01/01/70 00:00      
      To Mr Franck            01/01/70 00:00      
         Source for the algorithm.            01/01/70 00:00      

Back to Subject List