Link to home
Start Free TrialLog in
Avatar of Wij_man
Wij_man

asked on

Assembly - Adding Floating Point Numbers

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)

Please reply ASAP.

Thank you.

wij_man
Avatar of manish_regmi
manish_regmi


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

then do addition manually
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~

Avatar of Wij_man

ASKER

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
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
does this help: http://en.wikipedia.org/wiki/IEEE_754
please read the wiki article or know the anatomy of a float before reading the following.

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.
Avatar of Wij_man

ASKER

You have to understand I don't know very much about this stuff.

And more help would be great !!!

Thanks
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
Avatar of Wij_man

ASKER

Well it has to be 32bit, so 1.1111111.

If you refer to https://www.experts-exchange.com/questions/21945534/Floating-point-arithmatic-Assembly-Atmel-8515.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
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
Avatar of Wij_man

ASKER

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
Hi VJ,

What maths instructions does an atmel have?

Paul
Avatar of Wij_man

ASKER

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
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
Avatar of Wij_man

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Wij_man,

Could you put some comments in please. I dont read this language. :-)

Paul
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
Avatar of Wij_man

ASKER

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
I am assuming 32bit wide registers and using pseudo language.

define exp_mask = 0x7F800000
define mantissa_mask = 0x007FFFFF

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