From 31806bdff6d97ed8f8e4713f315e55176f30891d Mon Sep 17 00:00:00 2001 From: "[12NaN]" Date: Sun, 17 Jun 2018 22:19:18 -0400 Subject: [PATCH] Projects All five of my projects from CS14. --- project1.asm | 94 +++++++++++++++ project2.asm | 196 ++++++++++++++++++++++++++++++ project3.asm | 330 +++++++++++++++++++++++++++++++++++++++++++++++++++ project4.asm | 242 +++++++++++++++++++++++++++++++++++++ project5.asm | 253 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 1115 insertions(+) create mode 100644 project1.asm create mode 100644 project2.asm create mode 100644 project3.asm create mode 100644 project4.asm create mode 100644 project5.asm diff --git a/project1.asm b/project1.asm new file mode 100644 index 0000000..337e15f --- /dev/null +++ b/project1.asm @@ -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" + diff --git a/project2.asm b/project2.asm new file mode 100644 index 0000000..148baa8 --- /dev/null +++ b/project2.asm @@ -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" \ No newline at end of file diff --git a/project3.asm b/project3.asm new file mode 100644 index 0000000..fee481c --- /dev/null +++ b/project3.asm @@ -0,0 +1,330 @@ + .text + .globl main +main: + la $a0, first_num # Prompts the user to enter the first integer + li $v0, 4 # Prints the prompt + syscall + + li $v0, 5 # User inputs the first integer + syscall + + move $t0, $v0 # Store the first integer in $t0 + + la $a0, second_num # Prompts the user to enter the second integer + li $v0, 4 # Prints the prompt + syscall + + li $v0, 5 # User inputs the second integer + syscall + + move $t1, $v0 # Store the second integer in $t1 + + la $a0, dnl # dnl = Double newline = "\n\n" + li $v0, 4 # Prints the double newline + syscall + + la $a0, dw_space # dw_space = Double white space = " " + li $v0, 4 # Prints the double white space + syscall + + li $t2, 0 # Loop counter: i = 0 + li $t3, 2 # Loop end (# of integers): i <= 2 + li $t5, 1 # Check if i = 1 (converted the first integer) + # , now convert the second integer +bin_convert: + beq $t2, $t3, print_sum # if(i == 2): print_sum (print '----' before the sum) + + li $t4, 32 # x = 32 (number of bits) + addi $t2, $t2, 1 # Increment loop counter: i++ + + jal load_array # Choose which integer array to use (i=1 (first_int), i=2 (second_int)) + sub $t4, $t4, 1 # x-- = 31 (subscript for the array: a[x]) + beq $t2, $t3, add_num # if(i == 2): add_num (print '+' before the second binary #) + + loop: + blt $t4, $zero, bin_convert # if(x < 0): bin_convert + + srlv $t6, $t0, $t4 # $t6 = $t0 (first integer) >> 31 + sub $t4, $t4, 1 # x-- = 30 + and $t7, $t6, 1 # $t7 = $t6 (1 or 0) && 1 + + jal bits # Print the bits + jal store # Store the bits + + j loop # Repeat + + load_array: + beq $t2, $t5, first_array # if(i == 1): first_array + + second_array: + la $a1, s_array # $a1 = s_array[x] + jr $ra # Return + + first_array: + la $a1, f_array # $a1 = f_array[x] + jr $ra # Return + + add_num: + la $a0, nl # nl = Newline + li $v0, 4 # Print a newline + syscall + + la $a0, plus # plus = '+' + li $v0, 4 # Print '+' + syscall + + la $a0, w_space # w_space = white space = " " + li $v0, 4 # Print the white space + syscall + + move $t9, $t0 # Move the first integer into $t9: $t9 = $t0 + move $t0, $t1 # Move the second integer into the register of the first: $t0 = $t1 + + j loop # Jump to loop. + + bits: + beqz $t7, zero # if(($t6 && 1) == 0): zero (print '0') + + one: + la $a0, o # o = '1' + li $v0, 4 # Print the bit '1' + syscall + + jr $ra # Return + zero: + la $a0, z # z = '0' + li $v0, 4 # Print the bit '0' + syscall + + jr $ra # Return + + store: + beq $t2, $t3, save_second # if(i == 2 (total # of integers)): save_second + + save_first: + sb $t7, 0($a1) # f_array[x] = $t7 (a bit (1 or 0)) + addi $a1, $a1, 4 # Increment the array: f_array[x+1] + + jr $ra # Return + + save_second: + sb $t7, 0($a1) # s_array[x] = $t7 (a bit (1 or 0)) + addi $a1, $a1, 4 # Increment the array: s_array[x+1] + + jr $ra # Return + + print_sum: + la $a0, equals # Print the equals line + li $v0, 4 # Print the line + syscall + + la $a0, dw_space # dw_space = double white space = " " + li $v0, 4 # Print dw_space + syscall + + j sum_bin # Jump to sum_bin +sum_bin: + li $t4, 31 # Number of bits in the array + li $s3, 0 # Set carry to 0 + + la $a1, f_array # $a1 = f_array[x] + la $a2, s_array # $a2 = s_array[x] + la $a3, sum_array # $a3 = sum_array[x] + + addi $a1, $a1, 124 # Start at the end of the array: f_array[124] + addi $a2, $a2, 124 # Start at the end of the array: s_array[124] + addi $a3, $a3, 124 # Start at the end of the array: sum_array[124] + + sum_loop: + blt $t4, $zero, sum_print # if($t4 < 0): sum_print + + lb $s0, ($a1) # $s0 = f_array[x] + lb $s1, ($a2) # $s1 = s_array[x] + + xor $s2, $s0, $s1 # $s2 = $s0(1 or 0) ^ $s1(1 or 0) + xor $s2, $s2, $s3 # $s2 = $s2 ($s0 ^ $s1) ^ $s3 (carry) + + jal carry # Set carry + + move $s5, $s0 # $s5 = $s0 (bit from f_array) + move $s6, $s1 # $s6 = $s1 (bit from s_array) + + sb $s2, ($a3) # sum_array[x] = $s2 + + sub $t4, $t4, 1 # x-- = 31 - 1 = 30 (Number of bits in the array) + + sub $a1, $a1, 4 # f_array[x-1] + sub $a2, $a2, 4 # s_array[x-1] + sub $a3, $a3, 4 # sum_array[x-1] + + j sum_loop # Repeat + + sum_print: + li $t4, 31 # Number of bits in the array + la $a3, sum_array # $a3 = sum_array[x] + + sum_print_loop: + blt $t4, $zero, hex_convert # if($t4 < 0): hex_convert + + lb $s2, ($a3) # $s2 = sum_array[x] + addi $a3, $a3, 4 # sum_array[x+1] + + sub $t4, $t4, 1 # x-- = 31 - 1 = 30 + + move $t7, $s2 # Move the bit from sum_array into $t7: $t7 = $s2 + jal bits # Print the bits + move $s2, $t7 # Move the bit back into $s2: $s2 = $t7 + + j sum_print_loop # Repeat + + carry: + beq $s0, $s1, carry_one # if($s0(first_int_bit) == $s1(second_int_bit)): carry_one + beq $s2, $s3, carry_one_2 # if($s2(sum_array_bit) == $s3(carry)): carry_one_2 + + jr $ra # Return + + carry_one: + beqz $s0, check # if($s0(first_int_bit) == 0): check + li $s3, 1 # Set carry to 1 + + jr $ra # Return + + carry_one_2: # Carry again + li $s3, 1 # Set carry to 1 + + jr $ra # Return + + check: + xor $s7, $s5, $s6 # $s7 = $s5(1 or 0) ^ $s6(1 or 0) + beq $s7, $s0, reset # if(($s5 ^ $s6) == $s0): reset + + li $s3, 0 # Set carry to 0 + jr $ra # Return + + reset: + li $s3, 0 # Reset carry + jr $ra # Return + + return: + jr $ra # Return +hex_convert: + li $t2, 0 # Loop counter: i = 0 + li $t3, 2 # Loop end (# of integers): i <= 2 + li $t5, 1 # Check if i = 1 (converted the first integer) + li $t4, 32 # Number of bits + li $a2, 4 # Size of hex digit + + move $t1, $t0 # Move the second integer in $t0 back into $t1: $t1 = $t0 + move $t0, $t9 # Move the first integer in $t9 back into $t0: $t0 = $t9 + + li $t7, 1 # Set $t7 = 1 + sllv $t7, $t7, $a2 # $t7 = $t7 << 4 + sub $t7, $t7, 1 # $t7 = $t7 - 1 + + la $a1, hex_digits # $a1 = hex_digits = "0123...DEF" + la $a0, hex_mess # Prompts the user that the program will now print the numbers in hex + li $v0, 4 # Prints the message + syscall + + la $a0, dw_space # dw_space = double white space = " " + li $v0, 4 # Print dw_space + syscall + + conversion: + beq $t2, $t3, done # if(i == $t3): done (end program) + beqz $t2, first_hex_num # if(i == 0): first_hex_num (Convert the first integer) + addi $t2, $t2, 1 # i++ + + li $t4, 32 # Number of bits + + la $a0, nl # nl = newline + li $v0, 4 # Print nl + syscall + + la $a0, plus # plus = '+' + li $v0, 4 # Print '+' + syscall + + la $a0, w_space # w_space = white space = " " + li $v0, 4 # Print the white space + syscall + + la $a3, hex_sarray # $a3 = hex_sarray + move $t6, $t1 # Move the second integer into $t6: $t6 = $t1 + + hex_loop: + and $t8, $t6, $t7 # $t8 = $t6(1 or 0) && $t7(1 or 0) + lb $t8, hex_digits($t8) # $t8 = hex_digits($t8) (Gets the hex digit) + + sub $a3, $a3, 1 # Decrement the array by 1: hex_a[x-1] + sb $t8, 0($a3) # hex_a[x-1] = $t8 + + srlv $t6, $t6, $a2 # $t6 = $t6 (integer(first or second)) >> 4 + sub $t4, $t4, $a2 # $t4 = 32 - 4 = 28 + bgtz $t4, hex_loop # if($t4 > 0): hex_loop + + move $a0, $a3 # Move $a3 (array of the integer in hex) to $a0 + li $v0, 4 # Print the integer in hex + syscall + + bgt $t2, $t3, done # if($t2 > $t3): done + beq $t2, $t3, print_hex_sum # if($t2 == $t3): print_hex_sum + + j conversion # Jump to conversion + + first_hex_num: + addi $t2, $t2, 1 # i++ + la $a3, hex_farray # $a3 = hex_farray + move $t6, $t0 # Move the first integer into $t6: $t6 = $t0 + + j hex_loop # Jump to hex_loop + + print_hex_sum: + la $a0, equals # Print the equals line + li $v0, 4 # Print the line + syscall + + la $a0, dw_space # dw_space = double white space + li $v0, 4 # Print dw_space + syscall + + j sum_hex_num # Jump to sum_hex_num (Print the sum in hex) + + sum_hex_num: + la $a3, hex_sum_array # $a3 = hex_sum_array + addi $t2, $t2, 1 # i++ + add $t9, $t0, $t1 # $t9 (sum) = $t0 (integer 1) + $t1 (integer 2) + + move $t6, $t9 # Move the sum of the integers in $t9 into $t6: $t6 = $t9 + li $t4, 32 # Reset $t4 to 32 (Number of bits) + + j hex_loop # Jump to hex_loop + +done: + la $a0, complete # Prompt the user that the program is complete. + li $v0, 4 # Print the message. + syscall + + li $v0, 10 # Exit the program + syscall + + .data +first_num: .asciiz "Enter the first number: " +second_num: .asciiz "Enter the second number: " +o: .asciiz "1" +z: .asciiz "0" +nl: .asciiz "\n" +dnl: .asciiz "\n\n" +f_array: .space 128 +s_array: .space 128 +sum_array: .space 128 +complete: .asciiz "\n\nprogram complete" +plus: .asciiz "+" +w_space: .asciiz " " +dw_space: .asciiz " " +equals: .asciiz "\n------------------------------------------\n" +hex_mess: .asciiz "\n\nIn Hex: \n" +hex_farray: .space 32 +hex_sarray: .space 32 +hex_sum_array: .space 32 +hex_digits: .asciiz "0123456789ABCDEF" \ No newline at end of file diff --git a/project4.asm b/project4.asm new file mode 100644 index 0000000..6202abb --- /dev/null +++ b/project4.asm @@ -0,0 +1,242 @@ + .text + .globl main +main: + jal allwork # Start the program + + li $v0, 10 # Exit the program + syscall + +allwork: + sw $ra, ($sp) # Push the return address of main to the top of the stack: s[0] = $ra1 + jal input # Ask the user for input a, b, c + jal check # Check if the points make a right triangle + jal print # Print the results + +input: + addi $sp, $sp, -16 # Decrement the stack: s[4] + sw $ra, ($sp) # Push '$ra2' to s[4]: s[4] = $ra2 + addi $sp, $sp, 16 # Return to s[0] + + li $t4, 1 # Check for integer 1: j = 1 + li $t5, 2 # Check for integer 2: j = 2 + li $t6, 3 # Check for the last integer / End loop: j <= 3 + li $s6, 0 # Check if we've set the stack to 'a' + + first_num: + + la $a0, enterNum1 # Prompts the user to enter an integer for 'a' + li $v0, 4 # Prints the prompt + syscall + + li $v0, 5 # Take the input for 'a' + syscall + + move $t0, $v0 # $t0 = $v0 = a (Mutable) + move $s3, $v0 # $s3 = $v0 = A (Immutable) + + move $t7, $t0 # ex: x = integer 1 (Mutable) + move $t9, $t0 # ex: X = integer 1 (Immutable) + + addi $sp, $sp, -4 # Decrement the stack: s[1] + sw $v0, ($sp) # Push 'a' to s[1]: s[1] = a + + jal pow # Get the exponent: a^2 + b^2 = c^2 + + second_num: + + move $s0, $t7 # Store a^2 in $s0 + + la $a0, enterNum2 # Prompts the user to enter an integer for 'b' + li $v0, 4 # Prints the prompt + syscall + + li $v0, 5 # Take the input for 'b' + syscall + + move $t1, $v0 # $t1 = $v0 = b (Mutable) + move $s4, $v0 # $s4 = $v0 = B (Immutable) + + move $t7, $t1 # ex: y = integer 2 (Mutable) + move $t9, $t1 # ex: Y = integer 2 (Immutable) + + addi $sp, $sp, -4 # Decrement the stack: s[2] + sw $v0, ($sp) # Push 'b' to s[2]: s[2] = b + + jal pow # Get the exponent: a^2 + b^2 = c^2 + + last_num: + + move $s1, $t7 # Store b^2 in $s1 + + la $a0, enterNum3 # Prompts the user to enter an integer for 'c' + li $v0, 4 # Prints the prompt + syscall + + li $v0, 5 # Take the input for 'c' + syscall + + move $t2, $v0 # $t2 = $v0 = c (Mutable) + move $s5, $v0 # $s5 = $v0 = C (Immutable) + + move $t7, $t2 # ex: z = integer 3 (Mutable) + move $t9, $t2 # ex: Z = integer 3 (Immutable) + + addi $sp, $sp, -4 # Decrement the stack: s[3] + sw $v0, ($sp) # Push 'c' to s[3]: s[3] = c + + jal pow # Get the exponent: a^2 + b^2 = c^2 + + move $s2, $t7 # Store c^2 in $s2 + + addi $sp, $sp, -4 # Decrement the stack: s[4] + lw $ra, ($sp) # Pop $ra2 and store it in $ra: $ra = $ra2 + addi $sp, $sp, 4 # Return to s[3] + + jr $ra # Return to allwork + +pow: + set: + li $t3, 1 # i = 1 + li $t8, 2 # i <= 2 + li $s6, 0 # Set to 0 to return to 'a' in the stack + + j loop # Go to loop + + get_num: + addi $t4, $t4, 1 # Next integer: j++ + + jr $ra # Jump back to input + + loop: + addi $t3, $t3, 1 # i++ + + mult $t7, $t9 # ex: x*X = x^2 + mflo $t7 # ex: x^2 + + beq $t3, $t8, get_num # if(i == 2): get_num + + j loop # Loop again + +check: + beqz $s6, set_to_a # if($s6 == 0): set_to_a (set stack to 'a') + bgt $t0, $t2, swap_a_c # if('a' > 'c'): swap_a_c + bgt $t1, $t2, swap_b_c # if('b' > 'c'): swap_b_c + + jr $ra # Return to Allwork + + swap_a_c: + lw $t0, ($sp) # Set $t0 = s[1] = a + addi $sp, $sp, -8 # Decrement the stack: s[3] = c + + lw $t2, ($sp) # Set $t2 = s[3] = c + sw $t0, ($sp) # Swap: s[3] = a + + addi $sp, $sp, 8 # Increment stack: s[1] = a + sw $t2, ($sp) # Swap: s[1] = c + + move $t0, $t2 # Swap: a = c + move $t2, $s3 # Swap: c = A = a + + move $s7, $s0 # Swap: temp = a^2 + move $s0, $s2 # Swap: a^2 = c^2 + move $s2, $s7 # Swap: c^2 = temp = a^2 + + j check # Check again + + swap_b_c: + addi $sp, $sp, -4 # Decrement the stack: s[2] = b + lw $t1, ($sp) # Set $t1 = s[2] = b + + addi $sp, $sp, -4 # Decrement the stack: s[3] = c + lw $t2, ($sp) # Set $t2 = s[3] = c + + sw $t1, ($sp) # Swap: s[3] = b + addi $sp, $sp, 4 # Increment the stack: s[2] = b + sw $t2, ($sp) # Swap: s[2] = c + addi $sp, $sp, 4 # Increment the stack: s[1] = a + + move $t1, $t2 # Swap: b = c + move $t2, $s4 # Swap: c = B = b + + move $s7, $s1 # Swap: temp = b^2 + move $s1, $s2 # Swap: b^2 = c^2 + move $s2, $s7 # Swap: c^2 = temp = b^2 + + j check # Check again + + set_to_a: + li $s6, 1 # Checks if we've set the stack to 'a' + addi $sp, $sp, 8 # Stack is at the location of 'a' + j check # Jump to check + +print: + add $s8, $s0, $s1 # total = $s8 = a^2 + b^2 + beq $s8, $s2, right # if(total == c^2): right + + move $a0, $s3 # $s3 = A = a + li $v0, 1 # Print A + syscall + + la $a0, comma # comma (,) + li $v0, 4 # Print a comma + syscall + + move $a0, $s4 # $s4 = B = b + li $v0, 1 # Print B + syscall + + la $a0, comma # comma (,) + li $v0, 4 # Print a comma + syscall + + move $a0, $s5 # $s5 = C = c + li $v0, 1 # Print C + syscall + + la $a0, not_right # Print that it is not a right triangle + li $v0, 4 # Print the message + syscall + + addi $sp, $sp, 4 # Increment the stack: s[0] = $ra1 + lw $ra, ($sp) # Pop the return address of function 'main' into $ra + + jr $ra # Return to main and exit the program + + right: + move $a0, $s3 # $s3 = A = a + li $v0, 1 # Print A + syscall + + la $a0, comma # comma (,) + li $v0, 4 # Print a comma + syscall + + move $a0, $s4 # $s4 = B = b + li $v0, 1 # Print B + syscall + + la $a0, comma # comma (,) + li $v0, 4 # Print a comma + syscall + + move $a0, $s5 # $s5 = C = c + li $v0, 1 # Print C + syscall + + la $a0, right_tri # It is a right triangle + li $v0, 4 # Print the message + syscall + + addi $sp, $sp, 4 # Increment the stack: s[0] = $ra1 + lw $ra, ($sp) # Pop the return address of function 'main' into $ra + + jr $ra # Return to main and exit the program + + .data +enterNum1: .asciiz "Enter value for side A: " +enterNum2: .asciiz "Enter value for side B: " +enterNum3: .asciiz "Enter value for side C: " +right_tri: .asciiz " - is a right triangle\n" +not_right: .asciiz " - is not a right triangle\n" +nl: .asciiz "\n" +comma: .asciiz ", " \ No newline at end of file diff --git a/project5.asm b/project5.asm new file mode 100644 index 0000000..74a3cf7 --- /dev/null +++ b/project5.asm @@ -0,0 +1,253 @@ + .text + .globl main +main: + la $a0, sign_integer # Prompts the user to enter a signed integer + li $v0, 4 # Print the prompt + syscall + + li $v0, 5 # Enter the signed integer: ex: -3 + syscall + + beqz $v0, done # if($v0 == 0): done + move $t0, $v0 # else: x = $v0 + + li $s1, 0x80000000 # Bitmask to get sign + and $a3, $t0, $s1 # $a3 = $t0 && $s1 = -3 && 0x80000000 = 1 && 1 = 1 + + jal sign_check # Check the sign in $a3 + + la $a0, decimal_num # Prompts the user to enter a decimal number in binary + li $v0, 4 # Prints the prompt + syscall + + la $a0, dec_bin_string # $a0 = dec_bin_string (stores the binary string) + li $a1, 32 # Size of the bin_string + li $v0, 8 # Reads the users input as a string. + syscall + + bltz $t0, make_positive # if($t0 < 0): (i.e. a negative #) make_positive + + j print_sign_bit # Print the sign bit + +sign_check: + beqz $a3, zero_sign # if($a3 == 0): zero_sign (0 for sign bit) + li $a3, 1 # else: 1 for sign bit + jr $ra # return to main + + zero_sign: + li $a3, 0 # $a3 = 0 (0 for sign bit) + jr $ra # return to main +make_positive: + li $s6, -1 # $s6 = -1 + + mult $t0, $s6 # ex: -3 * -1 = 3 + mflo $t0 # ex: x = 3 + +print_sign_bit: + la $a0, sign_bit # Prompt "sign bit = " + li $v0, 4 # Print the prompt + syscall + + beqz $a3, print_zero_sign_bit # if($a3 == 0): print_zero_sign_bit + + la $a0, one # ex: $a0 = $a3 = 1 (sign bit) + li $v0, 4 # Print the sign bit + syscall + + sll $a3, $a3, 31 # ex: $a3 = 1 << 31 = 1000 0000 0000 0000 0000 0000 0000 0000 + + j decimal_point # Jump to decimal_point + +print_zero_sign_bit: + la $a0, zero # ex: $a0 = $a3 = 0 (sign bit) + li $v0, 4 # Print the sign bit + syscall + + sll $a3, $a3, 31 # ex: $a3 = 0 << 31 = 0000 0000 0000 0000 0000 0000 0000 0000 + +decimal_point: + la $a0, dec_bin_string # $a0 = dec_bin_string + li $a1, 0x2e # hex for decimal point = '.' + + lb $t2, 0($a0) # if dec_bin_string has a decimal point $t2 = . = $a0[0] + bne $t2, $a1, conversion # if($t2 != '.'): conversion + + li $t1, 0 # i = 0 + la $a1, dec_bin_string # $a1 = dec_bin_string with decimal point = .101 + addi $a0, $a0, 1 # $a0[1] = dec_bin_string without decimal point = 101 + + without_decimal_point: + bgt $t1, 32, conversion # if(i > 32): conversion + + lb $t2, 0($a0) # ex: $t2 = $a0[1] = 1 + sb $t2, 0($a1) # ex: $a1[0] = 1 + + addi $a0, $a0, 1 # $a0[1 + 1] = $a0[2] + addi $a1, $a1, 1 # $a1[0 + 1] = $a1[1] + addi $t1, $t1, 1 # i++ + + j without_decimal_point # for(int i = 0; i <= 32; i++): repeat + +conversion: + la $a0, dec_bin_string # $a0 = dec_bin_string + la $a1, store_dec_bin_string # $a1 = store_dec_bin_string + li $t1, 0 # i = 0 + li $s1, 0x01010101 # Bitmask to find the exponent + + exponent: + bgt $t1, 8, exponent_digits # if(i > 8 (# of bits for exponent)): exponent_digits + + lw $t2, 0($a0) # $t2 = $a0[0] + and $t2, $t2, $s1 # ex: $t2 = $t2 && $s1 = 1 && 1 = 1 + sw $t2, 0($a1) # $a1[0] = $t2 + + addi $a0, $a0, 4 # $a0[0 + 1] = $a0[1] + addi $a1, $a1, 4 # $a1[0 + 1] = $a1[1] + addi $t1, $t1, 1 # i = i + 1 + + j exponent # Repeat + + exponent_digits: + la $t1, store_dec_bin_string # $t1 = store_dec_bin_string + li $t2, 31 # Total number of bits: 0 -> 31 + li $s6, 0 # y = 0 + + get_exponent_digits: + bltz $t2,fraction # if($t2 < 0): fraction + + lb $t3, 0($t1) # $t3 = $t1[0] + sllv $t3, $t3, $t2 # ex: $t3 = $t3 << $t2 = 1 << 31 = 1000...0000 (31 zeros following 1) + or $s6, $s6, $t3 # ex: $s6 = 0 || 1 = 1 + + add $t1, $t1, 1 # $t1[0+1] = $t1[1] + sub $t2, $t2, 1 # $t2 = $t2 - 1 = 31 - 1 + + j get_exponent_digits # Repeat + + fraction: + move $t2, $t0 # $t2 = signed integer + li $t1, 32 # i = 32 + li $s1, 0x80000000 # Bit mask to get the fraction bits + + fraction_digits: + and $t3, $t2, $s1 # $t3 = $t2 && $t1 = 1 && 1 = 1 + beq $t3, $s1, get_fraction_digits # if($t3 == 8): get_fraction_digits + + sll $t2, $t2, 1 # ex: $t2 = $t2 << 1 = 1 << 1 = 10 + sub $t1, $t1, 1 # i-- = i - 1 = 32 - 1 = 31 + + j fraction_digits # Repeat + + get_fraction_digits: + move $t2, $s6 # ex: $t2 = 0 + + sub $t3, $t1, 1 # $t3 = 8 - 1 + srlv $t2, $t2, $t3 # ex: $t2 = $t2 >> $t3 = 0 >> 7 = 0000 0000 + li $s3, 33 # $s3 = 33 + + sub $s3, $s3, $t1 # $s3 = 33 - 8 + sllv $t3, $t0, $s3 # ex: $t3 = 1 << 25 = 1000 0000 0000 0000 0000 0000 0 + + or $s3, $t3, $t2 # ex: $s3 = $t3 || $t2 = 1 || 0 = 1 + srl $a2, $s3, 9 # ex: $a2 = 1 >> 9 = 00 0000 0001 + + print_exponent_in_decimal: + # exponent - biased + sub $t1, $t1, 1 # exponent - 1 + add $a1,$t1,127 # $a1 = exponent + 127 + + la $a0, dec_point # Prompt the user: "Exponent in decimal =" + li $v0, 4 # Print the prompt + syscall + + move $a0, $a1 # $a0 = $a1 = exponent in decimal + li $v0, 1 # Print the exponent in decimal + syscall + + sll $a1, $a1, 23 # ex: $a1 = $a1 << 23 = 1 << 23 = 1000 0000 0000 0000 0000 0000 + + or $s0, $a3, $a1 # $s0 = $a3(sign bit) || $a1(exponent) + or $s0, $s0, $a2 # $s0 = $s0 || $a2(fraction) + +floating_point: + la $a0, bits_float # Prompt the user: "All bits of the floating point number are:" + li $v0, 4 # Print the prompt + syscall + + li $t1, 0 # i = 0 + move $t2, $s0 # $t2 = floating point number + + li $t3, 0x80000000 # bit mask for floating point bits + li $s7, 0 # j = 0 + li $t8, 4 # j == 4 + + print_floating_point_bits: + beq $t1, 32, print_floating_point_num # if(i == 32): print_floating_point_num + beq $s7, $t8, bit_space # if(j == 4): bit_space (print white space after printing four bits = a nibble) + + and $a0, $t2, $t3 # ex: $a0 = $t2 && $t3 = 1 && 0 = 0 + srl $a0, $a0, 31 # ex: $a0 = 0 >> 31 = 0000 ... 0000 (31 0 bits following 0 ($a0)) + beqz $a0, print_zero # if($a0 == 0): print_zero + + la $a0, one # else: print "1" + li $v0, 4 # print "1" + syscall + + sll $t2, $t2, 1 # ex: $t2 = 1 << 1 = 10 + addi $s7, $s7, 1 # j = j + 1 + addi $t1, $t1, 1 # i = i + 1 + + j print_floating_point_bits # Repeat + + print_zero: + la $a0, zero # $a0 = "0" + li $v0, 4 # Print "0" + syscall + + sll $t2, $t2, 1 # ex: $t2 = 1 << 1 = 10 + addi $s7, $s7, 1 # j = j + 1 + addi $t1, $t1, 1 # $t1 = $t1 + 1 + + j print_floating_point_bits # Jump to floating_point_bits + + bit_space: + li $s7, 0 # Reset j to 0 + + la $a0, white_space # $a0 = " " + li $v0, 4 # Print the whitespace + syscall + + j print_floating_point_bits # Jump to floating_point_bits + + print_floating_point_num: + la $a0, float_point_num # Prompt the user: "The floating point number is: " + li $v0, 4 # Print the prompt + syscall + + mtc1 $s0, $f12 # ex: float = $s0 = 1100 0000 0110 1000 0000 0000 0000 0000 + li $v0, 2 # System call to print a floating point integer + syscall + +done: + la $a0, complete # Prompts the user that the program is complete + li $v0, 4 # Print the prompt + syscall + + li $v0, 10 # Exit the program + syscall + + .data +sign_integer: .asciiz "Enter signed integer part.\n" +decimal_num: .asciiz "Enter fraction as a binary string.\n" +nl: .asciiz "\n" +sign_bit: .asciiz "\nsign bit = " +dec_point: .asciiz "\nExponent in decimal = " +bits_float: .asciiz "\nAll bits of the floating point number are:\n" +float_point_num: .asciiz "\nThe floating point number is:\n" + .align 2 +dec_bin_string: .space 128 +store_dec_bin_string: .space 128 +one: .asciiz "1" +zero: .asciiz "0" +white_space: .asciiz " " +complete: .asciiz "\n\nProgram Complete" \ No newline at end of file