Solved

Assembly - Adding Floating Point Numbers

Posted on 2006-07-24
19
4,218 Views
Last Modified: 2008-01-09
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
0
Comment
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
  • Learn & ask questions
  • 7
  • 7
  • 2
  • +2
19 Comments
 
LVL 8

Expert Comment

by:manish_regmi
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

by:marchent
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

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~

0
 

Author Comment

by:Wij_man
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

by:manish_regmi
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

by:jhshukla
ID: 17238782
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.
0
 

Author Comment

by:Wij_man
ID: 17263187
You have to understand I don't know very much about this stuff.

And more help would be great !!!

Thanks
0
 
LVL 16

Expert Comment

by:PaulCaswell
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

by:Wij_man
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

by:PaulCaswell
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

by:Wij_man
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

by:PaulCaswell
ID: 17348914
Hi VJ,

What maths instructions does an atmel have?

Paul
0
 

Author Comment

by:Wij_man
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

by:PaulCaswell
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

by:Wij_man
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

by:
PaulCaswell earned 250 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

by:PaulCaswell
ID: 17349184
Hi Wij_man,

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

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
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

by:Wij_man
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

by:jhshukla
ID: 17351560
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
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question