Using the MARS or SPIM simulator develop a program that will
Solution
Section 1) MARS program
# MARS program to evaluate the mathematical expression 3*n+n*(n-1)-15
.data # variable declarations
n:.word 15 # create a integer variable with initial value 15
.text # instructions
lw $s1,n # load n to destination register.
li $s2,3 # load register $s2 with 3
mult $s1,$s2 # Perform multiplication 3*n
mflo $s4 # moving result saved in lo to $s4
addi $s5,$s1,-1 # evaluating 15-1 by $s5 = $s1-1
mult $s1,$s5 # Performing multiplication n*(n-1)
mflo $s5 # moving result saved in lo to $s5
addi $s6,$s5,-15 # Performing n*(n-1)-15 and result in $s6
add $s7,$s6,$s4 # add $s6 and $s4 for 3*n+n*(n-1)-15
li $v0, 1 # load print system call code into register $v0;
move $a0,$s7 # moving the result to $a0 to print
syscall # system call to print the result
li $v0,10 # load exit system call code into register $v0;
syscall # system call to exit
Result
240
-- program is finished running --
Section 2) MARS program
.data
# instruction to the user to enter the number n
n: .asciiz \"enter the value of n: \"
.text
la $a0, n # string to print
li $v0, 4 # print string
syscall # system call to print strin in n
li $v0, 5 # integer input
syscall # system call for input
move $s0, $v0 # copy input to s0
andi $s1, $s0, 1 #bitwise and immediately to know even or not
beq $s1, $zero, even # even number
li $s2,3 # load register $s2 with 3
mult $s0,$s2 # Perform multiplication 3*n
mflo $s4 # moving result saved in lo to $s4
addi $s5,$s4,1 # evaluating 3*n+1
j done # jump to done
even:
li $s4, 2
div $s5, $s0, $s4 # devision by 2
j done
done:
li $v0, 1 # load print system call code into register $v0;
move $a0,$s5 # moving the result to $a0 to print
syscall # system call to print the result
li $v0,10 # load exit system call code into register $v0;
syscall # system call to exit
Result
enter the value of n: 6
3
-- program is finished running --
enter the value of n: 5
16
-- program is finished running --

