使用AVR汇编程序添加两个32位浮点数 [英] Add Two 32 bit Floating Point Numbers with AVR-Assembler

查看:109
本文介绍了使用AVR汇编程序添加两个32位浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AVR Studio将两个32位浮点数加在一起。我知道我需要将32位数字存储在4个单独的8位寄存器中。然后,我需要使用进位标记将寄存器加在一起。到目前为止,这就是我所拥有的。我添加了5.124323和2.2134523。

 ; 5.124323(01000000101000111111101001110100)
;存储十六进制值(40A3FA74)
ldi r21,$ 40
ldi r22,$ A3
ldi r23,$ FA
ldi r24,$ 74


; 2.2134523(01000000000011011010100100110100)
;存储十六进制值(400DA934)
ldi r25,$ 40
ldi r26,$ 0D
ldi r27,$ A9
ldi r28,$ 34


;将包括进位标志在内的相应字节加在一起
add r21,r25
adc r22,r26
adc r23,r27
adc r24,r28

这似乎并没有给我正确的答案...我敢肯定,这里唯一的错误是最后四行代码中的寄存器顺序。或者,也许add / adc函数也是错误的。有人可以帮我解决这个问题吗?

解决方案

将近4年后,您没有直接收到该问题的答案,而你应得的。这样我们就可以了:



您不能获取浮点数,而只是将它们相加以获得结果的浮点数,它们不是十进制或十六进制数字。

p>

浮点数值系统使很多人感到恐惧,这是一种不同的表示数值的方式,对于使用巨大或微观数字进行计算非常有用。我们在小学,中学,甚至大学都没有学到这一点。没有袖珍的Casio计算器可以处理FP编号,因此我们对这些数字并不是很熟悉,我们一个人在黑暗中。



大多数程序员(如果不是全部),宁愿让FP由C / C ++编译器处理,他们也不用逐位接触FP,因为有些人喜欢瘟疫。无论如何,FP是由人为创造的(真正的问题),即使如此,也有一种解决方法。



如果您按照下面的说明进行操作,我的目的不是教您如何在AVR组装中进行操作,即使这样做是完全可能且容易的。我的目的是表明老虎不是怪物,它是一只蓝眼睛的小猫,但它仍然是有牙齿和利爪的猫科动物。



在下面您将找到:



1。浮点数的胆量

2。轻松浮点乘法

3。浮点加法(您的问题)



胆量:



具有32位和64位的单精度和双精度FP,可以剥离32位。



FP编号具有3个局部区域:





a)符号,1位,#32(左侧第一个),零正,一个为负。

b)指数,8位,这是老虎狩猎的地方,人们非常困惑。

c)尾数,23位,指数分数互补。



为了能够表示非常大或非常小的数字,中心将为1,因此,当指数的第7位(称为 7E)为一,数字等于或大于1。 7E = 1的数字> = 1,7E = 0的数字小于1(例如0.7)。



您无需将7E视为实数在考虑指数的位值时,有些人会这样做。从现在开始,我们将只处理大于1且为正的数字,因此我不会评论7E或Sign位,甚至不会将其视为指数的值。



指数和尾数均为二进制计数。指数位的值与常规二进制数相同,不同之处在于,该位的值始终加1,E1 = 2,E2 = 3,E2 + E1 = 4,E3 = 5 ...所有位E1到E6 on = 128。



指数表示的值为2 ^ n,其中 n是如上所述的位或位组合的值。最小值将是2 ^ -126,这是一个很小的小数字,最大值将是2 ^ 128,这是一个视为无穷大的庞大数字,2 ^ 127有39个数字。

  E7.6.5.4.3.2.1.0 
1 0 0 0 0 0 0 0 =十进制0,但是请记住,它需要加1,所以它是1,然后值是2 ^ 1 = 2

E7.6.5.4.3.2.1.0
1 0 0 0 0 0 0 1 =十进制1,+1 = 2,2 ^ 2 = 4
1 0 0 0 0 0 1 0 =十进制2,+1 = 3,2 ^ 3 = 8
1 0 0 0 0 0 1 1 =十进制3,+1 = 4,2 ^ 4 = 16
1 0 0 0 0 1 0 0 =十进制4,+1 = 5,2 ^ 5 = 32
1 0 0 0 0 1 0 1 =十进制5,+1 = 6, 2 ^ 6 = 64
1 0 0 0 0 1 1 0 =十进制6,+1 = 7,2 ^ 7 = 128
1 0 0 0 0 1 1 1 =十进制7,+1 = 8,2 ^ 8 = 256
1 0 0 0 1 0 0 0 =十进制8,+1 = 9,2 ^ 8 = 512
1 0 0 1 0 0 0 0 =十进制16,+ 1 = 17,2 ^ 17 = 131072
1 0 0 1 0 0 0 1 =十进制17,+1 = 18,2 ^ 18 = 262144

观察到,将指数加1乘以2。

取反,则为1,将指数除以2。



10000100 = 32,减去1,即为10000011 =16。

p>

此外,向左移动一位并加1,将指数自身乘以N ^ 2。
例如10000010 = 8,向左移动10000100 = 32,加上1 10000101 = 64 = 8 ^ 2。

因此,向左移动= N * N / 2。



仅当位E1(指数的最后一位)为1时,情况为逆,如果情况为10000101(64)减去1并右移将得到平方根10000010(8)。对于10001011(4096)减去1并向右移动的结果相同,则为10000101 =64。如果E1 = 0,则结​​果无效,如果10000110(128)减去1,则10000101并向右移位的情况变为10000010,即8,如果仅移动而不减去就变为10000011即16,两者均无效。



加强构想:

0000-1110,二进制表示2 ^ 3 + 2 ^ 2 + 2 ^ 1 = 8 + 4 + 2 = 14.

x000-1110(以FP指数表示)2 ^(2 ^ 3 + 2 ^ 2 + 2 ^ 1 +1)= 2 ^(14 + 1)= 2 ^ 15 =32768。



但是您会看到指数只能形成2 ^ n的值,而不能形成中间值,例如5、12、15、257等。这就是尾数的作用。它存储指数的分数,为2 ^ -n,其中 n是尾数位值。



FP位23是尾数位1,因此其值是2 ^ -1,即1/2或0.5。
FP位22是尾数位2,其值2 ^ -2、1 / 4或0.25,依此类推。



当您在负数n上加上负号作为2的指数时,与使用该值除以1相同。2 ^ 3 = 8,2 ^ -3 = 1/8。



但尾数不是数字,它是指数的被乘数。
例如,FP位23的尾数M1 10000000 ...不是0.5,这意味着指数值应加自己的Half。为了更好地理解它,尾数23位都小于1,它们是0.nnnn。基于此,我们需要在其前面插入一个不可见的整数1,因此,尾数1000000 ...即0.5变为1.5。这意味着指数必须乘以1.5



因此,FP 0-10000001-1000000000 ....表示指数4,乘以Mantisse 1.5 =6。

如果FP 0-10000001-110000000 ...表示4 x(1 + 0.5 + 0.25)= 4 x 1.75 = 7

 第23位= 0.5(左位)
第22位= 0.25
第21位= 0.125
第20位= 0.0625
第19位= 0.03125

...
...
位1 = 0.0000001192092896(FP 1位)

现在,相同的尾数0.75(1100000 ....)将始终将指数乘以1.75,无论其值如何。数字6、12、24共享相同的尾数 100000 ....,即0.5,因为它乘以4x1.5 = 6、8x1.5 = 12、16x1.5 = 24等。



这的重要性是什么?



2。容易的浮点乘法



看,如果您有两个数字,且使用相同的尾数,则意味着两个数字将具有相同的分数被乘数。
如果要乘以这些数字,只需乘以指数,将尾数移至右边即可。



例如,将6乘以24。 >
6的2 ^ n指数为4,尾数为0.5

24的2 ^ n指数为16,尾数也为0.5

然后,6 x 24 = 144,再除以1.5,再除以1.5 = 64,即正好是4x16。
由于有两个尾数,所以除以1.5两次,将等于除以144 /(1.5 * 1.5)= 144 / 2.25 = 64.

因此,乘法结果的新尾数将为0.5 * 0.5 = 0.25或0.5 /2。



好奇心,144的FP是什么?

0-10000110-0010000000000 ....

指数10000110是什么意思?右边的 000110位= 6 +1 = 7,2 ^ 7 = 128

001000000的尾数表示什么? 1/8或0.125。

因此,128 * 1.125 = 144 ...整齐。



6的指数乘以24的指数如何成为128的指数?

6的指数是4 0-10000001-xxxxxxxxxx

24的指数为16 0-10000011-xxxxxxxxxx



指数处的每个加数意味着将其乘以2

到16的指数变成1000-0011到1000-0100(32)。

我们需要将16乘以4,我们需要加上2位,它将从1000-0011变成1000-0100 (第一位),然后到1000-0101(第二位)=64。

由于两个尾数相同,因此将其右移相同数量的指数乘法,即2位。 >
从100000000 ...到001000000 ...

然后,结果FP将 6 FP 0-10000001-1000000 ...乘以 24 FP 0-10000011- 1000000 ...是144 FP 0-10000101-0010000000 ...指数= 128乘以尾数1.125 =144。



如果尾数不同,则技巧也不同



3。浮点加法(您的问题)



首先让我们添加简单数字6和8 = 14

  6 = FP 0-10000001-1000000000 ... 
8 = FP 0-10000010-0000000000 ....
14 = FP 0-10000010-1100000000 ....

14的指数是其下整数2 ^ n,即8

14的尾数是8的小数,它们试图构成6的差。

6/8 = 0.75,即0.5 + 0.25 $ b的组成$ b因此,14的尾数将为11000000000。...

但是它如何以二进制形式发生?



有规则。

首先,如果第一个指数小于第二个指数,则减去并取绝对差(AD)并右移较低尾数的AD位。



6的指数是10000001,8的指数是10000010,差是1,所以将6的尾数右移一位。请记住,10000尾数是0.5,但实际上是1.5,所以有一个不可见的整数1。现在您将需要在两个尾数之前插入此1,并向右尾移6 ONE位,即差。



6的尾数为1 = 1.10000000 ....

8的尾数为1等于1.00000000 ....



右移1位尾数为6 = 0.110000000

8位尾数保持不变= 1.000000000



现在同时添加两者

  0.1100000000 
1.0000000000
-= --------- +
1.1100000000

现在,重要的是整数1必须正好在该位置,不能在左边(1x.xxxxx ...)或零。如果发生这种情况,有一些技术可以解决。



现在,原始整数1.xxx消失了,无论如何它是不可见的,最后的尾数变为110000000...。



新指数将更大,在这种情况下为8的指数,即10000010。

这是规则,而指数较大或尾数相加则不以大于1的整数结尾,则新指数与较大的指数相同。如果相加结果的整数大于1,则它将为2(10.xxxx〜),然后将1加到指数,然后在整个结果尾数上向右移动一位,因此它变为1.xxxx〜。我在这篇文章的结尾处举了一个例子。



现在,仅需对指数+尾数进行合成:

0-10000010-11000000 ...是FP的14,加6 + 8.



您能否单独在下面添加自己的FP编号?

 您的数字A = 5.1243230 FP 0-10000001-01000111111101001110100 
您的数字B = 2.2134523 FP 0-10000000-00011011010100100110100
结果A + B = 7.3377753 FP 0 -....

A指数= 4 ,10000001

B指数= 2,10000000

它们之间的差为1

因此,在两个尾数前都包括整数1,并向右移1

  A = 1.01000111111101001110100 
B = 1.00011011010100100110100

向右移1位B

  A = 1.01000111111101001110100 
B = 0.10001101101010010011010

同时添加

  A = 1.01000111111101001110100 
B = 0.10001101101010010011010
----------------------- ------- +
1.11010101100111100001110

整数继续1,完美。

现在,伊利敏吃Integer并使用此尾数作为结果

与较大的指数4串联,10000001变为

FP = 0-10000001-1101010110011110000111000 ...

恰好是7.3377753的FP。



(后来添加了以下内容)

添加位为10.xxxx〜的尾数的示例>

假设您要添加7 + 2 = 9

  7 FP 0-10000001 -11000〜
2 FP 0-10000000-00000〜

7的指数大于2,而差是1。
因此,让我们在两个尾数前插入隐式整数1:

  7尾数:1.110000〜
2尾数:1.000000〜

左移较小(2)尾数右移一位

  7尾数:1.110000〜
2尾数:0.100000〜
---- ------------------- +
结果:10.010000〜

请参见,有一个2(位10.xxx〜)作为结果尾数的隐式整数。如前所述,此隐式1代表指数的一倍。
现在,这2个(位10.xx〜)代表指数的两倍。
我们需要将这多余的ONE时间转移回指数,将指数加1,实际上就是将指数乘以2。这样做,我们需要将尾数除以2,然后将其右移一位



因此,所得的尾数10.010000〜将变为1.001000〜

最终FP将为0-10000010-0010000〜,即9。



容易,不是吗?



现在在汇编AVR中进行操作,每个FP仅使用4个寄存器(32位),另外4个寄存器用于答案(感谢Peter捕捉到 3错字)。


Im trying to use AVR Studio to add two 32bit floating point numbers together. I know that I will need to store the 32bit number in 4 separate 8bit registers. I'll then need to add the registers together using the carry flag. This is what I have so far. Im adding 5.124323 and 2.2134523.

;5.124323 (01000000101000111111101001110100)
;Store hex value (40A3FA74)
ldi r21,$40
ldi r22,$A3
ldi r23,$FA
ldi r24,$74


;2.2134523 (01000000000011011010100100110100)
;Store hex value (400DA934)
ldi r25,$40
ldi r26,$0D
ldi r27,$A9
ldi r28,$34


;Add the corresponding bytes together including the carry flag
add r21,r25
adc r22,r26
adc r23,r27
adc r24,r28

This doesnt seem to give me the right answer... I'm pretty sure the only thing that's wrong here is the order of the registers in the last 4 lines of code. Or maybe the add / adc functions are also wrong. Can someone please help me out with this?

解决方案

After almost 4 years you didn't receive a straight answer for the question, and you deserve it. So here we go:

You can not get Floating Point bits and simply ADD them to get the Floatting Point of the result, they are not decimal or hexadecimal numbers.

Floating Point numeric system scares a lot of people, it is a different way to represent numeric values, very useful to calculate with gigantic or microscopic numbers. We do not learn that at elementary school, not a middle school, not even at college. There is no pocket Casio calculator that can handle FP numbers, so we are not really very familiar with those, and we are all alone in the dark.

Most programmers (if not all), prefer to leave FP to be dealt by C/C++ compilers, they don't touch FP bitwise, for some it is like a plague. Anyway, FP was created by humans (the real problem), even so, there is a way to deal with it.

My intention here is not to teach you how to do it in AVR assembly, even that it is totally possible and easy, if you follow my explanations below. My intentions here is to show "the tiger" is not a monster, it is a little blue eyes petcat, but still, it is a feline with teeth and claws.

Below you will find:

1. The guts of Floating Point
2. Easy Floating Point Multiplication Easy
3. Floating Point Addition (your question)

The Guts:

The are single and double precision FP, 32 and 64 bits, lets strip the 32.

A FP number has 3 districtive areas:

a) Sign, 1 bit, #32 (first at left), zero for positive, one for negative.
b) Exponent, 8 bits, here is where the tiger hunts, people get very confused.
c) Mantissa, 23 bits, the exponent fractions complements.

To be able to represent a very large or very small number, the center will be 1, so, when bit 7 of the Exponent (lets call it "7E") is one, the number is 1 or bigger. 7E=1 number is >=1, 7E=0 number is smaller than 1 (0.7 for example).

You don't need to count 7E as a real bit with value when thinking about the bits value of the Exponent, some people do. From now on here, we will only deal with numbers largers than 1 and positive, so I will not comment about 7E or Sign bits, nor even account it as a value of the Exponent.

The Exponent and Mantissa are binary counted. The values of the Exponent bits are the same as a regular binary number, the difference is that the bit value is always added of 1, E1=2, E2=3, E2+E1=4, E3=5... All bits E1 to E6 on = 128.

The value of the Exponent representation is 2^n, where "n" is the value of bits or combination of bits as above. The minimum will be 2^-126 a very small tinny number, and the maximum will be 2^128 a gigantic gargantuous number considered infinity, 2^127 has 39 digits.

E7.6.5.4.3.2.1.0   
 1 0 0 0 0 0 0 0 = decimal 0, but remember, it needs to add 1, so it is 1, and the value is 2^1 = 2

E7.6.5.4.3.2.1.0   
 1 0 0 0 0 0 0 1 = decimal 1, +1 = 2, 2^2 = 4   
 1 0 0 0 0 0 1 0 = decimal 2, +1 = 3, 2^3 = 8   
 1 0 0 0 0 0 1 1 = decimal 3, +1 = 4, 2^4 = 16   
 1 0 0 0 0 1 0 0 = decimal 4, +1 = 5, 2^5 = 32   
 1 0 0 0 0 1 0 1 = decimal 5, +1 = 6, 2^6 = 64   
 1 0 0 0 0 1 1 0 = decimal 6, +1 = 7, 2^7 = 128   
 1 0 0 0 0 1 1 1 = decimal 7, +1 = 8, 2^8 = 256   
 1 0 0 0 1 0 0 0 = decimal 8, +1 = 9, 2^8 = 512   
 1 0 0 1 0 0 0 0 = decimal 16, +1 = 17, 2^17 = 131072   
 1 0 0 1 0 0 0 1 = decimal 17, +1 = 18, 2^18 = 262144   

Observe that adding 1 to the Exponent multiply its value by 2.
The inverse is true, subtracting 1, divide the Exponent by 2.

10000100 = 32, subtracting 1, becomes 10000011 = 16.

Also, shifting one bit to the left and adding 1, multiply Exponent by itself, as N^2. For example, 10000010 = 8, shifting left 10000100 = 32, plus 1 10000101 = 64 = 8^2.
So, shifting left = N * N/2.

The inverse is only true if bit E1 (last bit of Exponent) is 1, case of 10000101 (64) subtracting one and shifting right will give you the square root, 10000010 (8). The same for 10001011 (4096) subtracting one and shifting right becomes 10000101 = 64. If E1=0, results invalid, case of 10000110 (128) subtracting one, 10000101 and shifting right becomes 10000010 that is 8, if only shifting without subtracting becomes 10000011 that is 16, both invalid.

Reinforcing the idea:
0000-1110 in binary means 2^3 + 2^2 + 2^1 = 8 + 4 + 2 = 14.
x000-1110 in FP Exponent means 2^(2^3 + 2^2 + 2^1 + 1) = 2^(14+1) = 2^15 = 32768.

But you see the Exponent can only form values that are 2^n, not intermediate ones, like 5, 12, 15, 257, etc. That is where the Mantissa works. It stores fractions of the Exponent, as 2^-n, where this "n" is the Mantissa bits values.

FP bit 23 is Mantissa bit 1, so its value is 2^-1, that is 1/2, or 0.5.
FP bit 22 is Mantissa bit 2, its value 2^-2, 1/4, or 0.25, and so on.

When yout put a negative sign on the "n" as exponent of 2, it is the same as use this value to divide 1. 2^3=8, 2^-3=1/8.

But the Mantissa value is not numeric, it is a multiplicand fraction of the Exponent. For example, FP bit 23 Mantissa M1 10000000... is not 0.5, it means that the Exponent value should be added by its own Half. To understand it better, Mantissa 23 bits are all smaller than 1, they are 0.nnnn. Based on that, we need to insert a invisible integer 1. in front of it, so, Mantissa 1000000... that is 0.5 becomes 1.5. It means that the Exponent must be multiplied by 1.5

So, FP 0-10000001-1000000000.... means exponent 4, multiplied by Mantisse 1.5 = 6.
If FP 0-10000001-110000000... means 4 x (1 + 0.5 + 0.25) = 4 x 1.75 = 7

Bit 23 = 0.5  (left bit)   
Bit 22 = 0.25   
Bit 21 = 0.125    
Bit 20 = 0.0625   
Bit 19 = 0.03125   

...   
...   
Bit 1 = 0.0000001192092896 (bit FP 1)  

Now, same Mantissa of 0.75 (1100000....) will always multiply the Exponent by 1.75, no matter the value of it. Number 6, 12, 24 shares the same Mantissa of "100000...." that is 0.5, because it multiplies 4x1.5=6, 8x1.5=12, 16x1.5=24, etc.

What is the importance of that?

2. Easy Floating Point Multiplication Easy

See, if your have two numbers, with the SAME Mantissa, means both numbers will have the same multiplicand of fraction. If you want to multiply those numbers, just multiply the Exponent, divide the Mantissa shifting to the right.

For example, multiplying 6 by 24.
The 2^n Exponent of 6 is 4, Mantissa is 0.5
The 2^n Exponent of 24 is 16, Mantissa is also 0.5
Then, 6 x 24 = 144, dividing by 1.5 and again by 1.5 = 64, that is exactly 4x16 .
Divided twice by 1.5 because there are two mantissas, will be the same as dividing 144 / (1.5 * 1.5) = 144 / 2.25 = 64.
So, the new Mantissa of the multiplication result will be 0.5 * 0.5 = 0.25, or 0.5 / 2.

Curiosity, what is the FP for 144?
0-10000110-0010000000000....
what is the Exponent 10000110 means? bits at the right "000110" = 6 +1 = 7, 2^7=128
what the Mantissa of 001000000... means? 1/8 or 0.125.
So, 128 * 1.125 = 144... neat.

How Exponent of 6 multiplied by Exponent of 24 becomes Exponent of 128?
Exponent of 6 is 4 0-10000001-xxxxxxxxxx
Exponent of 24 is 16 0-10000011-xxxxxxxxxx

Each added bit at Exponent means multiplying it by 2
So, adding a bit to the Exponent of 16 turns 1000-0011 to 1000-0100 (32).
We need to multiply 16 by 4, we need to add 2 bits, it will turns from 1000-0011 to 1000-0100 (first bit) then to 1000-0101 (second bit) = 64.
As the two Mantissas are the same, just shift right it the same quantity of bits of the Exponent multiplication, 2 bits.
From 100000000... to 001000000...
Then, the result FP of multiplying "6" FP 0-10000001-1000000... by "24" FP 0-10000011-1000000... is 144 FP 0-10000101-0010000000... Exponent=128 multiplied by Mantissa 1.125 = 144.

If the Mantissas are different, the technique is different.

3. Floating Point Addition (your question)

Lets first Add easy numbers, 6 and 8 = 14

6  = FP 0-10000001-1000000000...   
8  = FP 0-10000010-0000000000....   
14 = FP 0-10000010-1100000000.... 

The Exponent of 14 is its lower integer 2^n, that is 8
The Mantissa of 14 is the fractions of 8 that try to compose the difference that is 6.
6/8 = 0.75, that is a composition of 0.5 + 0.25
So, the Mantissa of 14 will be 11000000000....
But how it happens in binary form?

There are rules.
First, If the first Exponent is smaller than the second, subtract and take the absolute difference (AD) and shift right AD bits of the Mantissa of lower number.

