flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > New syntax for numerical expressions: with arguments

Author
Thread Post new topic Reply to topic
daluca



Joined: 05 Nov 2005
Posts: 86
daluca
Hello:

Let's say we want to load a constant RGB value color in to some register,
eax for instance, but don't want to code:
Code:

xor eax,eax
mov ah,100   ;blue
mov al,180   ;green
shl eax,8
mov al,190   ;red

    

Instead we want Fasm to do the calculations and load the value directly
like:
Code:

         ;blue        green      red
mov eax,(100 shl 16)+(180 shl 8)+190

    

Of course we can do the calculations ourselves and then code:
Code:

mov eax,64B4BEh
 ;or in decimal:
mov eax,6599870

    

But what if we want to performe such mov operation with another three
values?... well, macro:
Code:

macro movRGB destination,red,green,blue 
{
mov destination,(blue shl 16)+(green shl 8)+red
}

movRGB ebx,255,0,255
movRBG edx,128,64,32
movRGB [Color1],192,192,192

    

But what about pushing the color,or other operation apart from mov?
we have to extend the macro:
Code:

macro RGBoperation operation,destination,red,green,blue
{
operation destination,(blue shl 16)+(green shl 8)+red
}

RGBoperation xor,eax,192,192,192
RGBoperation and,[Color2],255,0,255
RGBoperation or,ebx,128,64,32

    

And about defining these values in data?... another macro?
or maybe a structure?
I don't like this approach of adding more mnemonics,because what we
are doing here is just handling immediate values.
so I would like to have a way to define values based on arguments
I know this is like inline macros, and in an old topic I found:
"inlining the macros is something against the Fasm's concept"
but please, let me continue...

I'm going to jump directly to my proposal,and then give some advantages
and examples of use:

1.- A new Keyword: I will use FORMULA
2.- A name for the formula freely chosen by the coder
3.- A list of arguments that the formula will take
4.- The actual formula
Code:

FORMULA RGB red,green,blue = (blue shl 16)+( green shl 8 ) + red

    

Since this definition is just a constant value specified by any
numerical expression,it should take only one line.
Obviously the arguments should be immediate values too.

The invocation of the formula would be something simple like:
Code:

RGB(192,192,192)

    

Or maybe: (RGB,192,192,192) to indicate that this expression should be
readed as one single immediate value.(I will use the two ways)
And wherever this expresion appears
in the source will be replaced with: (192 shl 16)+(192 shl 8 ) + 192

Why should be implemented:

- More readability.

- Less typing.

- No new mnemonics,we can use the standar ones.

- They are just numbers so we can use them as parameters to macros:

Code:

mov eax,RGB(190,181,100)

xor eax,RGB(192,192,192)

cmp eax,(RGB,255,0,255)

color1 dd  RGB(128,64,32)

ColorArray dd  (RGB,255,0,255)
           dd  (RGB,190,181,100)
           dd  (RGB,128,64,32)
           dd  (RGB,80h,80h,80h)

invoke SetTextColor,[hdc],(RGB,128,32,32)

    


Some usages:

For seting the highword of some register or memory to
a specific decimal value:
(The low word will be zero of course)
Code:

        ;HW = HighWord
FORMULA HW value = value shl 16

mov eax,HW(1523)

mov [VAR_1],(HW,4096)

    


To set or compare the cursor position with the bios interrupt
10h, or to define constant positions in memory:
Code:

FORMULA Pos column,row = (row shl 8) + column

;Set cursor:
mov ah,2
mov bh,0  ;page number
mov dx,Pos(5,15)
int 10h

;Get cursor
mov ah,3
mov bh,0  ;page number
int 10h
cmp dx,(Pos,24,79)

;or:
cmp dx,[EndPos]
;...code
;...code

;...data
EndPos dw (Pos,24,79)

    


The formula definition can be altered to define the
positions starting from 1 instead of 0:
Code:

FORMULA Pos column,row = ((row-1) shl 8 )) + (column-1)

    


So Pos(80,25) will be actually Pos(79,24) and equal to 184Fh.


For handling dates and times as packed words in the format:

Date: |YYYYYYYM|MMMDDDDD|
Time: |HHHHHmmm|mmmsssss|
Code:

FORMULA Date,day,month,year =  ((year -1980) shl 9)+(month shl 5)+day

FORMULA Time,hour,minute,second = (hour shl 11)+(minute shl 5) +(second/2)

mov bx,[filehandle]
mov ah,57h
mov al,0    ;0=get time and date of file
cmp dx,Date(27,8,2006)
je some_code
cmp cx,Time(17,30,0)
jne some_other_code

;...data
date1 dw (Date,1,1,2006)
date2 dw (Date,31,12,2006)

InitTime dw Time(0,0,0)
EndTime  dw Time(12,59,59)

    


Why should not be implemented:

-It would be dificult to implement.

well I don't know how much dificulty would it take
but from my naive point of view it would be just text
replecement by the preprocesor,and the assembler would
do the calculations as already does.

-The same thing can be achieved with macros.

maybe, but macros can't be use as parameters.

-This is just inline macros and that goes "against the Fasm's concept".

we can see this as inline macros although we can also see it
as a new way to represent constant values,a way that would be more
flexible because the use of arguments.

-The few occasions that we need to handle constant values in
this way,doesn't justify the implementation of this new "feature".

that maybe true but; how often do you use,in your sorces,directives
like load,store or virtual? I think this directives have usage in
few specific situations as well.


Well this is just an idea that i put to your consideration.

Thank you for your time.
Post 28 Aug 2006, 04:23
View user's profile Send private message Reply with quote
IceStudent



Joined: 19 Dec 2003
Posts: 60
Location: Ukraine
IceStudent
Interesting. It seems as macro function in the masm.
Post 28 Aug 2006, 09:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
Perhaps the syntax can look better like this:
Code:
FORMULA Date(day,month,year) = ...    
Post 28 Aug 2006, 09:37
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
already discussed - search for "inline macros" or something like that. It's design problem of FASM.

(But i still believe it could be added... it there's a will)
Post 28 Aug 2006, 09:57
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
@ daluca: Some of the things you mentioned can be accomplished by rewriting the actual function your using e.g.

Code:
macro mov op1,op2,v1,v2,v3
 {
        if op2 eq RGBVal
                mov op1,((v3 shl 16) + (v2 shl 8) + v1)
        else
                mov op1,op2
        end if
 }

struc RGB r,g,b
 {
        . dd ((b shl 16) + (g shl 8) + r)
 }

use32
mov eax,RGBVal,128,128,128
mov eax,[Colour]

Colour   RGB 128,128,128    
This avoids creating "new" macros and inline macros, but it does mean you have to add RGB,Pos,... options for all the operations you want to use (mov, xor, cmp, ...) and define strucs with different names Sad but once you've written them you'll never have to do it again Wink
Post 28 Aug 2006, 16:36
View user's profile Send private message Reply with quote
chris



Joined: 05 Jan 2006
Posts: 62
Location: China->US->China->?
chris
I guess this could be done by extending the 'fix' directive, so that the replacement could take arguments, like

Date(day,month,year) fix ((year -1980) shl 9)+(month shl 5)+day

but since 'fix' is introduced to adjust some language syntax, this might have some side effects.
Post 29 Aug 2006, 03:40
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
But it which project would this be used? I think that all you described is needed only for debugging, but in real life you only use these kinds of strctures when inititalizing something. Well, actually, everything that makes use of macros does some kind of initialization Razz . I still think that writing:
Code:
mov eax,128 shl 16+128 shl 8+128
    

instead of:
Code:
mov eax,RGB(128,128,128)
    

isn't much of a trouble and is clearer. with RGB() you don't know which value goes to which byte and weather red is actually in the place of red :S
Post 29 Aug 2006, 14:48
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7725
Location: Kraków, Poland
Tomasz Grysztar
Such feature was requested and considered a few times in fasm's history, but I never decided to go into such direction, as I had a "slightly different vision" of macro processor that I made into fasm. See also http://board.flatassembler.net/topic.php?t=4795
Post 30 Aug 2006, 11:53
View user's profile Send private message Visit poster's website Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend
My solution:
Code:
macro overload1 instr {
 macro instr op1 \{
  match =RGB red green blue, op1 \\{
   instr (red shl 16) or (green shl 8) or blue \\} \} }

macro overload2 instr {
 macro instr op1, op2 \{
  match =RGB red green blue, op2 \\{
   instr op1, (red shl 16) or (green shl 8) or blue \\} \} }

irps instruction, push {
 overload1 instruction }

irps instruction, mov xor sub add {
 overload2 instruction }


        mov     eax, RGB 192 192 192
        xor     ecx, RGB 255 255 0
        sub     edx, RGB 160 170 23

        push    dword RGB 1 1 1    
Post 10 Sep 2006, 14:27
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.