Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: C | Size: 3.60 KB | Hits: 68 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. .data
  2.         enterString: .asciiz "$> "
  3.         myString: .asciiz "(^ 2 (* 5 2))"
  4.         notNumberError: .asciiz "Not a valid number!"
  5.         myNewLine: .asciiz "\n"
  6.     stacksize: .byte 4
  7.     .align 2
  8.         inputString: .space 200
  9.         numbers: .space 200
  10.         operators: .space 200
  11.  
  12. .text
  13.        
  14.         main:
  15.         la $a0 enterString
  16.         li $v0 4
  17.         syscall
  18.  
  19.         la $a0 inputString
  20.         li $a1, 200
  21.         li $v0, 8
  22.         syscall
  23.        
  24.         la $a0 enterString
  25.         li $v0, 4
  26.         syscall
  27.  
  28.         la $a0, inputString
  29.         li $v0, 4
  30.         syscall
  31.                
  32.         move $s2 $a0
  33.  
  34.         la $s0 numbers             # current postion number stack
  35.         la $s1 operators                   # current position operator stack
  36.  
  37.         la $s4, 0($s1)                          # anchor for operator stack
  38.         la $s5, 0($s0)                          # anchor for number stack
  39.     lb $s6, stacksize
  40.  
  41.         j parser
  42.        
  43.         parser:                    # Adds the string items to 2 separate stacks for later processing..
  44.         lb $s3, ($s2)
  45.         beq $s3,'\0',outSide
  46.         blt $s3,'0', parser2:
  47.     bgt $s3,'9', parser2:
  48.  
  49.     parser2:
  50.         beq $s3,'(',insertPar
  51.         beq $s3,')',closeP
  52.         beq $s3,0x2B,operator           # + add
  53.         beq $s3,0x2D,operator           # - sub
  54.         beq $s3,0x2A,operator           # * mult
  55.         beq $s3,0x2F,operator           # / div
  56.         beq $s3,0x26,operator           # & and
  57.         beq $s3,0x25,operator           # % residuo
  58.         beq $s3,0x7C,operator           # | or
  59.         beq $s3,0x5E,operator           # ^ potencia
  60.  
  61.        
  62.         continue:
  63.         addi $s2, $s2, 1                # increment myString position
  64.         j parser
  65.        
  66.        
  67.         digit:
  68.         sub $s3 $s3 48                                          # adds digits to the number stack
  69.         add $s0, $s0, $s6
  70.         sw $s3, 0($s0)
  71.        
  72.         j continue
  73.        
  74.         insertPar:
  75.         li $s3 0xffffffff                                               # if  "(" add it as FF
  76.         add $s0, $s0, $s6
  77.         sw $s3, 0($s0)
  78.        
  79.         j continue
  80.  
  81.         operator:                                       # adds operators to the operator stack
  82.         #sub $s3 $s3 48
  83.         add $s1, $s1, $s6
  84.         sw $s3, 0($s1)
  85.        
  86.        
  87.         j continue
  88.  
  89.         closeP:
  90.        
  91.         atoi:
  92.        
  93.     preprocess:
  94.         lw $t0, 0($s0)                  # get the next thing in the number array
  95.         sub $s0, $s0, $s6
  96.  
  97.         beq $t0, 0xffffffff, finishOper    # finishes operation if it finds an open paren...
  98.         ##sub $s7 $s7 48                                # subtracts the ascii value to turn it to a real digit
  99.         blt $t0, 0,     notNumber       # checks to make sure after subtracting 48 its above 0
  100.         #bgt $s7, 15, notNumber         # checks to make sure after subtracting 48 its NOT above 9
  101.  
  102.         lw $t1, 0($s0)                  # get the next thing in the number array
  103.         sub $s0, $s0, $s6
  104.  
  105.         lw $t2, 0($s1)                  # get the next thing in the operators array
  106.        
  107.  
  108.                                                                 # Starts checking for operators in the array....
  109.         beq $t2,0x2B,operadd            # + add
  110.         beq $t2,0x2D,opersub            # - sub
  111.         beq $t2,0x2A,opermult           # * mult
  112.         beq $t2,0x2F,operdiv            # / div
  113.         beq $t2,0x26,operand            # & and
  114.         beq $t2,0x25,operresid          # % residuo
  115.         beq $t2,0x7C,operor                     # | or
  116.         beq $t2,0x5E,operpot            # ^ potencia
  117.         j atoi
  118.  
  119.     postprocess:
  120.         lw $t4, 0($s0)
  121.         beq $t4, 0xffffffff, finishOper
  122.         add $s0, $s0, $s6
  123.         sw $s6, 0($s0)
  124.         j preprocess   
  125.  
  126.         finishOper:
  127.         sw $t3, 0($s0)
  128.         sub $s1, $s1, $s6
  129.         j continue     
  130.  
  131.         operadd:
  132.         add $t3, $t1, $t0
  133.         j postprocess
  134.  
  135.         opersub:
  136.         sub $t3, $t1, $t0
  137.         j postprocess
  138.        
  139.  
  140.         opermult:
  141.         mul $t3, $t1, $t0
  142.         j postprocess
  143.  
  144.         operdiv:
  145.         div $t3, $t1, $t0
  146.         j postprocess
  147.        
  148.         operand:
  149.         and $t3, $t1, $t0
  150.         j postprocess
  151.  
  152.         operresid:
  153.         rem $t3, $t1, $t0
  154.         j postprocess
  155.  
  156.         operor:
  157.         or $t3, $t1, $t0
  158.         j postprocess
  159.  
  160.         operpot:
  161.         li $t3, 1
  162.     loop:
  163.         beqz $t0, postprocess
  164.         mul $t3, $t3, $t1
  165.         sub $t0, $t0 1 
  166.         j loop
  167.        
  168.  
  169.         notNumber:                  # Number was below 0 or higher then 9 in ascii table
  170.         la $a0 notNumberError           # Loads error and quits
  171.         li $v0 4
  172.         syscall
  173.         li $v0 10
  174.         syscall
  175.  
  176.         outSide:
  177.        
  178.         la $a0 enterString                      # prints the parsed operators
  179.         li $v0 4
  180.         syscall
  181.         lw $a0, ($s0)                                   # prints the end result
  182.         li $v0 1
  183.         syscall
  184.         li $v0 10
  185.         syscall