Skip to content

Commit

Permalink
Projects
Browse files Browse the repository at this point in the history
All five of my projects from CS14.
  • Loading branch information
12NaN committed Jun 18, 2018
1 parent 93f05f1 commit 31806bd
Show file tree
Hide file tree
Showing 5 changed files with 1,115 additions and 0 deletions.
94 changes: 94 additions & 0 deletions project1.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
.text
.globl main

main: # Loops until the user inputs a zero.
la $a0, mess # Loads the address of mess into the argument register.
li $v0, 4 # System call code to print a string.
syscall # Print mess.
li $v0, 5 # System call code that reads the integer.
syscall # Reads the integer that the user had inputted.

bnez $v0, increment # Branch if the integer doesn't equal zero.
beqz $v0, results # Branch if the integer equals zero.

increment:
add $t0, $t0, 1 # Increment the integer counter by 1.
add $t1, $t1, $v0 # Add the recently inputted integer to the current sum.

j main # Return to main.

calc_average: # Calculate the average.
div $t1, $t0 # Divide the sum in register $t1 by $t0.
mflo $t2 # Store the quotient in $t2.
mfhi $t3 # Store the remainder in $t3.

li $t4, 1000 # Stores the immediate value 1000 in $t4.
mult $t4, $t3 # Multiply the remainder by 1000.
mflo $t5 # Stores the product in $t5.
div $t5, $t5, $t0 # Divide $t5 by the number of integers
# and store the result in $t5.
jr $ra # Jump to the value in the return address.

results: # Print the results.
la $a0, sum_mess # Loads the address of sum_mess into the argument register.
li $v0, 4 # System call code to print a string.
syscall # Print sum_mess.
move $a0, $t1 # Moves the sum in $t1 into the argument register.
li $v0, 1 # System call code to print an integer.
syscall # Prints the sum.

la $a0, nl # Loads the address of nl into the argument register.
li $v0, 4 # System call code to print a string.
syscall # Print nl.
move $a0, $t0 # Moves the number of integers in $t0 into the argument register.
li $v0, 1 # System call code to print an integer.
syscall # Prints the number of integers that were inputted.

la $a0, num_entered_mess # Loads the address of num_entered_mess into the argument register.
li $v0, 4 # System call code to print a string.
syscall # Prints num_entered_mess.
la $a0, average_mess # Loads the address of average_mess into the argument register.
li $v0, 4 # System call code to print a string.
syscall # Prints average_mess.
jal calc_average # Jump and link to calc_average.
move $a0, $t2 # Move the value of $t2 into the argument register.
li $v0, 1 # System call code to print an integer.
syscall # Print the integer.
la $a0, decimal # Loads the address of decimal into the argument register.
li $v0, 4 # System call code to print a string.
syscall # Prints decimal.
move $a0, $t5 # Move the value of $t5 (the decimal value) to the argument register.
li $v0, 1 # System call code to print an integer.
syscall # Print the decimal value.

j exit # Jump to exit.

exit:
la $a0, complete # Loads the address of complete into the argument register.
li $v0, 4 # System call code to print a string.
syscall # Prints complete.
li $v0, 10 # System call code to exit the program.
syscall # Exit the program.

.data
mess: .asciiz "Enter a Number(0 to exit): "
sum_mess: .asciiz "\nThe sum is: "
num_entered_mess: .asciiz " numbers were entered\n"
nl: .asciiz "\n"
average_mess: .asciiz "\nThe average is: "
decimal: .asciiz "."
complete: .asciiz "\nProgram Exited"
196 changes: 196 additions & 0 deletions project2.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
.text
.globl main
main:
jal enter_amount # Prompts the user to enter the amount of integers to be entered.
jal enter_num # Enter the integers into the array.
jal contains # Prints the contents of the array.
jal sort # Sorts the integers in the array using a bubble sort.
jal contains # Prints the sorted contents of the array.
jal search_for # Enter the integer you're searching for.
jal search # Searches the array using a iterative binary search.
jal search_results # Prints the results of the search for the integer.
j done # Exit
enter_amount:
la $a0, amount_num # Asks the user to input the number of elements for the array.
li $v0, 4 # Prints the message.
syscall
li $v0, 5 # Reads the user's input (number of elements).
syscall
move $t0, $v0 # Stores the user's input (number of elements) in $t0.
li $t1, 0 # Offset for the array: array[x] = array[0]
li $t2, 1 # Counter for the number of integers printed: i = 1
jr $ra # Return to main.

enter_num:
addi $t2, $t2, 1 # Increment the counter: i++

la $a0, num # Asks the user to enter an integer.
li $v0, 4 # Prints the message.
syscall

li $v0, 5 # Reads the integer that the user inputted.
syscall
sw $v0, array($t1) # Stores the inputted integers into the array.
addi $t1, $t1, 4 # Increments the address of the array: array[x+1].

ble $t2, $t0, enter_num # if $t2 (the counter) <= $t0 (total # of integers): enter_num

