PDA

View Full Version : Constant Expressions



Charles Pegge
04-05-2009, 14:39
Constant expressions can now be evaluated at precompile time

For Example:

Macro withour evaluation:

def aa 1+2+3*4

aa becomes a macro containing 1+2+3*4

but

#def aa 1+2+3*4

aa becomes a macro containing 15



For Assembler only:

#def


For o2_basic / o2h:

#def
#define
%
$



You can also see from the example below that displacements used for memory addressing are also
automatically evaluated to a single number

Oxygen Update : http://community.thinbasic.com/index.php?topic=2517



' CONSTANTS 1
' contant expressions used in assembly code

uses "oxygen"

dim s as string
dim aa(10) as long

s="

#def vv 1+10
mov eax,vv

pushad

mov ecx,4
mov [#aa],12
mov [#aa+4],13
mov [ecx+#aa+2+2],14

mov edx,2
mov [ecx+edx*2+#aa+4],15
mov [ecx+edx*4+#aa+4],16

popad

ret
"

o2_asmo s
if len(o2_error) then msgbox 0,o2_error : stop
msgbox 0,o2_view s
msgbox 0,o2_exec+$cr+aa(1)+$cr+aa(2)+$cr+aa(3)+$cr+aa(4)+$cr+aa(5)



In Assembly code, Immediate values may also be an expression of numbers (though this is currently limited to addition and subtraction.)




' CONSTANTS 2
' 'addition to immediate value

uses "oxygen","file"
dim src as string
dim vv as long
src="
'o2h
;-----------
#def a 1+2*3
mov eax,a+3
;-----------

ret
"

msgbox 0,"Test: "+o2_error+o2_view (src)

o2_asmo src
if len(o2_error) then
msgbox 0,"Test: "+o2_error+o2_view (src)
file_save("t.txt",o2_view (src))
stop
end if
vv=o2_exec
msgbox 0,"0x"+hex$(vv)

Petr Schreiber
04-05-2009, 19:32
This is definitely good feature,

I use notation of "formula" quite often, it can help to "document" the code for pointer offsets and similar cases.


Thanks,
Petr