News:

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

Simple formula parser

Started by anorak, March 18, 2006, 08:35:56 AM

Previous topic - Next topic

anorak

I've tryed to make a simle formula parser in asm but i fount that it's quite hard. on C++ it's much easier. Anyone has any ideas?

hutch--

The brain is much the same, its just that in assembler you can tailor the higher level stuff yourself to more accurately do what you need. Choices seem to be either an iterative or recursive parser that hits the same data over and over again until all of the formula is broken into 2 part values which are then calculated.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

anorak

the main problem is that i can't make it!
i've tryed four times with different ways - allways stucking on some stupid things...
even tryed parse_string function that cames with masm32 - the problem with that fuction is that it don't understands brackets '(' and ')'

zooba

I have an expression evaluator available here (forum post) which includes full source.

The general theory is as follows:

1. Read first value and push onto stack
2. Read operator
2a. If operator is + or - (or some others), evaluate everything until the most recent open bracket (without popping the bracket) and then push the new operator
2b. If operator is ) (close bracket) evaluate everything until the most recent open bracket, pop the open bracket and push the new operator
2c. Else push the operator
3. Go to 1

Evaluation works like this:
1. Pop the operator.
2. Perform the operation on either the topmost value or two topmost values
3. Leave the result as the topmost value.

I have found this method works quite efficiently, though I haven't performed any benchmarking of my code yet.

Good luck,

Zooba :U

anorak

yeah thanks man!  :P
exactly what i wanted!
im now testing this stuff - it's great
just have one question
im making it for an address calculator
for examle:
[400000]+A*[453AD0]+56A
square brackets means that you must read te mem

your parser is great but maybe you can help me to do that
[400000]+A*B[453AD0]+56A

i've already made that the address is reading, but it would be great if i can add the read size (B before brackets means read  BYTE)

hutch--

This is one approach to parsing a formula.


    1.  ( (1+(2*4)) * ((22+46)-13) )

    2.  ( (1+8) * ((22+46)-13) )

    3.  ( 9 * ((22+46)-13) )

    4.  ( 9 * (68-13) )

    5.  9 * 55

    6.  495


Each pass removes an opening and closing bracket pair, calculates the result and replaces the original pair with a single value. Keep iterating or recursing the same formula until you have a single value.


Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

anorak

yeah hutch its standart algo :P thank a lot for ideas and support - on many forums noone helped to me

hutch--

An address parser is a slightly different animal if it is the complex addressing modes for opcodes that you need to parse in normal Intel format. You need to do a single pass parse of the components between the single pair of square brackets and work from an order of argument relationship and handle the variations.

With the 4 possible components, base, index, multiplier and displacement you have to recognise the components if you want to support some of the out of order options that masm and similar support.

[displacement+base_register+index_register*multiplier]

You tend to get the index_register and multiplier as a pair then determine if there is a displacement and a seperate base address register. You must also handle the opcode that has a displacement as the base address.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

zooba

My expression evaluator allows you to pass the address of a function which interprets the symbol. Check the *cough*documentation*cough* :wink

Basically, when a symbol is found the string is passed to the function which returns the value. There's a little bit of forward-reference type stuff for values that aren't known yet (I wrote this while writing an assembler, so this was useful).

For example, if you pass "[400000]+A*B[453AD0]+56A" to the main function, your callback would receive the following strings (in this order):

[400000]
A
B[453AD0]
56A


Cheers,

Zooba :U

anorak