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
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:
%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.
;;