Advertisement
Guest User

Untitled

a guest
May 9th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #Multiply Floats
  3. #a0 = Single precision float A
  4. #a1 = Single precision float B
  5. #v0 = Multiplcation result of A * B
  6. #t0 will hold the sign of a0
  7. #t1 will hold the sign of a1
  8. #t2 will hold the exponent of a0
  9. #t3 will hold the exponent of a1
  10. #t4 will hold the mantissa of a0
  11. #t5 will hold the mantissa of a1
  12. #   1)Store the sign
  13. #   2)Store the exponents
  14. #   3)Store the mantissas
  15. #   4)Calculate the final product sign
  16. #   5)Get the bias out of the exponent and add
  17. #   6)Multiply the mantissas
  18. #   7)Store in a new register
  19. #   8)Normalize
  20. #
  21. #
  22. #Normalize Floats
  23. #a0 = 1-bit sign bit right aligned
  24. #a1 - [63:32] of mantissa
  25. #a2 = [31:0] of mantissa
  26. #a3 = 8-bit biased exponent right alined
  27. #v0 = normalized FP result
  28. #t0 will hold number of leading zeros total
  29. #t1 will hold calculate on a2 IF NEEDED
  30. #t2 holds number 32 (max zeros in a1)
  31. #t3 will hold the number 18
  32. #t4 temp for conditional comparison
  33. #t5 will hold number to shift by
  34. #t6 will hold exponent
  35. #t7 is temporary register when shifting a2
  36. #t8 is 32 subtracted by number we are shifting by
  37. #s2 will hold 17
  38. #   1)Calculate number of zeroes
  39. #   2)Shift the first register by that much
  40. #   3)If the whole first register is zeros, though, also check the second
  41. #   4)Start shifting
  42. #       4.1)If needing to shift left on the right register, mask it to the end of the first
  43. #       5)Pack everything together
  44. #
  45. #CompareFloats
  46. #   1)Compare signs
  47. #   2)If signs are same, continue
  48. #   3)Both positive
  49. #       4)Larger exponent
  50. #       5)If exponents are same, larger mantissa
  51. #   6)Both negative
  52. #       7)Smaller exponent
  53. #       8)If both exponents are same, smaller mantissa
  54. #
  55. #PrintFloat
  56. #   1)Seperate the sign, mantissa, and exponent as above
  57. #   2)Shift each to the right most, bitmask one bit at a time, and print out
  58. #
  59. #
  60. #
  61. .data
  62.     sign:   .asciiz "SIGN: "
  63.     exponent: .asciiz "EXPONENT: "
  64.     newLine: .asciiz "\n"
  65.     mantissa: .asciiz "MANTISSA: "
  66.  
  67. .text
  68.     #li $a0, 0x00000001
  69.     #li $a1, 0x00000000
  70.     #li $a2, 0x00000001
  71.     #li $a3, 0x00000001
  72.     #jal PrintFloat
  73.     #jal CompareFloats
  74.     #li $a0, 0x3B6BBBBB
  75.     #li $a1, 0xC2F00000
  76.     #lw  $a0, test_A
  77.     #lw  $a1, test_B
  78.     #li $a0, 0xCF000000
  79.     #li $a1, 0x34800000
  80.     #jal CompareFloats
  81.     #li $a0, 0x3C0BBBBB
  82.     #li $a1, 0x3C111111
  83.  
  84.     li $v0, 10
  85.     syscall
  86.    
  87.     AddFloats:
  88.         subi $sp, $sp, 4
  89.         sw $ra, 0($sp)
  90.         #a0 = Single precision float A
  91.         #a1 = Single precision float B
  92.         #v0 = Addition result of a0 + a1
  93.         #t0 will hold the sign of a0
  94.         #t1 will hold the sign of a1
  95.         #t2 will hold the exponent of a0
  96.         #t3 will hold the exponent of a1
  97.         #t4 will hold the mantissa of a0
  98.         #t5 will hold the mantissa of a1
  99.         #t6 used for comparison
  100.         move $t0, $a0
  101.         srl $t0, $t0, 31
  102.         move $t1, $a1
  103.         srl $t1, $t1, 31
  104.         #Signs done storing
  105.         andi $t2, $a0, 0x7F800000
  106.         srl $t2, $t2, 23
  107.         andi $t3, $a1, 0x7F800000
  108.         srl $t3, $t3, 23
  109.         #Exponents done storing.
  110.         andi $t4, $a0, 0x007FFFFF
  111.         andi $t5, $a1, 0x007FFFFF
  112.         ori $t4, $t4, 0x00800000
  113.         ori $t5, $t5, 0x00800000
  114.         #Mantissas done storing
  115.         #Now time to figure out which exponent is smaller.
  116.         #is the exponent of a0 smaller?
  117.         slt $t6, $t2, $t3
  118.         bgtz $t6, a0smaller
  119.         slt $t6, $t3, $t2
  120.         bgtz $t6, a1smaller
  121.         #otherwise skip shifting
  122.         j skipshift
  123.         a0smaller:
  124.             #t6 is now the difference in exponent
  125.             sub $t6, $t3, $t2
  126.             srlv $t4, $t4, $t6
  127.             move $t2, $t3
  128.             j skipshift
  129.         a1smaller:
  130.             sub $t6, $t2, $t3
  131.             srlv $t5, $t5, $t6
  132.             move $t3, $t2
  133.         skipshift:
  134.             #a0 holds sign
  135.             move $a0, $t0
  136.             #a1 holds first number
  137.             add $a1, $t4, $t5
  138.             li $a2, 0
  139.             move $a3, $t2
  140.             jal NormalizeFloat
  141.             lw $ra, 0($sp)
  142.             addi $sp, $sp, 4
  143.             jr $ra
  144.        
  145.     MultFloats:
  146.         subi $sp, $sp, 4
  147.         sw $ra, 0($sp)
  148.         #a0 = Single precision float A
  149.         #a1 = Single precision float B
  150.         #v0 = Multiplcation result of A * B
  151.         #t0 will hold the sign of a0
  152.         #t1 will hold the sign of a1
  153.         #t2 will hold the exponent of a0
  154.         #t3 will hold the exponent of a1
  155.         #t4 will hold the mantissa of a0
  156.         #t5 will hold the mantissa of a1
  157.         move $t0, $a0
  158.         srl $t0, $t0, 31
  159.         move $t1, $a1
  160.         srl $t1, $t1, 31
  161.         #Signs done storing
  162.         andi $t2, $a0, 0x7F800000
  163.         srl $t2, $t2, 23
  164.         andi $t3, $a1, 0x7F800000
  165.         srl $t3, $t3, 23
  166.         #Exponents done storing.
  167.         andi $t4, $a0, 0x007FFFFF
  168.         andi $t5, $a1, 0x007FFFFF
  169.         #Mantissas done storing
  170.         #t0 will hold final sign
  171.         bgtz $t0, firstnegativesign
  172.         bgtz $t1, secondnegativesign
  173.         j continuetoexponent
  174.         firstnegativesign:
  175.             bgtz $t1, twonegatives
  176.             li $t0, 1
  177.             j continuetoexponent
  178.         secondnegativesign:
  179.             bgtz $t0, twonegatives
  180.             li $t0, 1
  181.             j continuetoexponent
  182.         twonegatives:
  183.             li $t0, 0
  184.            
  185.         continuetoexponent:
  186.         #t2 will hold final exponent
  187.         add $t2, $t2, $t3
  188.         subi $t2, $t2, 127
  189.         #Add hidden bit
  190.         ori $t4, $t4, 0x00800000
  191.         ori $t5, $t5, 0x00800000
  192.         #t4 will hold high part of mantissa
  193.         #t5 will hold low part of mantissa
  194.         mult $t4, $t5
  195.         mfhi $t4
  196.         mflo $t5
  197.         #now normalize and store
  198.         move $a0, $t0
  199.         move $a1, $t4
  200.         move $a2, $t5
  201.         move $a3, $t2
  202.         jal NormalizeFloat
  203.         lw $ra, 0($sp)
  204.         addi $sp, $sp, 4
  205.         jr $ra
  206.        
  207.        
  208.     NormalizeFloat:
  209.         subi $sp, $sp, 4
  210.         sw $ra, 0($sp)
  211.         #a0 = 1-bit sign bit right aligned
  212.         #a1 - [63:32] of mantissa
  213.         #a2 = [31:0] of mantissa
  214.         #a3 = 8-bit biased exponent right alined
  215.         #v0 = normalized FP result
  216.         #t0 will hold number of leading zeros total
  217.         #t1 will hold calculate on a2 IF NEEDED
  218.         #t2 holds number 32 (max zeros in a1)
  219.         #t3 will hold the number 18
  220.         #t4 temp for conditional comparison
  221.         #t5 will hold number to shift by
  222.         #t6 will hold exponent
  223.         #t7 is temporary register when shifting a2
  224.         #t8 is 32 subtracted by number we are shifting by
  225.         #s2 will hold 17
  226.         li $s2, 17
  227.         li $t4, 0
  228.         li $t3, 18
  229.         li $t8, 32
  230.         li $t2, 32
  231.         move $t6, $a3
  232.         li $t5, 0
  233.         clz $t0, $a1
  234.         beq $t0, $t2, MoreZeros
  235.         j NoMoreZeros
  236.             MoreZeros:
  237.                 clz $t1, $a2
  238.                 add $t0, $t0, $t1
  239.         NoMoreZeros:
  240.         #Is the number of zeros less than 17?
  241.         slt $t4, $t0, $s2 #will hold 1 if less than 17
  242.         bgtz $t4, LessThan18
  243.        
  244.         slt $t4, $s2, $t0 #will hold 1 if greater than 17
  245.         bgtz $t4, MoreThan18
  246.        
  247.         beqz $t4, skip
  248.         j LessThan18
  249.             LessThan18:
  250.                 move $t5, $t3
  251.                 sub $t3, $t3, $t0
  252.                 subi $t3, $t3, 1
  253.                 move $t5, $t3
  254.                 #shift right by t5
  255.                 j ShiftRight
  256.             MoreThan18:
  257.                 #addi $t0, $t0, 1
  258.                 sub $t5, $t0, $s2
  259.                 #shift left by t5
  260.                 j ShiftLeft
  261.        
  262.             ShiftRight:
  263.                 #need to calculate new registers better
  264.                 srlv $a2, $a2, $t5
  265.                 move $t7, $a1
  266.                 sub $t8, $t8, $t5
  267.                 sllv $t7, $t7, $t8
  268.                 srlv $a1, $a1, $t5
  269.                 or $a2, $a2, $t7
  270.                 j IncreaseExponent
  271.             ShiftLeft:
  272.                 #need to calculate new registers better
  273.                 sllv $a1, $a1, $t5
  274.                 move $t7, $a2
  275.                 sub $t8, $t8, $t5
  276.                 srlv $t7, $t7, $t8
  277.                 sllv $a2, $a2, $t5
  278.                 or $a1, $a1, $t7
  279.                 j DecreaseExponent
  280.             skip:
  281.             DecreaseExponent:
  282.                 sub $t6, $t6, $t5
  283.                 j StartPacking
  284.             IncreaseExponent:
  285.                 add $t6, $t6, $t5
  286.                 j StartPacking
  287.         #a0 = 1-bit sign bit right aligned
  288.         #a1 - [63:32] of mantissa normalized
  289.         #a2 = [31:0] of mantissa normalized
  290.         #a3 = 8-bit biased exponent right alined
  291.         #v0 = normalized FP result
  292.         #t0 holds last 14 bits of a1
  293.         #t1 holds first 9 bits of a2
  294.         #t2
  295.         #t3
  296.         #t4
  297.         #t5
  298.         #t6 holds exponent(edited)
  299.             StartPacking:
  300.                 move $v0, $a0
  301.                 sll $v0, $v0, 31
  302.                 #now MSB of v0 is the sign
  303.                 sll $t6, $t6, 23
  304.                 or $v0, $v0, $t6
  305.                 #t0 will hold last 14 bits of a1
  306.                 and $t0, $a1, 0x00003FFF
  307.                 sll $t0, $t0, 9
  308.                 or $v0, $v0, $t0
  309.                 #t1 will hold first 9 bits of a2
  310.                 andi $t1, $a2, 0xFF800000
  311.                 srl $t1, $t1, 23
  312.                 or $v0, $v0, $t1
  313.                 lw $ra, 0($sp)
  314.                 addi $sp, $sp, 4
  315.                 jr $ra
  316.                
  317.                
  318.            
  319.    
  320.     CompareFloats:
  321.         #a3 will hold temporarily the v0 value
  322.         li $a3, 0
  323.         li $v0, 0
  324.         #t0 holds sign a0
  325.         move $t0, $a0
  326.         srl $t0, $t0, 31
  327.         #t1 holds sign a1
  328.         move $t1, $a1
  329.         srl $t1, $t1, 31
  330.         #t2 holds exponent a0
  331.         move $t2, $a0
  332.         srl $t2, $t2, 23
  333.         andi $t2, $t2, 0x000000FF
  334.         #t3 holds exponent a1
  335.         move $t3, $a1
  336.         srl $t3, $t3, 23
  337.         andi $t3, $t3, 0x000000FF
  338.         #t4 holds mantissa for a0
  339.         move $t4, $a0
  340.         andi $t4, $t4, 0x3FFFFF
  341.         #t5 holds mantissa for a1
  342.         move $t5, $a1
  343.         andi $t5, $t5, 0x3FFFFF
  344.         #Now storage is done.
  345.         #IF the sign of a0 is less than the sign of a1,store 1
  346.         slt $a3, $t0, $t1
  347.         move $v0, $a3
  348.         bgtz $v0, doneComparing
  349.         #IF the sign of a1 is less than sign of a0, store -1
  350.         slt $a3, $t1, $t0
  351.         #if it is 1, then subtract 2. else, keep as 0.
  352.         beqz $a3, isZero
  353.         subi $a3, $a3, 2
  354.         bltz $v0, doneComparing
  355.         isZero:
  356.         move $v0, $a3
  357.         beqz $v0, dontJump
  358.         jr $ra
  359.         #Now if the sign is the same, we continue
  360.         #Now we will compare exponents
  361.         #If the exponent of a1 is less than that of a0, and both positive, store 1
  362.         dontJump:
  363.         beqz $t0, bothPositive
  364.         bgtz $t0, bothNegative
  365.         bothPositive:
  366.             #Now both are positive. If exponent of a1 is less than that of a0, store 1
  367.             slt $a3, $t3, $t2
  368.             move $v0, $a3
  369.             beqz $v0, dontJumpOne
  370.             jr $ra
  371.             #If exponent of a1 is greater than at of a0, store -1
  372.             dontJumpOne:
  373.             slt $a3, $t2, $t3
  374.             beqz $a3, isZeroOne
  375.             subi $a3, $a3, 2
  376.             isZeroOne:
  377.             move $v0, $a3
  378.             beqz $v0, PositiveAndSameExponent
  379.             jr $ra
  380.             PositiveAndSameExponent:
  381.                 #If the mantissa of a1 is less than that of a0, store 1
  382.                 slt $a3, $t5, $t4
  383.                 move $v0, $a3
  384.                 #If mantissa of a1 is greater than that of a0, store -1
  385.                 slt $a3, $t4, $t5
  386.                 beqz $a3, isZeroTwo
  387.                 subi $a3, $a3, 2
  388.                 isZeroTwo:
  389.                 move $v0, $a3
  390.                 jr $ra
  391.         bothNegative:
  392.             #Now both are negative. If exponent of a1 is less than that of 0, store -1
  393.             slt $a3, $t3, $t2
  394.             bgtz $a3, a3isone
  395.             j dontJumpTwo
  396.             a3isone:
  397.             sub $a3, $a3, 2
  398.             move $v0, $a3
  399.             beqz $v0, dontJumpTwo
  400.             jr $ra
  401.             dontJumpTwo:
  402.             #If exponent of a1 is greater than that of a0, store 1
  403.             slt $a3, $t2, $t3
  404.             move $v0, $a3
  405.             beqz $v0, NegativeAndSameExponent
  406.             jr $ra
  407.             NegativeAndSameExponent:
  408.                 #If the mantissa of a1 is less than that of a0, store -1
  409.                 slt $a3, $t5, $t4
  410.                 beqz $a3, dontJumpThree
  411.                 mantissaissmaller:
  412.                 subi $a3, $a3, 2
  413.                 move $v0, $a3
  414.                 beqz $v0, dontJumpThree
  415.                 jr $ra
  416.                 dontJumpThree:
  417.                 #If exponent of a1 is greater than that of a0, store 1
  418.                 slt $a3, $t4, $t5
  419.                 move $v0, $a3
  420.                 j doneComparing
  421.         #If the exponent of a1 is greater than that of a1
  422.                 doneComparing:
  423.                 jr $ra
  424.        
  425.        
  426.     #PrintFloat Function
  427.     PrintFloat:
  428.         # the number is stored in a0.
  429.         # t0 has operations done on it
  430.         # t4 holds the number
  431.         move $t4, $a0
  432.         move $t0, $t4
  433.         srl $t0, $t0, 31
  434.         la $a0, sign
  435.         li $v0, 4
  436.         syscall
  437.         la $a0, ($t0)
  438.         li $v0, 1
  439.         syscall
  440.         li $v0, 4
  441.         la $a0, newLine
  442.         syscall
  443.         #---- Sign done. now exponent:
  444.         move $t0, $t4
  445.         srl $t0, $t0, 23
  446.         andi $t0, $t0, 0x000000FF
  447.         #storing the exponent in s0
  448.         #number of digits in argument a1 - 1, number to shift by
  449.         li $a1, 7
  450.         move $s0, $t0
  451.         la $a0, exponent
  452.         li $v0, 4
  453.         syscall
  454.         j ExponentToBinary
  455.         backfromexponent:
  456.         li $v0, 4
  457.         la $a0, newLine
  458.         syscall
  459.         #---- Exponent done. now mantissa:
  460.         move $t0, $t4
  461.         andi $t0, $t0, 0x007FFFFF
  462.         li $a1, 22
  463.         move $s0, $t0
  464.         la $a0, mantissa
  465.         li $v0, 4
  466.         syscall
  467.         j MantissaToBinary
  468.         backfrommantissa:
  469.         li $s0, 0
  470.         jr $ra
  471.    
  472.     ExponentToBinary:
  473.         bltz $a1, backfromexponent
  474.         move $t0, $s0
  475.         srlv $t0, $t0, $a1
  476.         sub $a1, $a1, 1
  477.         and $t0, $t0, 0x01
  478.         li $v0,1
  479.         la $a0, ($t0)
  480.         syscall
  481.         j ExponentToBinary
  482.        
  483.     MantissaToBinary:
  484.         bltz $a1, backfrommantissa
  485.         move $t0, $s0
  486.         srlv $t0, $t0, $a1
  487.         sub $a1, $a1, 1
  488.         and $t0, $t0, 0x01
  489.         li $v0,1
  490.         la $a0, ($t0)
  491.         syscall
  492.         j MantissaToBinary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement