Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Assembly - Adding Floating Point Numbers

Posted on 2006-07-24
Medium Priority
4,395 Views
This is for some college work -

I need to add two floating point numbers using asm with an atmel.

I pretty much know how to add normal numbers, but totally stuck on floating point numbers.
Also I need to know how to assing a value to a memory address and pass it to a register (so it can be used to test my subroutine for adding)

Thank you.

wij_man
0
Question by:Wij_man
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 7
• 7
• 2
• +2

LVL 8

Expert Comment

ID: 17173445

>> I need to add two floating point numbers using asm with an atmel.
which, 8051 i guess.

It does not have floating point instructions. so you need to emulate one.

Most compilers for 8051 have built-in support for floating point. I used to  use Kyle Cx51.

what compiler/assembler are you using?

this might give u hint on how to emulate floating point.
http://www.cs.wisc.edu/~smoler/x86text/lect.notes/arith.flpt.html

regards
Manish Regmi

0

LVL 13

Expert Comment

ID: 17176643
use two register for ur adding

for number 1
ah = before decimal point
al =  after decimal point
for number 2
bh = before decimal point
bl =  after decimal point

00001111  11100000
00010111  10100000
-------------------------
better put ur answer into another register, or a variable

and following URL contains lots of assembly source code, try what u need
http://www.emu8086.com/dr/asm2html/assembler_source_code/

~marchent~

0

Author Comment

ID: 17182789
Sorry should have mentioned I'm using an Atmel AT90S8515.

And as for splitting the number before and after d.p how would I get it to split by itself ?

Also I need to know how to assing a value to a memory address and pass it to a register (so it can be used to test my subroutine for adding).

Thanks
0

LVL 8

Expert Comment

ID: 17182852
okey,
dont have much experience in AVR's. But here is a tutorial on how to use floating point in AVR.
http://www.avr-asm-tutorial.net/avr_en/calc/FPCONV.html

here is full code on how to do floating point in 8051 (though not AVR but might be some help)
http://www.8052.com/users/Jerson/

regards
Manish Regmi
0

LVL 9

Expert Comment

ID: 17238782
does this help: http://en.wikipedia.org/wiki/IEEE_754

1. extract and separate exponent and mantissa
2. combine sign and mantissa to get ints
3. shift bits around to align binary points (binary analogue of decimal point) of both numbers
4. perform addition of mantissae as with ints
5. if there are leading zeros or overflow shift left or right, respectively as required and adjust the exponent
6. combine exponent and mantissa and move the sign around to get your result.

that is the general idea. i think you can work your way through little kinks that you may find.
0

Author Comment

ID: 17263187

And more help would be great !!!

Thanks
0

LVL 16

Expert Comment

ID: 17332419
Hi Wij_man,

Sorry to take so long. :-)

There are many different formats for floating point numbers. What format do you wish to use?

In many cases you can get by with integers by storing the scaled (times 100 for currency is often sufficient). What sort of number range will these have? How many decimal places do you need?

Paul
0

Author Comment

ID: 17348084
Well it has to be 32bit, so 1.1111111.

If you refer to http://www.experts-exchange.com/Programming/Programming_Languages/Assembly/Q_21945534.html
there are another 320 points up for grabs. SAME QUESTION.

So help me and I'll give you both sets of points.

Mind you I have till tomorrow to have this done now.

I need to add and subtract two 32 bit floating point numbers !!! Using IEEE standard on a at90s board.

VJ
0

LVL 16

Expert Comment

ID: 17348665
Hi VJ,

You will need to choose a certain number of bits for exponent and mantissa and chose a number of bits for each. That is where the standard comes in.

The rest is just bit twiddling. I hope you still have time to do all this work. :-)

FYI - One question is not allowed to exceed 500 points. Would you like me to reduce the points on the other one?

Paul
0

Author Comment

ID: 17348832
Umm if you like.

The IEEE single precision floating point standard representation requires a 32 bit word, which may be represented as numbered from 0 to 31, left to right. The first bit is the sign bit, S, the next eight bits are the exponent bits, 'E', and the final 23 bits are the fraction 'F':

S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
0 1           8 9                                    31