Exponent of 6 is 10000001, exponent of 8 is 10000010, difference is 1, so shift right one bit the Mantissa of 6. Remember that Mantissa 10000.... is 0.5, but in real is 1.5, so there is this invisible integer 1. Now you WILL need to insert this 1 in front of both mantissas, and shift right mantissa of 6 ONE bit, the difference.

Mantissa of 6 with 1. = 1.10000000....
Mantissa of 8 with 1. = 1.00000000....

Shift right 1 bit mantissa of 6 = 0.110000000
Mantissa of 8 stil the same = 1.000000000

Now ADD both

0.1100000000    
1.0000000000   
--=--------- +   
1.1100000000   

Now, it is important that the integer 1. must be exactly in that position, it could not be to the left (1x.xxxxx...) or zero in its place. If it happens, there are techniques to fix that.

Now, the original integer 1.xxx disappear, it is invisible anyway, and the final Mantissa becomes 110000000....

The new Exponent will be the bigger, in this case the exponent of the 8, 10000010.
This is the rule, while there is a larger exponent or the addition of the mantissas does not ended with integer bigger than 1, the new exponent is the same as the bigger one. If the integer of the adding result is bigger than 1, it will be 2 (10.xxxx~) then just add 1 to the Exponent and shift one bit right the whole resulting Mantissa, so it becomes 1.xxxx~. I put an example at the end of this post.

Now it is just a matter to composite Exponent + Mantissa:
0-10000010-11000000... is the FP of 14, adding 6 + 8.

Can you do alone the add of your own FP numbers below?

Your number A = 5.1243230 FP 0-10000001-01000111111101001110100   
your number B = 2.2134523 FP 0-10000000-00011011010100100110100   
Result A + B  = 7.3377753 FP 0-....  

A Exponent = 4, 10000001
B Exponent = 2, 10000000
Difference between them is 1
So, include integer 1. in front of both mantissas, and shift right 1 bit the mantissa of B.

A = 1.01000111111101001110100   
B = 1.00011011010100100110100   

Shift right 1 bit B

A = 1.01000111111101001110100   
B = 0.10001101101010010011010 

Add both

A = 1.01000111111101001110100   
B = 0.10001101101010010011010   
------------------------------ +   
    1.11010101100111100001110   

Integer continues 1, perfect.
Now, eliminate the Integer and use this Mantissa as result
Concatenate with the larger Exponent, of 4, 10000001 becomes
FP = 0-10000001-1101010110011110000111000...
That is exactly the FP of 7.3377753.

(The following was added later)
Example of adding resulting Mantissa with bits 10.xxxx~

Suppose you are adding 7 + 2 = 9

7 FP  0-10000001-11000~
2 FP  0-10000000-00000~

Exponent of 7 is bigger than 2 and the difference is 1. So lets insert the implicit integer 1 in front of both Mantissas:

7 Mantissa:  1.110000~
2 mantissa:  1.000000~

Shift smaller (2) Mantissa right one bit and add both.

7 Mantissa:  1.110000~
2 mantissa:  0.100000~
----------------------- +
Resulting:  10.010000~

See, there is a 2 (bits 10.xxx~) as the implicit integer of the resulting Mantissa. As said before, this implicit 1 represent one time the Exponent. Now, this 2 (bits 10.xx~) represent TWO times the Exponent. We need to transfer this extra ONE time back to the Exponent, adding 1 to the Exponent, that is in fact multiplying the exponent by 2. By doing so, we need to divide the Mantissa by 2, shifting it right one bit.

So, the resulting Mantissa 10.010000~ will become 1.001000~
The final FP will be 0-10000010-0010000~ that is 9.

Easy, isn't it?

Now lets do it in Assembly AVR, usando only 4 registers (32 bits) for each FP and another 4 registers for the answer (thanks Peter for catching the "3" typo).

这篇关于使用AVR汇编程序添加两个32位浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