Silver_Smoulder icon

[ASM}[Func]Exponentiation

Silver_Smoulder | PRO | 05/22/19 03:09:10 PM UTC | 0 ⭐ | 416 👁️ | Never ⏰ | []
text |

3.14 KB

|

None

|

0 👍

/

0 👎

#be able to take the power of a positive or negative exponent
 .data
 input_base:
	.asciiz "Please enter your base: "
 input_exponent:
	.asciiz "Please input your exponent: "
 output_result:
	.asciiz "Your result  is: " 
 newline:
	.asciiz "\n"
 .globl main
.text
 	#we are going to do this iteratively - we will have a counter.
	#when the counter will be equal to the exponent, we will either output+terminate, or negate+output+terminate
	#t0 is going to be our counter, t1 is going to be our base, t2 is going to be our exponent
	#t3 is going to be the absolute value of the exponent (and exit condition), t4 is going to store our base increment
	#f0 is going to be our output, $f2 is going to store ffloat 1, and $t5 is going to be integer 1
 main:
#get all the inputs we need
	li $t5, 1 #this is ugly and I hate it BUT it can also be our output for when the exponent is 0
	li $t0, 1 #initialize our counter to 0
 	li $v0, 4 #string output
	la $a0, input_base
	syscall
 	li $v0, 5 #prep for user input
	syscall
	move $t1, $v0 
 	li $v0, 4 #string output
	la $a0, newline
	syscall		
 	li $v0, 4 #string output
	la $a0, input_exponent
	syscall
 	li $v0, 5 #prep for user input
	syscall
	move $t2, $v0 
 	li $v0, 4 #string output
	la $a0, newline
	syscall
 	#next, we will determine whether the exponent is positive or negative and deal with it accordingly
 	move $t4, $t1 #set our base total value to our base increment during the first pass
	beqz $t2, zero_exponent
	bltz $t2, negative_exponent #if negative, go do a thing to it
	#else if the exponent is positive
	move $t3, $t2 #make sure our exponent is set in several places
 positive_exponent:
	beq $t0, $t3, exit #if the counter is equal to the exponent (our exit condition/sentinel), we leave
	#else
	mul $t4, $t4, $t1 #multiply our base total value by the base increment
	addi $t0, $t0, 1 #increment our counter by 1
	j positive_exponent
 negative_exponent:
	mul $t3, $t2, -1 #convert the negative exponent to a positive one and store it in a different place
	j positive_exponent
 zero_exponent:
	#we will just output that the answer is 1
 	li $v0, 4 #string output
	la $a0, output_result
	syscall	
 	move $a0, $t5
	li $v0, 1
	syscall
 	li $v0, 10 #terminate the program
	syscall
 exit:
	bltz $t2, exit_negative #if the exponent was negative, we'll need to make it into a float
 	#else just output the result
 	li $v0, 4 #string output
	la $a0, input_base
	syscall
 	move $a0, $t4 #set a0 to have our final result
	li $v0, 1 #prep for integer output
	syscall
 	li $v0, 4 #string output
	la $a0, newline
	syscall	
 	li $v0, 10 #terminate the program
	syscall
 exit_negative:
	mtc1 $t4, $f0 #move our result from t4 to f0
	cvt.s.w $f0, $f0 #convert t4 to a single-float in f0
	mtc1 $t5, $f2 #set f2 to 1
	cvt.s.w $f2, $f2 #convert the value 1 into a float	
	div.s $f12, $f2, $f0 #then we divide 1 by f0, and store it back in f12
 	li $v0, 2 #prep to output a float
	syscall #go output the number stored in f12
 	li $v0, 4 #string output
	la $a0, newline
	syscall	
 	li $v0, 10 #terminate the program
	syscall

Comments