Share Pastebin
Guest
Public paste!

Untitled

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