In: Computer Science
This assignment worth 10% of your total course grade.
Write spim program and execute it on
mars. Your program reads two integer values x, y. Both x and y must
be single digit number > 0.
If the user does not enter a value that meets this condition ask
the user to enter a new value
again. Also Write two functions. The first function call it SumEven
that gets x and y passed as
parameters and returns the sum of even values between x and 10*y
inclusive. Also write
another function call it Factorial that gets |y-x| (absolute value
of y-x) as a parameter and
returns the factorial of this value. Before you exit your program
Must print both values
returned on the screen a long with your name.
Input:
Enter first number: x if x is not single didit ask fox another
value
Enter second number y if y is not single digit ask for another
value
Output:
Your Name
The value returned by SumEven
The value returned by Factorial.
Mips Languages
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
code.asm
.data
prompt: .asciiz "\nPlease enter first number : "
prompt1: .asciiz "\nPlease enter second number : "
errMsg: .asciiz "\nPlease enter single digit number"
sumEvenMsg: .asciiz "\nSum of even number is : "
factMsg: .asciiz "\nThe result of factorial is : "
.globl main
.text
main:
askFirst:
li $v0,4
la $a0,prompt #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0 #save a to t1
bgt $t1,10,error1
blt $t1,0,error1
j next
error1:
li $v0,4
la $a0,errMsg #it will print prompt
syscall
j askFirst
next:
askSecond:
li $v0,4
la $a0,prompt1 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t2,$v0 #save a to t1
bgt $t2,10,error2
blt $t2,0,error2
j next2
error2:
li $v0,4
la $a0,errMsg #it will print prompt
syscall
j askSecond
next2:
move $a0,$t1
move $a1,$t2
jal sumEven
move $t0,$v0
li $v0,4
la $a0,sumEvenMsg #it will print prompt
syscall
move $a0,$t0
li $v0,1
syscall
move $a0,$t1
move $a1,$t2
jal Factorial
move $t0,$v0
li $v0,4
la $a0,factMsg #it will print prompt
syscall
move $a0,$t0
li $v0,1
syscall
li $v0,10
syscall
sumEven:
li $v0,0
mul $a1,$a1,10 #get b*10
loop:
div $t0,$a0,2
mfhi $t0
bne $t0,0,skipOdd
add $v0,$v0,$a0
skipOdd:
add $a0,$a0,1 #
ble $a0,$a1,loop
jr $ra
Factorial:
sub $t3,$a0,$a1
bgt $t3,0,skipNeg
mul $t3,$t3,-1 #get abs value
skipNeg:
li $t0,1
li $v0,1
loop2:
mul $v0,$v0,$t0
add $t0,$t0,1
ble $t0,$t3,loop2
jr $ra