#do the pythagorean theorem using the stack and functions calling functions
.data
prompt_A:
.asciiz "Please enter side A: "
prompt_B:
.asciiz "\n Please enter side B: "
prompt_C:
.asciiz "\n Please enter side C: "
comma:
.asciiz ", "
yes_right:
.asciiz " - is a right triangle."
no_right:
.asciiz " - is not a right triangle."
infinite_loop:
.asciiz "\n \n THERE IS AN INFINITE LOOP!!! YOU SCREWED UP!!! \n\n"
.globl main
.text
#the outline is as follows:
#1) Go to main and activate allwork.
#2) Store the address of allwork on the stack, then jump to input_numbers
#3) Store the address of input_numbers, prompt user for input, jump to evaluate
#4) Store the address of evaluate, and then test to see which of the numbers is the hypotenuse, jump to power
#5) Store the address of power, and square all of the values, jump to calculate
#6) Store the address of calculate, subtract the sum of the two non-hypotenuse squares (the legs), if they're zero, jump to yes, else no.
#7a) Store address of yes, output that it is a right angle, jump to exit
#7b) Store address of no, output that it is not a right angle, jump to exit.
#8a) Clear the addresses from the stack, exit the program.
main:
jal allwork
li $v0,10
syscall
allwork:
addi $sp, $sp, -4 #position -4
sw $ra, ($sp)
jal input_numbers
input_numbers:
addi $sp, $sp, -4 #position -8
sw $ra, ($sp)
li $v0, 4 #store 1st number
la $a0, prompt_A
syscall
li $v0, 5 #prep for user input
syscall
move $s0, $v0
li $v0, 4 #store 2nd number
la $a0, prompt_B
syscall
li $v0, 5 #prep for user input
syscall
move $s1, $v0
li $v0, 4 #store 3rd number
la $a0, prompt_C
syscall
li $v0, 5 #prep for user input
syscall
move $s2, $v0
jal evaluate
evaluate:
#we are going to compare three numbers and see which is the largest
#we will compare s0 to s1, then s0 to s2, then s1 to s2, and place the greatest number into s4, and the other two into s5 and s6
#if any of the numbers are equal to each other, we can hard_exit, since they cannot be a right triangle
beq $s0, $s1, hard_exit
beq $s1, $s2, hard_exit
addi $sp, $sp, -4 #position -12
sw $ra, ($sp)
move $s4, $s0 #store s0 in the hypotenuse slot
bgt $s4, $s1, evaluate_s0_gt_s2 #if s0>s1, go check s0 and s2
j evaluate_s1_gt_s2 #if not, then s1 is greater than s0
evaluate_s0_gt_s2:
bgt $s4, $s2, s0_grt #if s0 > s2 then s0 is the greatest
j s2_grt #if not, then s2 is the greatest
evaluate_s1_gt_s2:
move $s4, $s1 #since s1 was bigger than s0, we place s1 into s5
bgt $s4, $s2, s1_grt #if s1 > s2 then s1 is the greatest
j s2_grt #if not, then s2 is the greatest
s0_grt:
#s0 turned out to be the greatest number, so it is our hypotenuse
move $s4, $s0
move $s5, $s1
move $s6, $s2
jal power
s1_grt:
#s1 turned out to be the greatest number, so it is our hypotenuse
move $s4, $s1
move $s5, $s0
move $s6, $s2
jal power
s2_grt:
#s2 turned out to be the greatest number, so it is our hypotenuse
move $s4, $s2
move $s5, $s0
move $s6, $s1
jal power
hard_exit:
#this only triggers if any of the numbers are equal to each other
lw $ra, ($sp)
addi $sp, $sp, 8
jr $ra #jump back and exit
power:
addi $sp, $sp, -4 #position 16
sw $ra, ($sp)
li $t3, 0 #our power incrementor
power_loop:
add $t3, $t3, 1 #increment out power inrcrementor
beq $t3, 2, calculate #our incrementor is also the power to which we raise our values. in this case it's 2 but we could change it to other stuff
mul $s4, $s4, $s4 #square the hypotenuse
mul $s5, $s5, $s5, #square one leg
mul $s6, $s6, $s6 #square the other
jal calculate
calculate:
addi $sp, $sp, -4 #position -20
sw $ra, ($sp)
#set the sum of the two legs to s7. if s4 and s7 are equal, then it's a right triangle, if they aren't, then it isn't
add $s7, $s5, $s6
beq $s4, $s7, yes
j no
yes:
#output all the numbers and commas
move $a0, $s0 #output the first integer, then a comma space
li $v0, 1
syscall
li $v0, 4
la $a0, comma
syscall
move $a0, $s1 #output the second integer, then a comma space
li $v0, 1
syscall
li $v0, 4
la $a0, comma
syscall
move $a0, $s2 #output the third integer, then a comma space
li $v0, 1
syscall
li $v0, 4
la $a0, yes_right
syscall
addi $sp, $sp, 16
lw $ra, ($sp)
jr $ra #jump back and exit
no:
#output all the numbers and commas
move $a0, $s0 #output the first integer, then a comma space
li $v0, 1
syscall
li $v0, 4
la $a0, comma
syscall
move $a0, $s1 #output the second integer, then a comma space
li $v0, 1
syscall
li $v0, 4
la $a0, comma
syscall
move $a0, $s2 #output the third integer, then a comma space
li $v0, 1
syscall
li $v0, 4
la $a0, no_right
syscall
addi $sp, $sp, 16
lw $ra, ($sp)
jr $ra #jump back and exit
Comments