| ??? 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 |



