News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Runtime conditionals

Started by donkey, March 24, 2009, 11:15:42 AM

Previous topic - Next topic

jorgon

Having slept on this, I came up with this.

CMP? is clearly not a mnemonic since no mnemonic would ever have a question mark.
However because of the use of "CMP", to assembler programmers it has the feel of something done at run-time, rather than during assembly.
CMP? could come in two forms, as suggested by Mark, to provide the functionality demonstrated by Edgar.
CMP? single operand would work like SWITCH.  It would seem obvious that if you CMP? something with nothing, the actual comparison might be coming later.
CMP? two operands carries out the comparison immediately, like Edgar's WHERE or like IF.

So in a WndProc:-

WndProc FRAME hWnd, uMsg, wParam, lParam
CMP? uMsg                                       ;single operand version
WM_PAINT?
             ;code to achieve paint
RET
WM_CREATE?
             ;code to achieve create
RET
ENDCMP?
RET
ENDF

And the two operand version (borrowing from Edgar's example):-
ACL_COPY?
invoke GetFocus
mov ebx,eax
invoke GetClassName,eax,offset url,1024
invoke lstrcmp,offset url,offset IEWndClass
CMP? eax,0
invoke SendMessage,ebx,WM_COPY,0,0
                CMP?X
CoInvoke(pIWebBrowser2,IWebBrowser2.ExecWB,OLECMDID_COPY,OLECMDEXECOPT_DODEFAULT,NULL,NULL)
ENDCMP?
jmp >>.EXIT
ACL_ALL?
...


Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

donkey

Hi Jeremy,

Not sure about the others but I seriously find that syntax hard to read, SWITCH/CASE by itself would be preferable though it would be nice to have an IF analog as well.

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Vortex

Quote from: donkey on March 28, 2009, 08:32:09 AM
Hi Jeremy,

Not sure about the others but I seriously find that syntax hard to read, SWITCH/CASE by itself would be preferable though it would be nice to have an IF analog as well.

Edgar

I agree 100% with Edgar.

Ramon Sala

Although I don't think IF/ELSEIF/ELSE/ENDIF were a problem (they are practically used in all programming languages together with #IF/#ELSEIF/#ELSE/#ENDIF), I personally vote for SWITCH/CASE/BREAK/DEFAULT/ENDSWITCH (or even better: both). I also think the existance of BREAK would be nice because it has an advantage over using a return sentence (RET). Using BREAK would make execution to jump immediately after the SWITCH/ENDSWITCH block, which can be very useful if you need to process the code being there.

Also, I think GoAsm should have an option in the command line to activate the use of SWITCH/CASE/BREAK/DEFAULT/ENDSWITCH and IF/ELSEIF/ELSE/ENDIF (if also implemented), so that GoAsm remains working great for users which prefer to program in the traditional way and it only would be able to compile those "high level" sentences if the mentioned option was specified.

Ramon
Greetings from Catalonia

jorgon

Hi Ramon

Do you mean that in this sequence ..

RTREQUESTS FRAME Param
SWITCH Param
CASE LOVE
XOR EAX,EAX
RET
CASE CONFLICT
XOR EBX,EBX
BREAK
CASE FRUSTRATION
XOR ECX,ECX
ENDSWITCH
INJURY:
RET
ENDF


LOVE does not result in INJURY, but CONFLICT and FRUSTRATION do?
Author of the "Go" tools (GoAsm, GoLink, GoRC, GoBug)

Rainstorm

i think .if & #if can be confusing for a person just learning (from my experience).
Thing is intrinsically, .if for runtime comparisons seems the most natural (and short, compared to other conditional terms), & I guess since its usage is prevalent in many languages..people can take to it more easily

Ramon Sala

Hi Jeremy,

Yes, that's what I mean (more or less):

SWITCH <AnyVariable>
    CASE LOVE
        MOV EAX, IDS_LOVE
        BREAK

    CASE FRUSTRATION
        MOV EAX, IDS_FRUSTRATION
        BREAK

    CASE NOTHING    /* or DEFAULT */
        MOV EAX, IDS_NOTHING
ENDSWITCH

INVOKE LoadString, hInst, EAX, ADDR szBuffer, MAX_PATH
INVOKE SetWindowText, hWnd, ADDR szBuffer
...
...
XOR EAX, EAX
RET

Greetings from Catalonia

ecube

Can you please consider just adding the .if, and the .switch or select case. No ones going to be confused with .if and #if, if they are they shouldn't be programming in ASM to begin with. It's part of my job to convince customers/companies as to why ASM is a sufficient language for projects to be written in and without .if, .elseif etc which provides easy to read code(thats similar to almost all other languages) then there's no way I could convince them.


CMP? eax,0
invoke SendMessage,ebx,WM_COPY,0,0
               CMP?X
CoInvoke(pIWebBrowser2,IWebBrowser2.ExecWB,OLECMDID_COPY,OLECMDEXECOPT_DODEFAULT,NULL,NULL)
ENDCMP?


that makes my head want to explode, syntax wise that's not easy to read at all



.if eax==0
    invoke SendMessage,ebx,WM_COPY,0,0
.elseif X==0
   CoInvoke(pIWebBrowser2,IWebBrowser2.ExecWB,OLECMDID_COPY,OLECMDEXECOPT_DODEFAULT,NULL,NULL)
.endif


that looks a lot better, if you take a look at some of icezlions old larger size projects, he uses indention and .if's quite a bit, and the code looks beautiful.
Also a way you can IMO make .IF better than masm is to make the default checks signed, for masm it requires

.if sdword ptr val > -5

.endif

the signed stuff is kind of a gotcha they don't tell you about, and can mess up code fast if you don't realize its not being looked for.

wjr

The case for SWITCH/CASE/BREAK/DEFAULT/ENDSWITCH looks good, the rest somewhat iffy... for a cleaner look and consistency, I find that the IF/ELSEIF/ELSE/ENDIF without the leading period (as in MASM) would be better.

One way to perhaps limit the potential syntax confusion would be to allow the C-like preprocessor directives only in the traditional lower case (so we have #if vs IF). Although a preference of mine (yet still relatively common), I would even go a step further and suggest require having the higher level syntax in UPPER CASE. This would provide for more uniform looking code and most likely simplify syntax highlighting within editors too.

Mark Jones

Well my 2 quid is that the .if/.else syntax is very familar, therefore it seems "right" to want the same thing in GoASM.

I can respect Jeremy's idea for CMP?. It has a certain low-level feel to it, but does seem a little "obtuse" for lack of a better word. Of course, this may simply be because it is not as familiar as .if/.else. Personally I dispise syntaxical elements (.foo, #bar, etc.) When code starts to look like C++, I want to set it on fire. :toothy

Perhaps... another idea could be to create the necessary logic to perform these conditionals, but leave their names undefined as default. (Read: people who don't care about conditionals don't have to change a thing.) To use .if/.else syntax, specify a file on the command-line which contains the equates for these functions. (i.e., the file contents: #DEFINE ___Z36773473 = .if etc.) That way, if someone wants the conditionals to be called .if/.else, it's only a matter of selecting the appropriate file from the command-line. Likewise, definition files can be made for #if/#else, WHILE/ELSEW, or anything else the user wants to call them.

Can anyone think of any other ideas?
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

donkey

Well, the consensus on IF is that it would be OK and the ambiguity is something that users are willing to deal with in order to have a more standard comparative operator. I am fine with that and will drop support for the WHERE syntax. It appears that the following sums up what everybody is asking for...

note that I have never used SWITCH/CASE so I am not completely familiar with it
SWITCH var
  CASE value1
  CASE value2
  CASE nothing // not sure what this does ???
  DEFAULT (or CASE DEFAULT)
ENDSWITCH


IF eax<>0
ELSEIF eax=1
ELSE
ENDIF


The leading period would hopelessly confuse some of my older code, since I tended to use .IF/.ELSE/.ENDIF as anonymous labels. Though it is not a great task to translate it, it would be easier for me if the leading period was left out completely. Forcing upper and lower case is something I don't like much but again is not a major issue with me, some minor editing in the headers project is the most I will have to do (I use case in some headers to distinguish DIID and IID so I can search faster - mshtml.h is a good example).

Edgar
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

Ramon Sala

QuoteCASE nothing // not sure what this does ???

Yes Edgard, you are right. In fact, I meant "any other case", so DEFAULT is the right word.

On the other hand, if IF/ELSEIF/ELSE/ENDIF is going to be implemented, I'm not sure whether the leading period should be added or not. A lot of people come from Masm programming and that syntax is very familiar.
Greetings from Catalonia

GregL

I think IF (without the leading period) would be fine.  I like the SWITCH/CASE/BREAK/DEFAULT/ENDSWITCH conditionals too.




mitchi

IF without the leading period is fine ! (Even better)  :dance:

ecube

Quote from: Mark Jones on March 28, 2009, 08:58:21 PM
another idea could be to create the necessary logic to perform these conditionals, but leave their names undefined as default. (Read: people who don't care about conditionals don't have to change a thing.) To use .if/.else syntax, specify a file on the command-line which contains the equates for these functions. (i.e., the file contents: #DEFINE ___Z36773473 = .if etc.) That way, if someone wants the conditionals to be called .if/.else, it's only a matter of selecting the appropriate file from the command-line. Likewise, definition files can be made for #if/#else, WHILE/ELSEW, or anything else the user wants to call them.

Jeremy,
Can you just do this please, that way everyone wins, and some don't lose. It goes without precident you'll go with what donkey wants over others because he has more backing and is a big part of this project. But using this let the user specify the directive idea above is perfect.