Untitled
By: a guest | Mar 22nd, 2010 | Syntax:
C | Size: 3.60 KB | Hits: 68 | Expires: Never
.data
enterString: .asciiz "$> "
myString: .asciiz "(^ 2 (* 5 2))"
notNumberError: .asciiz "Not a valid number!"
myNewLine: .asciiz "\n"
stacksize: .byte 4
.align 2
inputString: .space 200
numbers: .space 200
operators: .space 200
.text
main:
la $a0 enterString
li $v0 4
syscall
la $a0 inputString
li $a1, 200
li $v0, 8
syscall
la $a0 enterString
li $v0, 4
syscall
la $a0, inputString
li $v0, 4
syscall
move $s2 $a0
la $s0 numbers # current postion number stack
la $s1 operators # current position operator stack
la $s4, 0($s1) # anchor for operator stack
la $s5, 0($s0) # anchor for number stack
lb $s6, stacksize
j parser
parser: # Adds the string items to 2 separate stacks for later processing..
lb $s3, ($s2)
beq $s3,'\0',outSide
blt $s3,'0', parser2:
bgt $s3,'9', parser2:
parser2:
beq $s3,'(',insertPar
beq $s3,')',closeP
beq $s3,0x2B,operator # + add
beq $s3,0x2D,operator # - sub
beq $s3,0x2A,operator # * mult
beq $s3,0x2F,operator # / div
beq $s3,0x26,operator # & and
beq $s3,0x25,operator # % residuo
beq $s3,0x7C,operator # | or
beq $s3,0x5E,operator # ^ potencia
continue:
addi $s2, $s2, 1 # increment myString position
j parser
digit:
sub $s3 $s3 48 # adds digits to the number stack
add $s0, $s0, $s6
sw $s3, 0($s0)
j continue
insertPar:
li $s3 0xffffffff # if "(" add it as FF
add $s0, $s0, $s6
sw $s3, 0($s0)
j continue
operator: # adds operators to the operator stack
#sub $s3 $s3 48
add $s1, $s1, $s6
sw $s3, 0($s1)
j continue
closeP:
atoi:
preprocess:
lw $t0, 0($s0) # get the next thing in the number array
sub $s0, $s0, $s6
beq $t0, 0xffffffff, finishOper # finishes operation if it finds an open paren...
##sub $s7 $s7 48 # subtracts the ascii value to turn it to a real digit
blt $t0, 0, notNumber # checks to make sure after subtracting 48 its above 0
#bgt $s7, 15, notNumber # checks to make sure after subtracting 48 its NOT above 9
lw $t1, 0($s0) # get the next thing in the number array
sub $s0, $s0, $s6
lw $t2, 0($s1) # get the next thing in the operators array
# Starts checking for operators in the array....
beq $t2,0x2B,operadd # + add
beq $t2,0x2D,opersub # - sub
beq $t2,0x2A,opermult # * mult
beq $t2,0x2F,operdiv # / div
beq $t2,0x26,operand # & and
beq $t2,0x25,operresid # % residuo
beq $t2,0x7C,operor # | or
beq $t2,0x5E,operpot # ^ potencia
j atoi
postprocess:
lw $t4, 0($s0)
beq $t4, 0xffffffff, finishOper
add $s0, $s0, $s6
sw $s6, 0($s0)
j preprocess
finishOper:
sw $t3, 0($s0)
sub $s1, $s1, $s6
j continue
operadd:
add $t3, $t1, $t0
j postprocess
opersub:
sub $t3, $t1, $t0
j postprocess
opermult:
mul $t3, $t1, $t0
j postprocess
operdiv:
div $t3, $t1, $t0
j postprocess
operand:
and $t3, $t1, $t0
j postprocess
operresid:
rem $t3, $t1, $t0
j postprocess
operor:
or $t3, $t1, $t0
j postprocess
operpot:
li $t3, 1
loop:
beqz $t0, postprocess
mul $t3, $t3, $t1
sub $t0, $t0 1
j loop
notNumber: # Number was below 0 or higher then 9 in ascii table
la $a0 notNumberError # Loads error and quits
li $v0 4
syscall
li $v0 10
syscall
outSide:
la $a0 enterString # prints the parsed operators
li $v0 4
syscall
lw $a0, ($s0) # prints the end result
li $v0 1
syscall
li $v0 10
syscall