Silver_Smoulder

[ASM}[Func]Exponentiation

May 22nd, 2019
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #be able to take the power of a positive or negative exponent
  2.  
  3. .data
  4.  
  5. input_base:
  6. .asciiz "Please enter your base: "
  7.  
  8. input_exponent:
  9. .asciiz "Please input your exponent: "
  10.  
  11. output_result:
  12. .asciiz "Your result is: "
  13.  
  14. newline:
  15. .asciiz "\n"
  16.  
  17. .globl main
  18. .text
  19.  
  20. #we are going to do this iteratively - we will have a counter.
  21. #when the counter will be equal to the exponent, we will either output+terminate, or negate+output+terminate
  22. #t0 is going to be our counter, t1 is going to be our base, t2 is going to be our exponent
  23. #t3 is going to be the absolute value of the exponent (and exit condition), t4 is going to store our base increment
  24. #f0 is going to be our output, $f2 is going to store ffloat 1, and $t5 is going to be integer 1
  25.  
  26. main:
  27. #get all the inputs we need
  28. li $t5, 1 #this is ugly and I hate it BUT it can also be our output for when the exponent is 0
  29. li $t0, 1 #initialize our counter to 0
  30.  
  31. li $v0, 4 #string output
  32. la $a0, input_base
  33. syscall
  34.  
  35. li $v0, 5 #prep for user input
  36. syscall
  37. move $t1, $v0
  38.  
  39. li $v0, 4 #string output
  40. la $a0, newline
  41. syscall
  42.  
  43. li $v0, 4 #string output
  44. la $a0, input_exponent
  45. syscall
  46.  
  47. li $v0, 5 #prep for user input
  48. syscall
  49. move $t2, $v0
  50.  
  51. li $v0, 4 #string output
  52. la $a0, newline
  53. syscall
  54.  
  55. #next, we will determine whether the exponent is positive or negative and deal with it accordingly
  56.  
  57. move $t4, $t1 #set our base total value to our base increment during the first pass
  58. beqz $t2, zero_exponent
  59. bltz $t2, negative_exponent #if negative, go do a thing to it
  60. #else if the exponent is positive
  61. move $t3, $t2 #make sure our exponent is set in several places
  62.  
  63. positive_exponent:
  64. beq $t0, $t3, exit #if the counter is equal to the exponent (our exit condition/sentinel), we leave
  65. #else
  66. mul $t4, $t4, $t1 #multiply our base total value by the base increment
  67. addi $t0, $t0, 1 #increment our counter by 1
  68. j positive_exponent
  69.  
  70. negative_exponent:
  71. mul $t3, $t2, -1 #convert the negative exponent to a positive one and store it in a different place
  72. j positive_exponent
  73.  
  74. zero_exponent:
  75. #we will just output that the answer is 1
  76.  
  77. li $v0, 4 #string output
  78. la $a0, output_result
  79. syscall
  80.  
  81. move $a0, $t5
  82. li $v0, 1
  83. syscall
  84.  
  85. li $v0, 10 #terminate the program
  86. syscall
  87.  
  88. exit:
  89. bltz $t2, exit_negative #if the exponent was negative, we'll need to make it into a float
  90.  
  91. #else just output the result
  92.  
  93. li $v0, 4 #string output
  94. la $a0, input_base
  95. syscall
  96.  
  97. move $a0, $t4 #set a0 to have our final result
  98. li $v0, 1 #prep for integer output
  99. syscall
  100.  
  101. li $v0, 4 #string output
  102. la $a0, newline
  103. syscall
  104.  
  105. li $v0, 10 #terminate the program
  106. syscall
  107.  
  108. exit_negative:
  109. mtc1 $t4, $f0 #move our result from t4 to f0
  110. cvt.s.w $f0, $f0 #convert t4 to a single-float in f0
  111. mtc1 $t5, $f2 #set f2 to 1
  112. cvt.s.w $f2, $f2 #convert the value 1 into a float
  113. div.s $f12, $f2, $f0 #then we divide 1 by f0, and store it back in f12
  114.  
  115. li $v0, 2 #prep to output a float
  116. syscall #go output the number stored in f12
  117.  
  118. li $v0, 4 #string output
  119. la $a0, newline
  120. syscall
  121.  
  122. li $v0, 10 #terminate the program
  123. syscall
Advertisement
Add Comment
Please, Sign In to add comment