Share Pastebin
Guest
Public paste!

Untitled

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