简体   繁体   中英

In MIPS Assembly, how do I truncate to two decimal places if I have a floating point value X.YZDEF in a floating point register?

If I have a value in an "f" register in MIPS, how do I truncate this down to X.YZ from X.YZDEF? Supposedly, you must convert from the float to two ints and display those... How is this done?

You may want to look and see if these links will help you.

http://en.wikipedia.org/wiki/MIPS_architecture#MIPS_Assembly_Language

http://chortle.ccsu.edu/AssemblyTutorial/index.html#part8

You may also find this helpful: http://www.uni-koblenz.de/~avolk/MIPS/Material/MIPSFloatingPointInstructions.pdf

It has been a long time since I did assembly programming, but, if you multiply by 100 < mul.s >, then you will copy the number to an integer register, then if you divide by 100 < div > then you will have just the two digits on the right. The number to the left of the decimal will be in LO and the number to the right should be in HI, I expect.

The easiest thing to do is:

  1. multiply the value by 100 ( mul.d ),
  2. round to an integer, ( round.ld ),
  3. convert back to floating point ( cvt.dl ), and
  4. divide by 100 ( div.d ).

In order to implement a truncation (not rounding) in MIPS, you can do the following

# Note: The number you want to truncate is in $f12

##### Load 100 #####
li $t5,100                              # t5 = 100 (word),       t5 (word)
mtc1 $t5,$f5                            # f5 = t5  (word),       f5 (word)
cvt.s.w $f5,$f5                         # f5 = wordToSingle(f5), f5 (single)

##### Multiply f12(single) by 100 (single) #####    
mul.s $f12,$f12,$f5                     # f12 = f12 (single) * f5 (single), f12 (single)

##### Truncate single to word #####
trunc.w.s $f12,$f12                     # f12 = truncWordToSingle(f12 (single)), f12 (word)

##### Convert from word to single #####
cvt.s.w $f12,$f12                       # f12 = convertWordToSingle(f12 (word)), f12 (single)

##### Divide by 100 #####
div.s $f12,$f12,$f5                     # f12 = f12 (single) / f5 (single), f12 (single)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM