Silver_Smoulder icon

[ASM] Basic Arithmetic and Output [Functional]

Silver_Smoulder | PRO | 04/02/19 03:00:32 PM UTC | 0 ⭐ | 428 👁️ | Never ⏰ | []
text |

4.12 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
 		#probably with blez (see pg 17 and 79 in Britton)
 		##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
		#addition
		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
 	.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:
	.asciiz "Please terminate the program and input the numbers in the proper sequence - the first number must be greater than the second number."

Comments