Author Topic: Arithmetic Expression Evaluator Macro  (Read 3970 times)

Offline Mathi

  • Jr. Member
  • *
  • Posts: 79
  • Country: in
    • Win32NASM
Arithmetic Expression Evaluator Macro
« on: January 09, 2013, 10:08:47 AM »
Hi All,

 I have written a macro(N_EVAL) to generate code to evaluate an arithmetic expression passed as string.

For eg : 
A=B+C*D
can be written as
N_EVAL A, 'B+C*D'

Code will be generated to - evaluate 'B+C*D' and store it in A (or in eax)
-----
n_eval ebx, '2+ecx*edx'
will generate

Code: [Select]
push dword 2
 push dword ecx
 push dword edx
 pop ebx
 pop eax
 mul ebx
 push eax
 pop ebx
 pop eax
 add eax,ebx
 push eax
 pop eax
 mov ebx,eax

PFA the include file n_eval.inc
With this macro we can code something like

N_EVAL EAX, 'EBX + EDX*ECX'
or even,
N_EVAL [final], 'SAMPLEVALUE* (([index]*(ecx*((ebx) / 2)*(1<<2)  +  [bigvariablename])-2*[sv]))'

Sample program:

Code: [Select]
%include 'n_eval.inc'

extern ExitProcess

%define SAMPLEVALUE 50


global start
section .data

IntegerArray : times 100 dd 0
index dd 5
bigvariablename dd 1000
sv dd 100
final dd 0

section .text
start:

n_eval 'IntegerArray+[index]*4'  ;;Address of fifth element  will be stored in eax

mov ecx, 1
mov ebx, 2
n_eval [final], 'SAMPLEVALUE* (([index]*(ecx*((ebx) / 2)*(1<<2)  +  [bigvariablename])-2*[sv]))'    ;;EAX = 241000 and stored in to [final]

push dword 0
call ExitProcess

;; BIGRULES:
;;    : NO UNARY OPERATOR SUPPORT
;;    : NO FLOAT SUPPORT
;;    : NO ASSIGNMENT OPERATOR SUPPORT IN ANY FORM ( = , +=, -=, *=, /=, %= etc)
;;    : NOT SPEED OR SIZE OPTIMIZED.
;;    : LIMITED ERROR CHECKING
;;    : NO EXCEPTION HANDLING
;;    : MACRO USES EAX, EBX, ECX, EDX  - EDIT THIS MACRO to save or save it before calling.
;;BINARY OPERATORS SUPPORTED
;;    **               --->    pow (A,B)
;;    *   /   %     --->    Multiplication, division, and remainder 
;;    +   -       --->    Addition and subtraction 
;;    <<   >>       --->    Bitwise left shift and right shift 
;;    <   <=       --->    For relational operators < and <= respectively 
;;    >   >=       --->    For relational operators > and >= respectively 
;;    ==   !=      --->    For relational == and != respectively 
;;    &        --->    Bitwise AND 
;;    ^          --->    Bitwise XOR (exclusive or) 
;;    |          --->    Bitwise OR (inclusive or) 
;;    &&         --->    Logical AND 
;;    ||                   --->    Logical OR 


;; HOW THIS WORKS : http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm
;; STEP 1:  TOKENIZATION
;; STEP 2:  CONVERT THE EXPRESSION FROM INFIX TO POSTFIX
;; STEP 3:  GENERATE THE CODE TO EVALUATE THE POSTFIX EXPRESSION USING STACK.
;;


« Last Edit: January 09, 2013, 11:36:12 AM by Mathi »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Arithmetic Expression Evaluator Macro
« Reply #1 on: January 09, 2013, 06:27:19 PM »
Wow, this stuff really brings back memories. lol

Great work, Mathi!   8)

About Bryant Keller
bkeller@about.me

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 430
  • Country: us
Re: Arithmetic Expression Evaluator Macro
« Reply #2 on: January 09, 2013, 08:36:36 PM »
Indeed, fantastic work.  8)

After a cursory look at the macro - two observations:
1. It would be cool if 64-bit was also supported.   ;D
2. Is it possible to eliminate the internal ebx usage?  Otherwise users will have to remember to frame the n_eval macro with push/pop ebx when writing asm functions callable from C that use the macro in order to ensure that the function doesn't violate the saved register requirements of the calling convention.  Or it can simply be an additional documented requirement.  Obviously if ebx is used within the expression string itself or is the target of the result then it falls upon the user to already understand this. 

Offline Mathi

  • Jr. Member
  • *
  • Posts: 79
  • Country: in
    • Win32NASM
Re: Arithmetic Expression Evaluator Macro
« Reply #3 on: January 10, 2013, 03:50:50 AM »
Thanks Bryant, Rob.

One thing i missed in nasm was %strstr  :)

Quote
It would be cool if 64-bit was also supported.

Yeah that could be done. :)

Quote
Is it possible to eliminate the internal ebx usage?
Hmm. infact ecx, edx were also used in the macro . I guess we are not losing anything saving those 3 registers.

Thanks for the suggestions :)

Offline Mathi

  • Jr. Member
  • *
  • Posts: 79
  • Country: in
    • Win32NASM
Re: Arithmetic Expression Evaluator Macro
« Reply #4 on: January 10, 2013, 09:04:27 AM »
Incorporated those changes .
PFA the updated include file.
« Last Edit: January 10, 2013, 09:36:10 AM by Mathi »