Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- charArray: .space 40
- postfixArray: .space 40
- char: .space 2
- menuPrompt: .asciiz "Arithmetic (1), Other Operations (2), or Eixt (3)?"
- menuError: .asciiz "Invalid input. Please enter either 1, 2, or 3."
- arithmeticPrompt: .asciiz "Please input an equation without spaces.\nAllowable characters are ( ) + - * / and 0 through 9\n"
- null: .asciiz ""
- intPrompt: .asciiz "Please input an integer: "
- .text
- menu: # outputs the main menu and breaks to the appropriate point
- ori $v0, $zero, 4 # load system call for print_string
- la $a0, menuPrompt # loads the main menu's address for the system call
- syscall # prints the string "Arithmetic (1), Other Operations (2), or Eixt (3)?"
- ori $v0, $zero, 5 # load system call for read_int
- syscall # reads the input integer and stores it in $v0
- beq $v0, 1, arithmetic # if a 1 was entered, go to aritmetic
- beq $v0, 2, otherOps # if a 2 was entered, go to otherOps
- beq $v0, 3, exit # if a 3 was entered, got to exit
- ori $v0, $zero, 4 # load system call for print string
- la $a0, menuError # loads the error message's address for the system call
- syscall # prints the string "Invalid input. Please enter either 1, 2, or 3."
- j menu # restarts menu to allow another selection to be made
- arithmetic: # takes a string input, breaks it into its characters, converts it to a postfix expression, and evaluates the expression
- or $t9, $zero, $zero # initialize $t9 as the counter for charArray offset
- lb $t8, null # store a null character in $t8 for comparison to see if done parsing the string
- ori $v0, $zero, 4 # load system call for print_string
- la $a0, arithmeticPrompt # loads the arithmetic prompt's address for the system call
- syscall # prints the string "Please input an equation without spaces./nAllowable characters are ( ) + - * / and 0 through 9"
- getCharLoop:
- ori $v0, $zero, 8 # load system call for read_string
- la $a0, char # designates the memory allocated at char as the buffer
- ori $a1, $zero, 2 # sets the length of the string to two, 1 byte char, 1 byte null
- syscall # stores the char byte from the input buffer into char
- lb $t0, char # load the char byte from the char buffer into $t0, stripping the null
- sb $t0, charArray($t9) # move the char in $t0 into the character array, offset by the counter in $t9
- addi $t9, $t9, 1 # increment the counter in $t9 to the next offset for the charArray
- bne $t0, $t8, getCharLoop # if we haven't reached the terminal null character yet, continue reading in characters
- # continuing here means that we have reached the null character in the getCharLoop, so the whole string is in charArray
- or $t9, $zero, $zero # initialize $t9 as the counter for charArray offset for postfixLoop
- ori $t7, $zero, -4 # initialize $t7 as the counter for postfixArray offset for postfixLoop. Initialized at -4 based on where I increment
- or $fp, $sp, $zero # initialize $fp to point to the top of the stack so I can determine when the stack is empty
- postfixLoop: # convert to a postfix expression ie (1+2)*3 converted to 1 2 + 3 *
- # digits 0-9 are 48 - 57, (, ), *, + are 40-43, - is 45, / is 47 using ASCII
- lb $t0, charArray($t9) # loads the character in charArray[$t9] into $t0
- beq $t0, $t8, evaluate # compares the character returned to the null in $t8 to see if we've reached the end of the array
- blt $t0, 40, error # a character other than those allowed has been entered
- blt $t0, 48, operator # if the character is between 40 and 48, it must either be an operator or an invalid entry
- bgt $t0, 57, error # a character other than those allowed has been entered
- digit: # if the char is a digit, get all the digits before the next operator, convert to int, and append to postfixArray
- addi $t7, $t7, 4 # increments the postfixArray index counter in $t7 to the next index. Steps of 4 since it's storing ints.
- addi $t0, $t0, -48 # converts the char to an int by subtracting 48 from the ASCII value of the char
- sw $t0, postfixArray($t7) # stores the integer value in $t0 into the postfixArray
- nextDigitLoop: # keeps checking the next char to see if it's also a digit. If so, multiply last value by 10, add new digit, and store.
- addi $t9, $t9, 1 # increments the charArray index counter in $t9 to look at the next char
- lw $t1, charArray($t9) # loads the next character into $t1
- blt $t1, 48, postfixLoop # if the next character is less than ASCII 48, it's not a digit, so go back to postfix
- bgt $t1, 57, postfixLoop # if the next character is greater than ASCII 57, it's not a digit, so got back to postfix
- addi $t1, $t1, -48 # converts the char to an int by subtracting 48 from the ASCII value of the char
- mul $t0, $t0, 10 # multiplies the value in $t0 by 10 since there's another digit following it
- add $t0, $t0, $t1 # adds the value in $t0 with the value in $t1 to have a multi-digit integer represented
- sw $t0, postfixArray($t7) # overwrites the value in postfixArray[$t7] with the multi-digit integer
- j nextDigitLoop # jumps back to check if the next char is also a digit
- operator: # makes use of the stack and a set of rules to determine when to add the different operators to the postfixArray
- beq $fp, $sp, push # if the stack is empty, push the operator onto the stack
- beq $t0, 40, push # if the operator is a left parenthesis, push the operator onto the stack
- lb $t1, ($sp) # peeks at the item at the top of the stack and puts it in $t1 for comparison
- beq $t1, 40, push # if the top of the stack is a left parenthesis, push the operator onto the stack
- # if the token is a right parenthesis, pop the stack and append all operators to the list until a left parenthesis is popped. Discard the pair of parentheses.
- # if the token has higher precedence than the top of the stack, push it on the stack. For our purposes, the only operators are +, -, *, /, where the latter two have higher precedecence than the first two.
- # if the token has equal precedence with the top of the stack, pop and append the top of the stack to the list and then push the incoming operator.
- # if the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and append it to the list. Then repeat the above tests against the new top of stack
- push:
- evaluate: # evaluate the postfix expression by iterating through each item in the postfish expression stack
- # it it is a null, pop the last element off the stack and output it
- # if it is an operator, pop two items off the stack and operate on them, then push the result
- # if it is a digit, push it onto the stack
- error:
- otherOps:
- decimalToBinary:
- li $v0, 4 # print string system call
- la $a0, intPrompt # prompt: "Please input an integer: "
- syscall # issue system call
- li $v0, 5 # read integer system call
- syscall # issue system call
- or $t0, $v0, $zero # move the integer into $t0
- li $t1, 1 # initialize the mask
- li $t3, 31 # initialize the counter
- sll $t1, $t1, 31 # shift the mask to the MSB
- loop: # iterates through and prints each bit
- and $t2, $t0, $t1 # and with mask to store the output bit in $t2
- srlv $t2, $t2, $t3 # shift the output bit to the LSB
- li $v0, 1 # print integer system call
- or $a0, $t2, $zero # move the output bit into $a0
- syscall # issue system call
- beq $t3, $zero, exit # all bits have been output
- addi $t3, $t3, -1 # decrement the counter
- srl $t1, $t1, 1 # shift the mask bit right one
- j loop # restart the loop
- exit:
- ori $v0, $zero, 10 # load system call for exit
- syscall # ends the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement