News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

If condition Then action one-liner

Started by jj2007, October 06, 2013, 04:20:08 AM

Previous topic - Next topic

jj2007

Some Basic dialects know this construct: If MyVar==1 Then MyVar++

include \masm32\include\masm32rt.inc

_If MACRO arg:VARARG
LOCAL is
  is INSTR <arg>, <Then>
  ife is
   .err @CatStr(<*** no Then in line >, %@Line, < ***>)
  else
   % echo condition=[@SubStr(<arg>, 1, is-2)]
   .if @SubStr(<arg>, 1, is-1)
      @SubStr(<arg>, is+5)
   .endif
  endif
ENDM
.code
start:
   or ebx, -1
   mov edi, 123
   _If !!!!(ebx==+1 && edi==999) Then print "Condition A is FALSE", 13, 10
   _If ebx==-1 && edi==123 Then print "Condition B is TRUE", 13, 10

   inkey "ok?"
   exit
end start


Drawbacks:
- doesn't look like assembler :biggrin:
- negation requires FOUR exclamation marks  ::)
- not possible with JWasm :(

Gunther

Jochen,

Quote from: jj2007 on October 06, 2013, 04:20:08 AM
Drawbacks:
- doesn't look like assembler :biggrin:
- negation requires FOUR exclamation marks  ::)
- not possible with JWasm :(

But the idea is nice.

Gunther
You have to know the facts before you can distort them.

hutch--

Looks fine for basic, I wonder if you can use a text word for the negation ?

I confess I prefer the block form and use it in basic but there are situations where the one line form is useful.

jj2007

Quote from: hutch-- on October 06, 2013, 10:52:39 AM
Looks fine for basic, I wonder if you can use a text word for the negation ?
No luck with ex1 equ ! or similar. The beast really wants 4 exclamation marks :(

QuoteI confess I prefer the block form and use it in basic but there are situations where the one line form is useful.
There are coders who prefer to see as much as possible (many lines on screen, dense code, complex macros...), others prefer to see the detail, and even add empty lines containing only comments.
Likewise, some people prefer the monolithic 10,000 lines source, while others split it into one-hundred include files. What is better? What is more productive?
It is a bit like the known difference between those who need a complete diagram to understand a problem, while others need a page of text instead. Interesting question in itself, it would merit a thread in the Soap Box ;-)

dedndave

some of those personal preferences stem from how the programmer got started
when i started out, DEBUG was one of my best tools - lol
i was used to seeing each instruction with no macros or symbols

jimg

Doesn't look at all like basic, looks like c crap

japheth

Quote from: jimg on October 07, 2013, 01:52:55 AM
Doesn't look at all like basic, looks like c crap

:icon_mrgreen:

No, C is way, way, way more elegant:


i == 1 ? printf("i is 1\n") : printf("i is not 1\n");

jj2007

Quote from: jimg on October 07, 2013, 01:52:55 AM
Doesn't look at all like basic, looks like c crap
Can't speak for many BASIC dialects but those that I'm familiar with do have the one-liner:

VBA:
  If result = 1 Then MsgBox "hi"
Gfa:
  IF IsZoomed(hDlg&) THEN ~ShowWindow(hDlg&,SW_RESTORE)
PowerBasic:
  IF BeepOn THEN BEEP

hutch--

JJ,

The notation is ugly but it seems to work.


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    iff MACRO arg1,arg2
      .if arg1
        arg2
      .endif
    ENDM

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    mov eax, 1
    iff eax !!= 1,<mov eax, 3>
    print ustr$(eax),13,10

    mov eax, 1
    iff eax == 1,<mov eax, 3>
    print ustr$(eax),13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start


Now here is a variant that is truly disgusting.  :icon_mrgreen:



  ; mi - multiple instruction

    mi MACRO arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arga,argb,argc,argd,arge,argf,argg
      arg1
      arg2
      arg3
      arg4
      arg5
      arg6
      arg7
      arg8
      arg9
      arga
      argb
      argc
      argd
      arge
      argf
      argg
    ENDM

    mi <.if eax !!= 1>,<mov eax, 1>,<add eax, 1>,<add eax, 1>,<add eax, 1>,<.endif>
    print ustr$(eax),13,10


Paulo

hutch

How about _If ? (like jj2007 originally wrote).
I find that _If just looks better then iff .

Like in C/C++, where an underscore followed by an uppercase letter is reserved for macros and libs (I think).

qWord

include \masm32\include\masm32rt.inc

when EQU .until !
then macro action:VARARG
LOCAL lbl
    jmp lbl
    .repeat
       
        action
        .break
    lbl:
    EXITM <>
endm

.code
main proc
   
    when TRUE then (mov esi,1234)
    mov edi,123

    when (eax == eax && esi >= edi) then (print "foo^2",13,10)
   
    when (rv(GetTickCount) != 0 && eax < -1 ) then (print "GTC is in some range",13,10)

    inkey
    exit
main endp
end main

:biggrin:
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

#11
Quote from: hutch-- on October 07, 2013, 05:36:07 AM
The notation is ugly but it seems to work.

Hutch,
It's almost as elegant as the Chinese version ;)

But still, I like staying as close as possible to existing Basic syntax - and I found a solution for the bloody !!!! problem:

include \masm32\include\masm32rt.inc

_If MACRO arg:VARARG
LOCAL is
  is INSTR <arg>, <Then>
  ife is
   .err @CatStr(<*** no Then in line >, %@Line, < ***>)
  else
   ifidni @SubStr(<arg>, 1, 3), <not>
      .if !@SubStr(<arg>, 5, is-5)
         @SubStr(<arg>, is+5)
      .endif
   else
      .if @SubStr(<arg>, 1, is-1)
         @SubStr(<arg>, is+5)
      .endif
   endif
  endif
ENDM

.code
start:   or ebx, -1
   mov edi, 123
   _If not (ebx==1 && edi==999) Then print "Condition A is FALSE", 13, 10
   _If ebx==-1 && edi==123 Then print "Condition B is TRUE", 13, 10
   inkey "ok?"
   exit
end start


Note: The _If not ... variant requires brackets only if there is more than one condition, i.e. _If not eax Then ... is valid syntax. The positive _If ... Then variant never requires brackets.

hutch--


jj2007

Neat trick, qWord :t
    nops 4
    when ebx==edi then (mov esi,1234)
    nops 4
    _If ebx==edi Then mov esi,1234
    nops 4

00401001       ³.  90                  nop
00401002       ³.  90                  nop
00401003       ³.  90                  nop
00401004       ³.  90                  nop
00401005       ³. EB 07               jmp short 0040100E
00401007       ³>  BE D2040000         mov esi, 4D2
0040100C       ³. EB 04               jmp short 00401012
0040100E       ³>  3BDF                cmp ebx, edi
00401010       ³. 75 F5               jne short 00401007
00401012       ³>  90                  nop
00401013       ³.  90                  nop
00401014       ³.  90                  nop
00401015       ³.  90                  nop
00401016       ³.  3BDF                cmp ebx, edi
00401018       ³. 75 05               jne short 0040101F
0040101A       ³.  BE D2040000         mov esi, 4D2
0040101F       ³>  90                  nop
00401020       ³.  90                  nop
00401021       ³.  90                  nop
00401022       ³.  90                  nop


Could be a little bit shorter under the hood, though ;-)

Gunther

Ah, the macro specialists have found the solution. It's a bit macro mania. :t

Gunther
You have to know the facts before you can distort them.