Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: ASM (NASM) | Size: 3.81 KB | Hits: 62 | 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 number stack
  16.         la $s5, 0($s0)                          # anchor for operator 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,'(',digit
  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.        
  47.         continue:
  48.         addi $s2, $s2, 1                # increment myString position
  49.         j parser
  50.        
  51.        
  52.         digit:                                          # adds digits and "(" to the number stack
  53.         sb $s3, 0($s0)
  54.         addi $s0, $s0, 1
  55.         j continue
  56.  
  57.         operator:                                       # adds operators to the operator stack
  58.         sb $s3, 0($s1)
  59.         addi $s1, $s1, 1
  60.        
  61.         j continue
  62.  
  63.         closeP:
  64.         subi $s0, $s0, 1
  65.         subi $s1, $s1, 1
  66.         beq $s4, $s0, outSide
  67.         j atoi
  68.        
  69.         atoi:
  70.         la $a0 myNewLine                                # prints newline
  71.         li $v0 4
  72.         syscall
  73.         la $a0, ($s6)                                   # prints the end result
  74.         li $v0 1
  75.         syscall
  76.         la $a0 myNewLine                                # prints newline
  77.         li $v0 4
  78.         syscall
  79.        
  80.         lb $s7, ($s0)                   # load the next thing in the array
  81.        
  82.  
  83.         beq $s7, '(', finishOper    # finishes operation if it finds an open paren...
  84.         sub $s7 $s7 48                          # subtracts the ascii value to turn it to a real digit
  85.         blt $s7, 0,     notNumber       # checks to make sure after subtracting 48 its above 0
  86.         bgt $s7, 9,     notNumber       # checks to make sure after subtracting 48 its NOT above 9
  87.         beq $s6 0, addFirst         # if end result has 0 in adds the number to the result to process next instruction
  88.        
  89.        
  90.         lb $t0, 0($s1)                  # get the next thing in the operators array
  91.                                                                 # Starts checking for operators in the array....
  92.         beq $t0,0x2B,operadd            # + add
  93.         beq $t0,0x2D,opersub            # - sub
  94.         beq $t0,0x2A,opermult           # * mult
  95.         beq $t0,0x2F,operdiv            # / div
  96.         beq $t0,0x26,operand            # & and
  97.         beq $t0,0x25,operresid          # % residuo
  98.         beq $t0,0x7C,operor                     # | or
  99.         beq $t0,0x5E,operpot            # ^ potencia
  100.         j atoi
  101.        
  102.  
  103.         addFirst:                       # if the end result has 0 add the next number to it
  104.         add $s6, $s6, $s7
  105.         subi $s0, $s0, 1
  106.         j atoi
  107.        
  108.         operadd:
  109.         add $s6, $s6, $s7
  110.         subi $s0, $s0, 1
  111.         j atoi 
  112.  
  113.         opersub:
  114.         sub $s6, $s6, $s7
  115.         subi $s0, $s0, 1
  116.         j atoi
  117.  
  118.         opermult:
  119.         mul $s6, $s6, $s7
  120.         subi $s0, $s0, 1
  121.         j atoi
  122.  
  123.         operdiv:
  124.         div $s6, $s6, $s7
  125.         subi $s0, $s0, 1
  126.         j atoi
  127.        
  128.         operand:
  129.         and $s6, $s6, $s7
  130.         subi $s0, $s0, 1
  131.         j atoi
  132.  
  133.         operresid:
  134.         rem $s6, $s6, $s7
  135.         subi $s0, $s0, 1
  136.         j atoi
  137.  
  138.         operor:
  139.         or $s6, $s6, $s7
  140.         subi $s0, $s0, 1
  141.         j atoi
  142.  
  143.         operpot:
  144.         mul $s6, $s7, $s7
  145.         subi $s0, $s0, 1
  146.         j atoi
  147.        
  148.         finishOper:
  149.         sb $s6, 0($s0)
  150.         #addi $s0, $s0, 1
  151.        
  152.         j continue
  153.  
  154.  
  155.         notNumber:                  # Number was below 0 or higher then 9 in ascii table
  156.         la $a0 notNumberError           # Loads error and quits
  157.         li $v0 4
  158.         syscall
  159.         li $v0 10
  160.         syscall
  161.  
  162.         outSide:
  163.         la $a0 numbers                  # prints the parsed numbers and parenthesis
  164.         li $v0 4
  165.         syscall
  166.         la $a0 myNewLine                        # prints newline
  167.         li $v0 4
  168.         syscall
  169.         la $a0 operators                        # prints the parsed operators
  170.         li $v0 4
  171.         syscall
  172.         la $a0 myNewLine                                # prints newline
  173.         li $v0 4
  174.         syscall
  175.         la $a0, ($s6)                                   # prints the end result
  176.         li $v0 1
  177.         syscall
  178.         li $v0 10
  179.         syscall