News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

poccessing math from a command line

Started by ninjarider, February 13, 2006, 04:03:26 PM

Previous topic - Next topic

ninjarider

been working on an idea and i can read in the numbers and operands off a command line. it can proccess simple math left to right cant do decimal place because im using the cpu registers instead of fpu registers.

i was wondering what would be the easiest and cleaniest way to be able to process the order of operaions?

the way it currently works

a:\ 836+10*2/5
my program first reads the first character and sees if its a space. then reads the line looking for a non number adding up the values it finds as it moves accross the string. once it finds a non number it then goes threw a if than group trying to figure out wha math operand it is. it stores all it finds as word bytes to ram. the math section then reads it and processes the mat left to right.

Ratch

ninjarider,
     I think that a lot of people have gone before you on this idea.  See http://www.derive-europe.com/downloads.asp . Ratch

ninjarider

its a cool program but from what i see i need an existing os to use it and im trying to make my own os.

Tedd

Is there any reason to not do the processing while scanning?
So..

result = 0; op = ' '
scanning: number(8), number(3), number(6), not-number(+) -- toVal("836"), push(836), op='+'
scanning: number(1), number(0), not-number(*) -- toVal("10"), push(10), (op != ' '): doOp('+',pop(=10),pop(=836)) ==> result
etc..
{sorry about the notation -- hope you can follow it}

BUT it's normal to have operator-precedence (1+2*3 = 7) and not simple left-to-right (1+2*3 = 6) -- but that will need slightly different processing (not so simple) and I would probably build a tree for it.
No snowflake in an avalanche feels responsible.

ninjarider

tedd,

apprectiat the idea not sure if i understand it currectly. but in the event noone is clear on what my code is doing and i will try to post it tomarrow.

a:\ 836+10*2/5

program scans the byte string

register di and si are set to a static variable in memory
operands
* = 1
+ = 2
- = 3
/ = 4

finds 836
stores w byte 836
finds +
stores w byte 2
finds 10
stores w byte 10
finds *
stores w byte 1
finds 2
stores w byte 2
finds /
stores w byte 4
finds 5
stores w byte 5
stores w byte 0 or end of line

loads the first math byte

loop
check for end of line
load the math operand
load the second math byte
check operand code - possible priority bit come to think of it
perfom math operation
jump to loop

Mark Jones

Quote from: ninjarider on February 13, 2006, 08:00:30 PM
check operand code - possible priority bit come to think of it

Yes, for the results to come out right, items in parentheses must be done first, followed by * and / , then + and -

Floats are necessary when using division, are you sure you can't support them?

Perhaps identify the operand priority in the source and replace them one-at-a-time. i.e.,

-123+65*2+(1/4):
(1/4) is the first calculation due to highest priority. Result is +0.25

-123+65*2+0.25
65*2 is second highest priority. Result is +130.

-123+130+0.25
-123 + 130 = 7

7+0.25
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

zooba

What a coincidence :bg

I'm just about to post my expression evaluation code  :toothy. It correctly handles integer arithmetic including basic arithmetic operations, round-nearest (/) and truncate (\) division and logical operators (ie. &, |, ^, &&, ||, ==, >=, etc.) as well as bracketed expressions.

You're welcome to use the function provided (it's in a lib file but source is there too) in your own code :U

Expression Evaluator (in the Workshop)