Floating-point Representation
Floating-Point Data Declarations
Floating-point values are defined with the .float (32-bit) or .double (64-bit) directives. The IEEE floating-point format is used for the internal representation of floating-point values. The following declarations are used to define the floating-point variables "pi" to a 32-bit floating-point value initialized to 3.14159 and "tao" to a 64-bit floating-point value initialized them to 6.28318.
pi: .float 3.14159
tao: .double 6.28318
1.
.data
fnum1: .float 6.28318
fnum2: .float 3.14159
fans1: .float 0.0
fans2: .float 0.0
dnum1: .double 42.3
dnum2: .double 73.6
dans1: .double 0.0
dans2: .double 0.0
.text
.globl main
main :
li.s $f1,1.0 # $f1 = constant 1.0
li.s $f2,2.0 # $f2 = constant 2.0
li.s $f10,1.0e-5 #$f10 = 0.00001
add.s $f3,$f1,$f2
s.s $f3,fans1
l.d $f4, dnum1
l.d $f6, dnum2
add.d $f8, $f4, $f6
s.d $f8, dans1 # dans1 = fnum1 + fnum2
#Done, terminate program.
li $v0, 10 # call code for terminate
syscall # system call
.end main
2.
.data
a: .float 1.0
bb: .float 1.0
c: .float 1.0
prompt: .asciiz "Enter x:"
blank:.asciiz " "
newl: .asciiz "\n"
.text
.globl main
main:
la $a0,prompt
li $v0,4 # print string "Enter X:"
syscall
li $v0,6 # Read float number from users: but for double use $v0,7
syscall
l.s $f2,a # Load single precision float value
mul.s $f2,$f2,$f0
l.s $f4,bb
add.s $f2,$f2,$f4
mul.s $f2,$f2,$f0
l.s $f4,c
add.s $f2,$f2,$f4
mov.s $f12,$f2
li $v0,2
syscall
la $a0,newl
li $v0,4
syscall
li $v0,10
syscall
3. # Program to find the Area of Circle
.data
pi: .float 3.142
radius: .float 0.0
area: .float 0.0
.text
.globl main
main:
li $v0,6
syscall
s.s $f0,radius
l.s $f1,radius
mul.s $f2,$f1,$f1
l.s $f3,pi
mul.s $f3,$f2,$f3
mov.s $f12,$f3
li $v0,2
syscall
li $v0,10
syscall
.end main