jr $ra # Return to main.
contains:
li $t1, 0 # Resets the offset for the array: array[x] = array[0]
li $t2, 1 # Resets the counter for print the integers: i = 0
la $a0, array_contains # Prints "The array contains the following: "
li $v0, 4 # Prints the message.
syscall
print_array:
addi $t2, $t2, 1 # Increment the counter: i++

lw $a0, array($t1) # Loads the integer into $a0: a = array[x].
li $v0, 1 # Prints the integer.
syscall
la $a0, nl # nl = newline (The next integer will be printed on a newline).
li $v0, 4 # Prints a newline.
syscall
addi $t1, $t1, 4 # Increments the offset of the array: x+1
ble $t2, $t0, print_array # if $t2 (integer counter) <= $t0 (total # of integers): print_array

jr $ra # Return to main.
sort:
li $t2, 0 # i = 0
outer:
addi $t2, $t2, 1 # Increments i: i++
la $a1, array # Load array address into $a1
li $t1, 0 # array[x] = array[0]
sub $t3, $t0, 1 # $t3 = n - 1 (n = total # of integers)
addi $t4, $t2, 1 # j = i + 1
ble $t2, $t3, inner # if i <= (n-1): inner
jr $ra # Return to main.
inner:
lw $t5, 0($a1) # Ex: a = 6
lw $t6, 4($a1) # Ex: b = 5
bgt $t5, $t6, swap # if a > b: swap
j continue # else: continue
swap:
sw $t6, 0($a1) # Ex: a = 5
sw $t5, 4($a1) # Ex: b = 6
continue:
addi $a1, $a1, 4 # Array[x+1]
addi $t4, $t4, 1 # j + 1
bgt $t4, $t0, outer # if (j = i + 1) > n: outer
j inner # else: inner
search_for: # Enter the integer you're looking for.
la $a0, search_num # Prompts the user to enter an integer.
li $v0, 4 # Prints the message.
syscall
li $v0, 5 # Reads the integer.
syscall
move $t0, $v0 # Stores the integer to search for in $t0
li $t2, 0 # First element in the array: array[0]
li $t4, 2 # Used to divide the amount of elements in the array by two.
li $t6, 4 # Used to increment the array

jr $ra # Return to main.
search:
bgt $t2, $t3, return # if $t2(first element) > $t3(last element (n-1)): The number wasn't found and return to main
middle_num:
add $t7, $t2, $t3 # Store the sum of $t2(first element) and $t3(last element(n-1)) in $t7.
div $t7, $t4 # $s0 # Get the middle element: $t7 / $t4 = (first element + last element) / 2.
mflo $t8 # Store the middle element in $t8.
mult $t8, $t6 # Multiply the middle element by 4 to get the index for the array.
mflo $t1 # Store the product in $t1.
lw $t5, array($t1) # Load the value of array[(middle) * 4] in $t5.
# Ex: middle = 5, x = 5 * 4 = 20, y = array[x]

bgt $t5, $t0, lower # if middle element > x (User's desired integer): check the lower half of the array
blt $t5, $t0, upper # if middle element < x (User's desired integer): check the upper half of the array
li $s0, 1 # The integer was found (1 = true)
jr $ra # if middle element == x (User's desired integer): return to main
upper: # Upper half of the array.
add $t2, $t8, 1 # $t2 (first element) = $t8 (middle element) + 1
j search # Jump to search and check the rest of the array.
lower: # Lower half of the array.
sub $t3, $t8, 1 # $t3 (last element (n-1)) = $t8 (middle element) - 1
j search # Jump to search and check the rest of the array.
return:
li $s0, 0 # The integer wasn't found (0 = false)
jr $ra # Return to main.

search_results:
beqz $s0, not_found # if $s0 == 0: not_found
found:
move $a0, $t0 # Store the user's desired integer in $a0 to print.
li $v0, 1 # Print the user's integer.
syscall
la $a0, f # Prompt the user that the integer was found.
li $v0, 4 # Print the message.
syscall

div $t1, $t6 # Divide the offset of the array by 4 to get the
mflo $a0 # location of the number in the array and store it in $a0.

li $v0, 1 # Print the location of the number.
syscall
la $a0, cl_bracket # Print ] for the location of the number.
li $v0, 4 # Print the bracket.
syscall
jr $ra # Return to main.
not_found:
move $a0, $t0 # Store the user's desired integer in $a0 to print.
li $v0, 1 # Print the user's integer.
syscall
la $a0, nf # Prompt the user that the integer wasn't found.
li $v0, 4 # Print the message.
syscall
jr $ra # Return to main.
done:
la $a0, complete # Tells the user that the program is complete.
li $v0, 4 # Prints the message.
syscall
li $v0, 10 # Ends the program.
syscall

.data
amount_num: .asciiz "How many numbers do you have? "
num: .asciiz "Enter a number: "
nl: .asciiz "\n"
array_contains: .asciiz "\nThe array contains the following: \n"
complete: .asciiz "\nprogram complete"
.align 2
array: .space 40
search_num: .asciiz "\nEnter number to search for: "
f: .asciiz " was found at array["
cl_bracket: .asciiz "]"
nf: .asciiz " was not found"
Loading

0 comments on commit 31806bd

Please sign in to comment.