Lecture 6
Lecture 6
Floating-Point Operations:
The floating-point unit has 32 floating-point registers. These registers are numbered like the CPU
registers. In the floating-point instructions we refer to these registers as $f0, $f1, and so on. Each of
these registers is 32 bits wide. Thus, each register can hold one single-precision floating-point number.
How can we use these registers to store double precision floating-point numbers? Because these
numbers require 64 bits, register pairs are used to store them. This strategy is implemented by storing
double-precision numbers in even-numbered registers. For example, when we store a double-precision
number in $f2, it is actually stored in registers $f2 and $f3.
mov.s FRdest,FRsrc
This instruction copies a single-precision floating-point number from the FRsrc to the Frdest register. If
we want to copy a double-precision number, use mov.d instead. For e.g.
mov.d $f12,$f0
Above Instruction copies the double floating-point from $f0 to $f12 register.
Following Instructions load the single precision floating point variable into FP register and double
precision floating point variable into FP register respectively.
Similarly for Store instruction s.s and s.d are use for single and double precision floating point numbers
respectively, as shown below:
Following Instructions load the single precision floating point constant value into FP register double
precision floating point constant value into FP register respectively.
0.0
FRdest,FRsrc1,FRsrc2 For
Addition: add.s
FRdest,FRsrc1,FRsrc2 add.d
FRdest,FRsrc1,FRsrc2 For
Multiplication:
mul.s FRdest,FRsrc1,FRsrc2
mul.d
FRdest,FRsrc1,FRsrc2 For
Division: div.s
FRdest,FRsrc1,FRsrc2 div.d
FRdest,FRsrc1,FRsrc2
Example 1
"\nDiv = "
.text
la $a0, string1 li
$v0, 4 syscall
add.s $f3, $f1, $f2
li $v0, 2 syscall
la $a0, string2
li $v0, 4 syscall
$f2 mov.s
$f12, $f3
li $v0, 2 syscall
la $a0, string3 li
$v0, 4 syscall
li $v0, 2 syscall
la $a0, string4 li
$v0, 4 syscall
li $v0, 2 syscall
li $v0, 10
syscall
Example 2
.data
string1:.asciiz
"\nAdd = "
string2:.asciiz
"\nSub = "
string3:.asciiz
"\nMul = "
string4:.asciiz
"\nDiv = "
.text
la $a0, string1
li $v0, 4
$f12, $f4
li $v0, 3
syscall
la $a0, string2 li
$v0, 4 syscall
li $v0, 3
syscall
la $a0, string3 li
$v0, 4 syscall
li $v0, 3 syscall
la $a0, string4 li
$v0, 4 syscall
li $v0, 3 syscall
li $v0, 10 syscall
Example 3 User
Input
.data
.text
li $v0, 6 syscall
add.s $f12, $f0, $f1
li $v0, 2 syscall
Comparison:
▫ c.le.s $f2, $f4 if $f2 <= $f4 then code = 1 else code = 0
▫ c.lt.s $f2, $f4 if $f2 < $f4 then code = 1 else code = 0
Branches:
not Equal\n"
.text
true
la $a0, string2
li $v0, 4
syscall b exit
true: la $a0,
string1 li $v0,
4 syscall
exit: li
$v0, 10
syscall
Converti
ng
floating
point
number
to
integer:
.data
Array: .float 24.0,87.0,34.0,23.0,42.0,67.0,76.0,12.0,92.0,85.0
N: .float 10.0
.text
.globl main
main:
la $t0 , Array
l.s $f4 , N
l.s $f5 , 0($t0)
li $t5, 1
loop :
bc1t loop
syscall
li $v0 , 10
syscall
LAB TASK 11
(1) Write a program in MIPS to reverse the array without using another array. Array should be of
type double and it should contain only 10 values.
(2) Write a program that inputs an integer value and prints the number with its digits reversed. For
example, given the number 1234, the program should print 4321.