Silver_Smoulder icon

[ASM] [Functional] Arithmetic, Output, Errors

Silver_Smoulder | PRO | 04/03/19 02:12:06 PM UTC | 0 ⭐ | 381 👁️ | Never ⏰ | []
text |

5 KB

|

None

|

0 👍

/

0 👎

#write a program that is going to take user input of two numbers (test it only with the 1st # > 2nd #) and then
#and then output: the sum/difference/quotient/product is and then actually do it, using the functions from the appendix
#furthermore when I am done, make it do an error message if 1st # < 2nd #
 .globl main
.text
 main:
		#copied from the first 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
 		#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 2 numbers and have stored them in $t0 and $t1	
 		##at this point, we want to test the two numbers and give the user an error message if the input is wrong
		##not at this point, because we need to determine the difference of the two values, so it will need to happen after the arithmetic block
		#probably with blez (see pg 17 and 79 in Britton)
		#start at line 49 with ##error test here
  		##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
 		##error test here
		#whoops forgot to tell it we're gonna load a string
		#li $v0, 4 
		#nope wasn't it
		#try evaluating it at #v0?
		#move $v0, $t4
		#nope
		#stuff like replacing sub with subu and blez with bltzal doesn't either
		blez $t4, brexit
  		#output block
		#adzal dition
		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
     		#and now we're done
    		li $v0, 10
    		syscall
     	brexit: #we put it here because any actual code needs to be in .text, but I am putting it after the "program exit" command
    		#so that we don't accidentally run it during our program phase
		li $v0, 4 #ready to output a string
		la $a0, error_msg #give it the address of my string
		syscall # go do it
 		li $v0, 10 #get ready to leave
		syscall #leave
 	.data #gonna be storing our text and prompts 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: "
error_msg:
	.asciiz "Please terminate the program and input the numbers in the proper sequence - the first number must be greater than the second number."

Comments