Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .data
  2.     charArray:      .space      40
  3.     postfixArray:       .space      40
  4.     char:           .space      2
  5.     menuPrompt:     .asciiz     "Arithmetic (1), Other Operations (2), or Eixt (3)?"
  6.     menuError:      .asciiz     "Invalid input. Please enter either 1, 2, or 3."
  7.     arithmeticPrompt:   .asciiz     "Please input an equation without spaces.\nAllowable characters are ( ) + - * / and 0 through 9\n"
  8.     null:           .asciiz     ""
  9.     intPrompt:      .asciiz     "Please input an integer: "
  10. .text
  11.     menu:                   # outputs the main menu and breaks to the appropriate point
  12.         ori $v0, $zero, 4       # load system call for print_string
  13.         la $a0, menuPrompt      # loads the main menu's address for the system call
  14.         syscall             # prints the string "Arithmetic (1), Other Operations (2), or Eixt (3)?"
  15.        
  16.         ori $v0, $zero, 5       # load system call for read_int
  17.         syscall             # reads the input integer and stores it in $v0
  18.        
  19.         beq $v0, 1, arithmetic      # if a 1 was entered, go to aritmetic
  20.         beq $v0, 2, otherOps        # if a 2 was entered, go to otherOps
  21.         beq $v0, 3, exit        # if a 3 was entered, got to exit
  22.        
  23.         ori $v0, $zero, 4       # load system call for print string
  24.         la $a0, menuError       # loads the error message's address for the system call
  25.         syscall             # prints the string "Invalid input. Please enter either 1, 2, or 3."
  26.         j menu              # restarts menu to allow another selection to be made
  27.    
  28.     arithmetic:             # takes a string input, breaks it into its characters, converts it to a postfix expression, and evaluates the expression       
  29.         or $t9, $zero, $zero        # initialize $t9 as the counter for charArray offset
  30.         lb $t8, null            # store a null character in $t8 for comparison to see if done parsing the string
  31.            
  32.         ori $v0, $zero, 4       # load system call for print_string
  33.         la $a0, arithmeticPrompt    # loads the arithmetic prompt's address for the system call
  34.         syscall             # prints the string "Please input an equation without spaces./nAllowable characters are ( ) + - * / and 0 through 9"
  35.        
  36.         getCharLoop:
  37.             ori $v0, $zero, 8       # load system call for read_string
  38.             la $a0, char            # designates the memory allocated at char as the buffer
  39.             ori $a1, $zero, 2       # sets the length of the string to two, 1 byte char, 1 byte null
  40.             syscall             # stores the char byte from the input buffer into char
  41.        
  42.             lb $t0, char            # load the char byte from the char buffer into $t0, stripping the null
  43.             sb $t0, charArray($t9)      # move the char in $t0 into the character array, offset by the counter in $t9
  44.             addi $t9, $t9, 1        # increment the counter in $t9 to the next offset for the charArray
  45.             bne $t0, $t8, getCharLoop   # if we haven't reached the terminal null character yet, continue reading in characters
  46.            
  47.             # continuing here means that we have reached the null character in the getCharLoop, so the whole string is in charArray
  48.             or $t9, $zero, $zero        # initialize $t9 as the counter for charArray offset for postfixLoop
  49.             ori $t7, $zero, -4      # initialize $t7 as the counter for postfixArray offset for postfixLoop. Initialized at -4 based on where I increment
  50.             or $fp, $sp, $zero      # initialize $fp to point to the top of the stack so I can determine when the stack is empty
  51.            
  52.         postfixLoop:            # convert to a postfix expression ie (1+2)*3 converted to 1 2 + 3 *
  53.                             # digits 0-9 are 48 - 57, (, ), *, + are 40-43, - is 45, / is 47 using ASCII
  54.             lb $t0, charArray($t9)      # loads the character in charArray[$t9] into $t0
  55.            
  56.             beq $t0, $t8, evaluate      # compares the character returned to the null in $t8 to see if we've reached the end of the array
  57.             blt $t0, 40, error      # a character other than those allowed has been entered
  58.             blt $t0, 48, operator       # if the character is between 40 and 48, it must either be an operator or an invalid entry
  59.             bgt $t0, 57, error      # a character other than those allowed has been entered
  60.            
  61.             digit:              # if the char is a digit, get all the digits before the next operator, convert to int, and append to postfixArray
  62.                 addi $t7, $t7, 4        # increments the postfixArray index counter in $t7 to the next index. Steps of 4 since it's storing ints.
  63.                 addi $t0, $t0, -48      # converts the char to an int by subtracting 48 from the ASCII value of the char
  64.                 sw $t0, postfixArray($t7)   # stores the integer value in $t0 into the postfixArray
  65.                
  66.                 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.
  67.                     addi $t9, $t9, 1        # increments the charArray index counter in $t9 to look at the next char
  68.                     lw $t1, charArray($t9)      # loads the next character into $t1
  69.                     blt $t1, 48, postfixLoop    # if the next character is less than ASCII 48, it's not a digit, so go back to postfix
  70.                     bgt $t1, 57, postfixLoop    # if the next character is greater than ASCII 57, it's not a digit, so got back to postfix
  71.                     addi $t1, $t1, -48      # converts the char to an int by subtracting 48 from the ASCII value of the char
  72.                     mul $t0, $t0, 10        # multiplies the value in $t0 by 10 since there's another digit following it
  73.                     add $t0, $t0, $t1       # adds the value in $t0 with the value in $t1 to have a multi-digit integer represented
  74.                     sw $t0, postfixArray($t7)   # overwrites the value in postfixArray[$t7] with the multi-digit integer
  75.                     j nextDigitLoop         # jumps back to check if the next char is also a digit
  76.  
  77.             operator:           # makes use of the stack and a set of rules to determine when to add the different operators to the postfixArray
  78.                 beq $fp, $sp, push      # if the stack is empty, push the operator onto the stack
  79.                 beq $t0, 40, push       # if the operator is a left parenthesis, push the operator onto the stack
  80.                 lb $t1, ($sp)           # peeks at the item at the top of the stack and puts it in $t1 for comparison
  81.                 beq $t1, 40, push       # if the top of the stack is a left parenthesis, push the operator onto the stack
  82.             # 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.
  83.             # 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.
  84.             # 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.
  85.             # 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
  86.                 push:
  87.  
  88.        
  89.         evaluate: # evaluate the postfix expression by iterating through each item in the postfish expression stack
  90.             # it it is a null, pop the last element off the stack and output it
  91.             # if it is an operator, pop two items off the stack and operate on them, then push the result
  92.             # if it is a digit, push it onto the stack
  93.     error:
  94.    
  95.     otherOps:
  96.         decimalToBinary:
  97.             li $v0, 4       # print string system call
  98.             la $a0, intPrompt       # prompt: "Please input an integer: "
  99.             syscall         # issue system call
  100.        
  101.             li $v0, 5       # read integer system call
  102.             syscall         # issue system call
  103.        
  104.             or $t0, $v0, $zero  # move the integer into $t0
  105.        
  106.             li $t1, 1       # initialize the mask
  107.             li $t3, 31      # initialize the counter
  108.             sll $t1, $t1, 31    # shift the mask to the MSB
  109.  
  110.             loop:               # iterates through and prints each bit
  111.                 and $t2, $t0, $t1   # and with mask to store the output bit in $t2
  112.                 srlv $t2, $t2, $t3  # shift the output bit to the LSB
  113.        
  114.                 li $v0, 1       # print integer system call
  115.                 or $a0, $t2, $zero  # move the output bit into $a0
  116.                 syscall         # issue system call
  117.        
  118.                 beq $t3, $zero, exit    # all bits have been output
  119.        
  120.                 addi $t3, $t3, -1   # decrement the counter
  121.                 srl $t1, $t1, 1     # shift the mask bit right one
  122.                 j loop          # restart the loop
  123.    
  124.     exit:
  125.         ori $v0, $zero, 10      # load system call for exit
  126.         syscall             # ends the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement