Silver_Smoulder icon

[ASM][Functional]Loops & Output

Silver_Smoulder | PRO | 04/03/19 03:15:54 PM UTC | 0 ⭐ | 443 👁️ | Never ⏰ | []
text |

5.01 KB

|

None

|

0 👍

/

0 👎

##do something similar to the previous program but instead, have there be a while loop that allows you to input as
##many numbers as you want, but will exit when the last number is less than the first and if the number is 0
 .globl main
.text
	main:	
		##overall, we want to create a loop that is going to compare our two testing values, execute them, and then re-do it
		##unless the 2nd number > 1st, at which point we exit with an error message, but we do it in a loop
 		#copied from the previous program
 		li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
		la $a0, prompt_first_number #we're telling the program where to look for our message that we defined at the bottom of the program
		syscall #go do it
 		#now, we should read in an integer as done by the user, with syscall 5
		li $v0, 5 #syscall for code 5 (reading in an integer)
		syscall #go do it
 		move $t0, $v0 #moving the stored integer from $v0 to $t0 so that we can free up the space for the termination code 10
		#we now stored the value that the user input in $t0
 		##loop will initialize here: if our 2nd number is larger than the first, we exit
	loop:	
 		#this is the block of code we want repeated in our code
		#now for the second number, code is going to be pretty identical
		#we can just copypaste this block because the new value in #v0/#a0 is going to bverwrite the old stuff
		li $v0, 4 #we're telling the program that we are going to be outputting a string and loading it immediately
		la $a0, prompt_second_number #we're telling the program where to look for our message that we defined at the bottom of the program
		syscall #go do it
 		#now, we should read in an integer as done by the user, with syscall 5
		li $v0, 5 #syscall for code 5 (reading in an integer)
		syscall #go do it
 		move $t1, $v0 #moving the stored integer from $v0 to $t1 so that we can free up the space for the termination code 10
 		##at this point we have prompted the user for at least 2 numbers and have stored them in $t0 and $t1	
 		##we will do our first conditional for the exit conditional here
		blt $t1, $t0, end
 		##We will be storing the sum in t3, the difference in t4, the quotient in t5, and the product in t6
  		#arithmetic block
		add $t3, $t0, $t1 #this should do: $t0+$t1 = something, which we put into $t3
		sub $t4, $t0, $t1 #this should do: $t0-$t1 = something, which we put into $t4
		div $t5, $t0, $t1 #this should do: $t0/$t1 = something, which we put into $t5
		mul $t6, $t0, $t1 #this should do: $t0*$t1 = something, which we put into $t6
 		#output block
		li $v0, 4 #tell the system we are going to load a string
		la $a0, sum #tell it that the address of the string is in $a0
		syscall #go do it
 		move $a0, $t3 #move the value stored $t0 to $a0
   		li $v0, 1     #tell it we're about to print an integer
    		syscall      #go do it
     		#subtraction
    		li $v0, 4 #tell the system we are going to load a string
		la $a0, diff #tell it that the address of the string is in $a0
		syscall #go do it
 		move $a0, $t4 #move the value stored $t0 to $a0
   		li $v0, 1     #tell it we're about to print an integer
    		syscall      #go do it
     		#division
    		li $v0, 4 #tell the system we are going to load a string
		la $a0, quot #tell it that the address of the string is in $a0
		syscall #go do it
 		move $a0, $t5 #move the value stored $t0 to $a0
   		li $v0, 1     #tell it we're about to print an integer
    		syscall      #go do it
     		#multiplication
    		li $v0, 4 #tell the system we are going to load a string
		la $a0, prod #tell it that the address of the string is in $a0
		syscall #go do it
 		move $a0, $t6 #move the value stored $t0 to $a0
   		li $v0, 1     #tell it we're about to print an integer
    		syscall      #go do it
     		##this is where we will terminate our loop and go back to the initialize
    		li $v0, 4 #prep string
    		la $a0, rinit #load address of string
    		syscall #go do it
     		ble $t0, $t1, loop #since our input values haven't changed, we tell it to go back to the loop init
     	end:
    		#output the exit message and terminate the program
     		#exit message
    		li, $v0, 4 #prep a string
    		la $a0, terminate #load string address
    		syscall #go do it
    		#and now we're done
    		li $v0, 10 #prep exit
    		syscall #go do it
       	.data #no longer sure what exactly we store here
 prompt_first_number:
	.asciiz "Enter your first number: \n"
prompt_second_number:
	.asciiz "Enter your second number: \n"
sum:
	.asciiz "\nThe sum is: "
diff:
	.asciiz "\nThe difference is: "
quot:
	.asciiz "\nThe quotient is: "
prod:
	.asciiz "\nThe product is: "
rinit:
	.asciiz "\nWe are now re-engaging the loop, please stand by.\n"
terminate: 
	.asciiz "Termination condition triggered. Have a nice day. The program will now terminate. Please restart to reinitialize."

Comments