I want to add two floats number in MIPS this is the codes I
I want to add two floats number in MIPS, this is the codes I had, but it doesn\'t work. (1).Please help me to make it work. (2). Copy the original float values from float registers to general purpose registers. (3). Separte each exponent into an available general purpose register, then display their values. Thank you.
.data
number1: .float 7.5
number2: .float 32.75
.text
.globl main
main:
lwc1 $f2, number1
lwc1 $f4, number2
add.s $f6,$f2,$f4
li $v0,2
syscall
li $v0,10
syscall
Solution
Answer
Below code should work:
.data
number1a: 7
number1b: 5
number2a: 32
number2b: 75
.text
.globl main
main:
lw $t0, number1a
lw $t1, number1b
lw $t2, number2a
lw $t3, number2b
# extraction of sign, exponenet and mantissa
move $t6, $t0
andi $t6, $t6, 0x000FFFF #extracting 1st part of mantissa
ori $t7, $t7, 0x00100000 #adding prefix 1 to mantissa
#rest of the mantissa stays in register $t1
move $t8, $t2
addi $t8, $t8, 0x7FF00000 #extracting exponent to $t8
move $t9, $t2
andi $t9, $t9, 0x000FFFFF #extracting first part of mantissa
ori $t9, $t9, 0x00100000 #adding prefix 1 to mantissa
addu $t7, $t7, $t9 #adding first part of the mantissa
addu $t1, $t1, $t3 #adding rest of the mantissa
exp1:
sll $s4, $t9, 31 #copy lsb of m1
sll $s5, $t3, 31 #copy lsb of m2
srl $t9, $t9, 1 #shift first part of the mantissa
srl $t3, $t3, 1 #shift the rest of the mantissa
or $t9, $t9, $s4 #put lsb in m1
or $t3, $t3, $s5 #put lsb in m2
addiu $t8, $t8, 0x00100000 #increase exponent $t8
j exp_check
exp2:
sll $s4, $t7, 31 #copy lsb of m1
sll $s5, $t1, 31 #copy lsb of m2
srl $t7, $t7, 1 #shift first part of the mantissa
srl $t1, $t1, 1 #shift the rest of the mantissa
or $t7, $t7, $s4 #put lsb in m1
or $t1, $t1, $s5 #put lsb in m2
addiu $t6, $t6, 0x00100000 #increase exponent $t6
j exp_check
shift:
andi $t4, $t7, 0x00200000
beqz $t4, result
sll $s2, $t7, 31 #copy least significant bit of m1
srl $t7, $t7, 1 #shift right m1
srl $t1, $t1, 1 #shift right m2
or $t1, $t1, $s2 #put m1\'s lsb in m2 msb
add $t6, $t6, 0x00100000 #increase exp
j result
shift2:
andi $t4, $t7, 0x00100000
bnez $t4, result
srl $s3, $t1, 31 #copy most significant bit of m2
sll $t7, $t7, 1 #shift right m1
sll $t1, $t1, 1 #shift right m2
or $t7, $t7, $s3 #put m2\'s msb in m1 lsb
sub $t6, $t6, 0x00100000 #increase exp
result:
andi $t7, $t7, 0x000FFFFF #preserve mantissa, zero the rest(cut the prefix - one)
move $t0, $s1 #copy propoer sign
or $t0, $t0, $t6 #add exponent
or $t0, $t0, $t7 #add mantissa part1
b output
exp_check:
bgt $t6, $t8, exp1 #exponent $t8 smaller than $t6
bgt $t8, $t6, exp2

