NASM - The Netwide Assembler
NASM Forum => Example Code => Topic started by: Mathi 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
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.
;;
-
Wow, this stuff really brings back memories. lol
Great work, Mathi! 8)
-
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.
-
Thanks Bryant, Rob.
One thing i missed in nasm was %strstr :)
It would be cool if 64-bit was also supported.
Yeah that could be done. :)
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 :)
-
Incorporated those changes .
PFA the updated include file.