and I'm seriously running out of time.

My major problem is that the tutor just didn't teach us very well.
I've researched as much as I can, I'm just desperate now !!

VJ
0

LVL 16

Expert Comment

ID: 17348914
Hi VJ,

What maths instructions does an atmel have?

Paul
0

Author Comment

ID: 17349079
http://users.informatik.uni-halle.de/~gabrisch/doc/AVR_Instructions.pdf#search=%22%22avr%20instruction%20set%22%22

It doesn't have a floating point handler.

it's a RISC microcontroller.

WJ
0

LVL 16

Expert Comment

ID: 17349145
Hi Wij_man,

Ok!

Start with the bit twiddling. You will need the bits out of the value in separate registers. Write that code and post it here and we'll check it and move forward to the maths stage.

Paul
0

Author Comment

ID: 17349161
well that would be something like (I THINK)

.include      "8515def.inc"

.def      num11      =r10
.def      num12      =r11
.def      num13      =r12
.def      num14      =r13

.def      num21      =r14
.def      num22      =r15
.def      num23      =r16
.def      num24      =r17
.def      count        =r18

.cseg

ldi num11,0x80                  ;Exponent
ldi num12,0x4b                  ;Mantissa
ldi num13,0x4b
ldi num14,0x4b

ldi num21,0x80
ldi num22,0x4b
ldi num23,0x4b
ldi num24,0x4b
0

LVL 16

Accepted Solution

PaulCaswell earned 500 total points
ID: 17349176
Hi Wij_man,

Incidentally, we are starting with jhshukla's suggestion:

>>1. extract and separate exponent and mantissa

and take a good look at manish_regmi's link:
>>http://www.8052.com/users/Jerson/Float51.asm

Your code will look a lot like part of that when it's done. I'll let you work out which part. ;-)

Paul
0

LVL 16

Expert Comment

ID: 17349184
Hi Wij_man,

Paul
0

LVL 16

Expert Comment

ID: 17349197
Hi Wij_man,

The code you posted only loads some registers with some values. now you need to separate out those bits into registers on their own.

Paul
0

Author Comment

ID: 17349253
Oh, well I don't know how to do that then.

I thought you just wanted me to load the registers and insert values of a floating point number.

WJ
0

LVL 9

Expert Comment

ID: 17351560
I am assuming 32bit wide registers and using pseudo language.

move number1 -> reg0
reg0 & exp_mask -> reg1                                                          ; get the exponent
right shift reg1 by 23 bits                                                          ; right align exponent
reg1 + 127 -> reg1                                                                  ; compensate for bias
reg0 & mantissa_mask -> reg2                                                   ; get the mantissa
reg2 | 0x00800000 -> reg2                                                       ; add the implicit 1 at the start
### Filter out special cases like NAN, extremely small numbers, etc. Insert code here ###
# use the same set of operations on number2 (regs 3, 4, 5)
reg1 - reg4 -> reg6
if ( reg6 >= 0 ) shift-right reg5 by reg6 bits                                  ; align the binary point (equi. decimal point)
else shift-right reg2 by -reg6 bits
reg0 | 0x80000000 -> reg2                                                       ; accommodate the sign
reg3 | 0x80000000 -> reg5                                                       ; accommodate the sign
max(reg1, reg4) -> reg8                                                           ; use the larger exponent
reg2 + reg5 -> reg9                                                                 ; add the mantissa
reg9 & 0x80000000 -> reg7                                                      ; extract the sign
while ( reg9 < 0x00800000 )                                                     ; accumulate how many bits you might have to adjust
reg10+1 = reg10
shift-left reg9
reg8 - reg10 -> reg8                                                                 ; adjust exponent
reg8 - 127 -> reg8                                                                    ; bias the exponent
; insert code for shift left reg8 by 23 bits
reg9 & 0x7F7FFFFF -> reg9                                                         ; clear the sign and implicit leading 1
reg7 | reg8 | reg9 -> reg11                                                        ; recombine the three parts to get the result
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on tâ€¦
Please read the paragraph below before following the instructions in the video â€” there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, â€¦
###### Suggested Courses
Course of the Month10 days, 22 hours left to enroll