Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Multiply Floats
- #a0 = Single precision float A
- #a1 = Single precision float B
- #v0 = Multiplcation result of A * B
- #t0 will hold the sign of a0
- #t1 will hold the sign of a1
- #t2 will hold the exponent of a0
- #t3 will hold the exponent of a1
- #t4 will hold the mantissa of a0
- #t5 will hold the mantissa of a1
- # 1)Store the sign
- # 2)Store the exponents
- # 3)Store the mantissas
- # 4)Calculate the final product sign
- # 5)Get the bias out of the exponent and add
- # 6)Multiply the mantissas
- # 7)Store in a new register
- # 8)Normalize
- #
- #
- #Normalize Floats
- #a0 = 1-bit sign bit right aligned
- #a1 - [63:32] of mantissa
- #a2 = [31:0] of mantissa
- #a3 = 8-bit biased exponent right alined
- #v0 = normalized FP result
- #t0 will hold number of leading zeros total
- #t1 will hold calculate on a2 IF NEEDED
- #t2 holds number 32 (max zeros in a1)
- #t3 will hold the number 18
- #t4 temp for conditional comparison
- #t5 will hold number to shift by
- #t6 will hold exponent
- #t7 is temporary register when shifting a2
- #t8 is 32 subtracted by number we are shifting by
- #s2 will hold 17
- # 1)Calculate number of zeroes
- # 2)Shift the first register by that much
- # 3)If the whole first register is zeros, though, also check the second
- # 4)Start shifting
- # 4.1)If needing to shift left on the right register, mask it to the end of the first
- # 5)Pack everything together
- #
- #CompareFloats
- # 1)Compare signs
- # 2)If signs are same, continue
- # 3)Both positive
- # 4)Larger exponent
- # 5)If exponents are same, larger mantissa
- # 6)Both negative
- # 7)Smaller exponent
- # 8)If both exponents are same, smaller mantissa
- #
- #PrintFloat
- # 1)Seperate the sign, mantissa, and exponent as above
- # 2)Shift each to the right most, bitmask one bit at a time, and print out
- #
- #
- #
- .data
- sign: .asciiz "SIGN: "
- exponent: .asciiz "EXPONENT: "
- newLine: .asciiz "\n"
- mantissa: .asciiz "MANTISSA: "
- .text
- #li $a0, 0x00000001
- #li $a1, 0x00000000
- #li $a2, 0x00000001
- #li $a3, 0x00000001
- #jal PrintFloat
- #jal CompareFloats
- #li $a0, 0x3B6BBBBB
- #li $a1, 0xC2F00000
- #lw $a0, test_A
- #lw $a1, test_B
- #li $a0, 0xCF000000
- #li $a1, 0x34800000
- #jal CompareFloats
- #li $a0, 0x3C0BBBBB
- #li $a1, 0x3C111111
- li $v0, 10
- syscall
- AddFloats:
- subi $sp, $sp, 4
- sw $ra, 0($sp)
- #a0 = Single precision float A
- #a1 = Single precision float B
- #v0 = Addition result of a0 + a1
- #t0 will hold the sign of a0
- #t1 will hold the sign of a1
- #t2 will hold the exponent of a0
- #t3 will hold the exponent of a1
- #t4 will hold the mantissa of a0
- #t5 will hold the mantissa of a1
- #t6 used for comparison
- move $t0, $a0
- srl $t0, $t0, 31
- move $t1, $a1
- srl $t1, $t1, 31
- #Signs done storing
- andi $t2, $a0, 0x7F800000
- srl $t2, $t2, 23
- andi $t3, $a1, 0x7F800000
- srl $t3, $t3, 23
- #Exponents done storing.
- andi $t4, $a0, 0x007FFFFF
- andi $t5, $a1, 0x007FFFFF
- ori $t4, $t4, 0x00800000
- ori $t5, $t5, 0x00800000
- #Mantissas done storing
- #Now time to figure out which exponent is smaller.
- #is the exponent of a0 smaller?
- slt $t6, $t2, $t3
- bgtz $t6, a0smaller
- slt $t6, $t3, $t2
- bgtz $t6, a1smaller
- #otherwise skip shifting
- j skipshift
- a0smaller:
- #t6 is now the difference in exponent
- sub $t6, $t3, $t2
- srlv $t4, $t4, $t6
- move $t2, $t3
- j skipshift
- a1smaller:
- sub $t6, $t2, $t3
- srlv $t5, $t5, $t6
- move $t3, $t2
- skipshift:
- #a0 holds sign
- move $a0, $t0
- #a1 holds first number
- add $a1, $t4, $t5
- li $a2, 0
- move $a3, $t2
- jal NormalizeFloat
- lw $ra, 0($sp)
- addi $sp, $sp, 4
- jr $ra
- MultFloats:
- subi $sp, $sp, 4
- sw $ra, 0($sp)
- #a0 = Single precision float A
- #a1 = Single precision float B
- #v0 = Multiplcation result of A * B
- #t0 will hold the sign of a0
- #t1 will hold the sign of a1
- #t2 will hold the exponent of a0
- #t3 will hold the exponent of a1
- #t4 will hold the mantissa of a0
- #t5 will hold the mantissa of a1
- move $t0, $a0
- srl $t0, $t0, 31
- move $t1, $a1
- srl $t1, $t1, 31
- #Signs done storing
- andi $t2, $a0, 0x7F800000
- srl $t2, $t2, 23
- andi $t3, $a1, 0x7F800000
- srl $t3, $t3, 23
- #Exponents done storing.
- andi $t4, $a0, 0x007FFFFF
- andi $t5, $a1, 0x007FFFFF
- #Mantissas done storing
- #t0 will hold final sign
- bgtz $t0, firstnegativesign
- bgtz $t1, secondnegativesign
- j continuetoexponent
- firstnegativesign:
- bgtz $t1, twonegatives
- li $t0, 1
- j continuetoexponent
- secondnegativesign:
- bgtz $t0, twonegatives
- li $t0, 1
- j continuetoexponent
- twonegatives:
- li $t0, 0
- continuetoexponent:
- #t2 will hold final exponent
- add $t2, $t2, $t3
- subi $t2, $t2, 127
- #Add hidden bit
- ori $t4, $t4, 0x00800000
- ori $t5, $t5, 0x00800000
- #t4 will hold high part of mantissa
- #t5 will hold low part of mantissa
- mult $t4, $t5
- mfhi $t4
- mflo $t5
- #now normalize and store
- move $a0, $t0
- move $a1, $t4
- move $a2, $t5
- move $a3, $t2
- jal NormalizeFloat
- lw $ra, 0($sp)
- addi $sp, $sp, 4
- jr $ra
- NormalizeFloat:
- subi $sp, $sp, 4
- sw $ra, 0($sp)
- #a0 = 1-bit sign bit right aligned
- #a1 - [63:32] of mantissa
- #a2 = [31:0] of mantissa
- #a3 = 8-bit biased exponent right alined
- #v0 = normalized FP result
- #t0 will hold number of leading zeros total
- #t1 will hold calculate on a2 IF NEEDED
- #t2 holds number 32 (max zeros in a1)
- #t3 will hold the number 18
- #t4 temp for conditional comparison
- #t5 will hold number to shift by
- #t6 will hold exponent
- #t7 is temporary register when shifting a2
- #t8 is 32 subtracted by number we are shifting by
- #s2 will hold 17
- li $s2, 17
- li $t4, 0
- li $t3, 18
- li $t8, 32
- li $t2, 32
- move $t6, $a3
- li $t5, 0
- clz $t0, $a1
- beq $t0, $t2, MoreZeros
- j NoMoreZeros
- MoreZeros:
- clz $t1, $a2
- add $t0, $t0, $t1
- NoMoreZeros:
- #Is the number of zeros less than 17?
- slt $t4, $t0, $s2 #will hold 1 if less than 17
- bgtz $t4, LessThan18
- slt $t4, $s2, $t0 #will hold 1 if greater than 17
- bgtz $t4, MoreThan18
- beqz $t4, skip
- j LessThan18
- LessThan18:
- move $t5, $t3
- sub $t3, $t3, $t0
- subi $t3, $t3, 1
- move $t5, $t3
- #shift right by t5
- j ShiftRight
- MoreThan18:
- #addi $t0, $t0, 1
- sub $t5, $t0, $s2
- #shift left by t5
- j ShiftLeft
- ShiftRight:
- #need to calculate new registers better
- srlv $a2, $a2, $t5
- move $t7, $a1
- sub $t8, $t8, $t5
- sllv $t7, $t7, $t8
- srlv $a1, $a1, $t5
- or $a2, $a2, $t7
- j IncreaseExponent
- ShiftLeft:
- #need to calculate new registers better
- sllv $a1, $a1, $t5
- move $t7, $a2
- sub $t8, $t8, $t5
- srlv $t7, $t7, $t8
- sllv $a2, $a2, $t5
- or $a1, $a1, $t7
- j DecreaseExponent
- skip:
- DecreaseExponent:
- sub $t6, $t6, $t5
- j StartPacking
- IncreaseExponent:
- add $t6, $t6, $t5
- j StartPacking
- #a0 = 1-bit sign bit right aligned
- #a1 - [63:32] of mantissa normalized
- #a2 = [31:0] of mantissa normalized
- #a3 = 8-bit biased exponent right alined
- #v0 = normalized FP result
- #t0 holds last 14 bits of a1
- #t1 holds first 9 bits of a2
- #t2
- #t3
- #t4
- #t5
- #t6 holds exponent(edited)
- StartPacking:
- move $v0, $a0
- sll $v0, $v0, 31
- #now MSB of v0 is the sign
- sll $t6, $t6, 23
- or $v0, $v0, $t6
- #t0 will hold last 14 bits of a1
- and $t0, $a1, 0x00003FFF
- sll $t0, $t0, 9
- or $v0, $v0, $t0
- #t1 will hold first 9 bits of a2
- andi $t1, $a2, 0xFF800000
- srl $t1, $t1, 23
- or $v0, $v0, $t1
- lw $ra, 0($sp)
- addi $sp, $sp, 4
- jr $ra
- CompareFloats:
- #a3 will hold temporarily the v0 value
- li $a3, 0
- li $v0, 0
- #t0 holds sign a0
- move $t0, $a0
- srl $t0, $t0, 31
- #t1 holds sign a1
- move $t1, $a1
- srl $t1, $t1, 31
- #t2 holds exponent a0
- move $t2, $a0
- srl $t2, $t2, 23
- andi $t2, $t2, 0x000000FF
- #t3 holds exponent a1
- move $t3, $a1
- srl $t3, $t3, 23
- andi $t3, $t3, 0x000000FF
- #t4 holds mantissa for a0
- move $t4, $a0
- andi $t4, $t4, 0x3FFFFF
- #t5 holds mantissa for a1
- move $t5, $a1
- andi $t5, $t5, 0x3FFFFF
- #Now storage is done.
- #IF the sign of a0 is less than the sign of a1,store 1
- slt $a3, $t0, $t1
- move $v0, $a3
- bgtz $v0, doneComparing
- #IF the sign of a1 is less than sign of a0, store -1
- slt $a3, $t1, $t0
- #if it is 1, then subtract 2. else, keep as 0.
- beqz $a3, isZero
- subi $a3, $a3, 2
- bltz $v0, doneComparing
- isZero:
- move $v0, $a3
- beqz $v0, dontJump
- jr $ra
- #Now if the sign is the same, we continue
- #Now we will compare exponents
- #If the exponent of a1 is less than that of a0, and both positive, store 1
- dontJump:
- beqz $t0, bothPositive
- bgtz $t0, bothNegative
- bothPositive:
- #Now both are positive. If exponent of a1 is less than that of a0, store 1
- slt $a3, $t3, $t2
- move $v0, $a3
- beqz $v0, dontJumpOne
- jr $ra
- #If exponent of a1 is greater than at of a0, store -1
- dontJumpOne:
- slt $a3, $t2, $t3
- beqz $a3, isZeroOne
- subi $a3, $a3, 2
- isZeroOne:
- move $v0, $a3
- beqz $v0, PositiveAndSameExponent
- jr $ra
- PositiveAndSameExponent:
- #If the mantissa of a1 is less than that of a0, store 1
- slt $a3, $t5, $t4
- move $v0, $a3
- #If mantissa of a1 is greater than that of a0, store -1
- slt $a3, $t4, $t5
- beqz $a3, isZeroTwo
- subi $a3, $a3, 2
- isZeroTwo:
- move $v0, $a3
- jr $ra
- bothNegative:
- #Now both are negative. If exponent of a1 is less than that of 0, store -1
- slt $a3, $t3, $t2
- bgtz $a3, a3isone
- j dontJumpTwo
- a3isone:
- sub $a3, $a3, 2
- move $v0, $a3
- beqz $v0, dontJumpTwo
- jr $ra
- dontJumpTwo:
- #If exponent of a1 is greater than that of a0, store 1
- slt $a3, $t2, $t3
- move $v0, $a3
- beqz $v0, NegativeAndSameExponent
- jr $ra
- NegativeAndSameExponent:
- #If the mantissa of a1 is less than that of a0, store -1
- slt $a3, $t5, $t4
- beqz $a3, dontJumpThree
- mantissaissmaller:
- subi $a3, $a3, 2
- move $v0, $a3
- beqz $v0, dontJumpThree
- jr $ra
- dontJumpThree:
- #If exponent of a1 is greater than that of a0, store 1
- slt $a3, $t4, $t5
- move $v0, $a3
- j doneComparing
- #If the exponent of a1 is greater than that of a1
- doneComparing:
- jr $ra
- #PrintFloat Function
- PrintFloat:
- # the number is stored in a0.
- # t0 has operations done on it
- # t4 holds the number
- move $t4, $a0
- move $t0, $t4
- srl $t0, $t0, 31
- la $a0, sign
- li $v0, 4
- syscall
- la $a0, ($t0)
- li $v0, 1
- syscall
- li $v0, 4
- la $a0, newLine
- syscall
- #---- Sign done. now exponent:
- move $t0, $t4
- srl $t0, $t0, 23
- andi $t0, $t0, 0x000000FF
- #storing the exponent in s0
- #number of digits in argument a1 - 1, number to shift by
- li $a1, 7
- move $s0, $t0
- la $a0, exponent
- li $v0, 4
- syscall
- j ExponentToBinary
- backfromexponent:
- li $v0, 4
- la $a0, newLine
- syscall
- #---- Exponent done. now mantissa:
- move $t0, $t4
- andi $t0, $t0, 0x007FFFFF
- li $a1, 22
- move $s0, $t0
- la $a0, mantissa
- li $v0, 4
- syscall
- j MantissaToBinary
- backfrommantissa:
- li $s0, 0
- jr $ra
- ExponentToBinary:
- bltz $a1, backfromexponent
- move $t0, $s0
- srlv $t0, $t0, $a1
- sub $a1, $a1, 1
- and $t0, $t0, 0x01
- li $v0,1
- la $a0, ($t0)
- syscall
- j ExponentToBinary
- MantissaToBinary:
- bltz $a1, backfrommantissa
- move $t0, $s0
- srlv $t0, $t0, $a1
- sub $a1, $a1, 1
- and $t0, $t0, 0x01
- li $v0,1
- la $a0, ($t0)
- syscall
- j MantissaToBinary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement