Skip to content

Commit

Permalink
add mips projects
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipNelson5 committed Dec 14, 2019
1 parent 4c99e82 commit 2c83a11
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 0 deletions.
102 changes: 102 additions & 0 deletions CS3810_ComputerArchitecture/MIPS/ArraySum.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#########################################################################################
# Array Sum
#########################################################################################

#########################################################################################
# DATA
#########################################################################################
.data
array: .word 1, -2, 4, -8, 16
string1: .asciiz "The sum of the array: "
string2: .asciiz "is "


#########################################################################################
# TEXT
#########################################################################################
.text
main:
la $a0, string1 # a0 base of string1
ori $v0, $0, 4
syscall
la $a0, array # a0 base of array
ori $a1, $0, 5 # a1 is size of array
jal printa
la $a0, string2 # a0 base of string2
ori $v0, $0, 4
syscall
la $a0, array # a0 base of array
ori $a1, $0, 5 # a1 is size of array
jal sum_array
or $a0, $0, $v0 # put return into a0
ori $v0, $0, 1 # load instruction 1 (print int)
syscall # print sum
j exit

#########################################################################################
# Sum the contents of an integer array
#
# registers in:
# $a0 beginning of the array
# $a1 number of elements in the array
# $ra the return address
#
# registers out:
# $v0 the sum of the array
#########################################################################################
sum_array:
or $t0, $0, $a0 # t0 is the base of the array
or $t1, $0, $0 # t1 is the counter
or $t2, $0, $0 # t2 is the sum

sum_array_loop:
beq $t1, $a1, sum_array_loop_end # if (counter == size) goto loop end

sll $t5, $t1, 2 # t5 = counter * 4 (t2 = counter num of words)
add $t3, $t0, $t5 # t3 = &array[counter]
lw $t4, 0($t3) # t4 = array[counter]

add $t2, $t2, $t4 # t2 += t4

addi $t1, $t1, 1 # ++counter
j sum_array_loop # goto top of the loop

sum_array_loop_end:
or $v0, $0, $t2 # set return to the sum
jr $ra # return

#########################################################################################
# print the contents of an integer array
#
# registers in:
# $a0 beginning of the array
# $a1 number of elements in the array
# $ra the return address
#
#########################################################################################
printa:
or $t0, $0, $a0 # t0 is the base of the array
or $t1, $0, $0 # t1 is the counter

printa_loop:
beq $t1, $a1, printa_loop_end # if (counter == size) goto loop end

sll $t2, $t1, 2 # t2 = counter * 4 (t2 = counter num of words)
add $t3, $t0, $t2 # t3 = &array[counter]
lw $a0, 0($t3) # a0 = array[counter]
ori $v0, $0, 1 # load instruction 1 (print int)
syscall # print array[counter]
ori $v0, $0, 11 # load instruction 11 (print char)
ori $a0, $0, 44 # load 32(space) to $a0
syscall # print space
ori $a0, $0, 32 # load 32(space) to $a0
syscall # print space
addi $t1, $t1, 1 # ++counter
j printa_loop # goto top of the loop

printa_loop_end:
jr $ra # return

############################################################################################
exit: li $v0, 10 # load system instruction 10 (terminate program)
syscall
182 changes: 182 additions & 0 deletions CS3810_ComputerArchitecture/MIPS/Dist2Pts.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
#########################################################################################
# Euclidian Distance Between Two Points
#########################################################################################

#########################################################################################
# DATA
#########################################################################################
.data
s_instructions: .asciiz "Enter 4 integers which represent two (x, y) pairs"
s_x1: .asciiz "x1: "
s_y1: .asciiz "y1: "
s_x2: .asciiz "x2: "
s_y2: .asciiz "y2: "
s_result: .asciiz "The distance between "
s_openParen: .asciiz "("
s_closeParen: .asciiz ")"
s_comma: .asciiz ","
s_colon: .asciiz ":"
s_space: .asciiz " "
s_and: .asciiz "and"
s_is: .asciiz "is"
newline: .asciiz "\n"

#########################################################################################
# TEXT
#########################################################################################
.text
main:
la $a0, s_instructions # load the base of the instruction string to $a0
jal printsnl

#################################
# Get two points
#################################

la $a0, s_x1 # ask for and receive x1
jal prints
jal get_int
or $s0, $0, $v0
la $a0, newline

la $a0, s_y1 # ask for and receive y1
jal prints
jal get_int
or $s1, $0, $v0
la $a0, newline

la $a0, s_x2 # ask for and receive x2
jal prints
jal get_int
or $s2, $0, $v0
la $a0, newline

la $a0, s_y2 # ask for and receive y2
jal prints
jal get_int
or $s3, $0, $v0
la $a0, newline

#################################
# Print final message
#################################

la $a0, s_result # print result message
jal prints

la $a0, s_openParen # print (
jal prints

or $a0, $0, $s0 # print x1
ori $v0, $0, 1
syscall

la $a0, s_comma # print ,
jal prints
la $a0, s_space # print ' '
jal prints

or $a0, $0, $s1 # print y1
ori $v0, $0, 1
syscall

la $a0, s_closeParen # print )
jal prints
la $a0, s_space # print ' '
jal prints
la $a0, s_and # print and
jal prints
la $a0, s_space # print ' '
jal prints
la $a0, s_openParen # print (
jal prints

or $a0, $0, $s2 # print x2
ori $v0, $0, 1
syscall

la $a0, s_comma # print ,
jal prints
la $a0, s_space # print ' '
jal prints

or $a0, $0, $s3 # print y2
ori $v0, $0, 1
syscall

la $a0, s_closeParen # print )
jal prints
la $a0, s_space # print ' '
jal prints
la $a0, s_is # print is
jal prints
la $a0, s_colon # print :
jal prints
la $a0, s_space # print ' '
jal prints

#################################
# Calculate distance
#################################

sub $s4, $s2, $s0 # a = x2 - x1
sub $s5, $s3, $s1 # b = y2 - y1

mul $s4, $s4, $s4 # a = a * a
mul $s5, $s5, $s5 # b = b * b

add $s6, $s4, $s5 # c = a + b

or $a0, $0, $s6 # store c in a0
jal sqrt # call sqrt(a0)

#################################
# Print distance
#################################

or $a0, $0, $v0 # put v0 in a0
ori $v0, $0, 1 # set syscall instruction
syscall # print square root

j exit # end progaram

#########################################################################################
prints:
ori $v0, $0, 4 # load instruction to print a string
syscall # print string
jr $ra # return

#########################################################################################
printsnl:
ori $v0, $0, 4 # load instruction to print a string
syscall # print string
la $a0, newline # load newline
syscall # print newline
jr $ra # return

#########################################################################################
get_int:
ori $v0, $0, 5 # load instruction to read an integer
syscall
jr $ra

#########################################################################################
sqrt:
ori $v0, $0, 0 # v0 = 0

sqrt_loop:
mul $t0, $v0, $v0 # t0 = v0 * v0
bgt $t0, $a0, sqrt_loop_end # if ( t0 > n ) goto end
addi $v0, $v0, 1 # v0 = v0 + 1
j sqrt_loop # goto sqrt_loop

sqrt_loop_end:
addi $v0, $v0, -1 # v0 = v0 - 1
jr $ra # return v0


#########################################################################################
# EXIT
#########################################################################################
exit: li $v0, 10 # load system instruction 10 (terminate)
syscall # terminate program
73 changes: 73 additions & 0 deletions CS3810_ComputerArchitecture/MIPS/RecursiveFactorial.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#########################################################################################
# String Encode and Compare
#########################################################################################

#########################################################################################
# DATA
#########################################################################################
.data
string1: .asciiz "The factorial of "
string2: .asciiz " is "

#########################################################################################
# TEXT
#########################################################################################
.text
main:
ori $a1, $0, 10 # a1 will hold the number to factorial

la $a0, string1 # load string1
ori $v0, $0, 4 # load instruction to print a string
syscall # print string

or $a0, $0, $a1 # print number
ori $v0, $0, 1
syscall

la $a0, string2 # load string1
ori $v0, $0, 4 # load instruction to print a string
syscall # print string

or $a0, $0, $a1 # call factorial func
jal factorial

or $a0, $0, $v0 # print result
ori $v0, $0, 1
syscall

j exit # exit program

#########################################################################################
# Recursively compute a factorial
#
# Registers In
# a0 the value of the factorial to compute
# ra the return address
#
# Registers Out
# v0 the value of the computed factorial
#
#########################################################################################
factorial:
beqz $a0, factorial_base_case# base case
sub $sp, $sp, 8 # move sp to save registers
sw $ra, 4($sp) # save the return address
sw $a0, 0($sp) # save a0

sub $a0, $a0, 1 # setup recursive call args
jal factorial # call factorial

lw $a0, 0($sp) # retreive stored registers
lw $ra, 4($sp)
addi $sp, $sp, 8

mul $v0, $v0, $a0 # return n * fact(n-1)
jr $ra # return

factorial_base_case:
ori $v0, $0, 1 # factorial(0) = 1
jr $ra # return

#########################################################################################
exit: li $v0, 10 # load system instruction 10 (terminate)
syscall # terminate program

0 comments on commit 2c83a11

Please sign in to comment.