Author Topic: If condition Then action one-liner  (Read 18621 times)

jj2007

  • Member
  • *****
  • Posts: 11546
  • Assembler is fun ;-)
    • MasmBasic
If condition Then action one-liner
« on: October 06, 2013, 04:20:08 AM »
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

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: If condition Then action one-liner
« Reply #1 on: October 06, 2013, 09:56:18 AM »
Jochen,

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

But the idea is nice.

Gunther
Get your facts first, and then you can distort them.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8479
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: If condition Then action one-liner
« Reply #2 on: October 06, 2013, 10:52:39 AM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 11546
  • Assembler is fun ;-)
    • MasmBasic
Re: If condition Then action one-liner
« Reply #3 on: October 06, 2013, 05:47:56 PM »
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 :(

Quote
I 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

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: If condition Then action one-liner
« Reply #4 on: October 06, 2013, 06:15:18 PM »
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

  • Member
  • ***
  • Posts: 467
Re: If condition Then action one-liner
« Reply #5 on: October 07, 2013, 01:52:55 AM »
Doesn't look at all like basic, looks like c crap

japheth

  • Guest
Re: If condition Then action one-liner
« Reply #6 on: October 07, 2013, 02:01:24 AM »
Doesn't look at all like basic, looks like c crap

 :icon_mrgreen:

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

Code: [Select]
i == 1 ? printf("i is 1\n") : printf("i is not 1\n");

jj2007

  • Member
  • *****
  • Posts: 11546
  • Assembler is fun ;-)
    • MasmBasic
Re: If condition Then action one-liner
« Reply #7 on: October 07, 2013, 02:28:53 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--

  • Administrator
  • Member
  • ******
  • Posts: 8479
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: If condition Then action one-liner
« Reply #8 on: October 07, 2013, 05:36:07 AM »
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

hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Paulo

  • Guest
Re: If condition Then action one-liner
« Reply #9 on: October 07, 2013, 07:04:45 AM »
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

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: If condition Then action one-liner
« Reply #10 on: October 07, 2013, 07:32:19 AM »
Code: [Select]
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

  • Member
  • *****
  • Posts: 11546
  • Assembler is fun ;-)
    • MasmBasic
Re: If condition Then action one-liner
« Reply #11 on: October 07, 2013, 08:42:27 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.
« Last Edit: October 07, 2013, 09:57:31 AM by jj2007 »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8479
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: If condition Then action one-liner
« Reply #12 on: October 07, 2013, 08:45:36 AM »
qword,

 :t
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 11546
  • Assembler is fun ;-)
    • MasmBasic
Re: If condition Then action one-liner
« Reply #13 on: October 07, 2013, 09:08:17 AM »
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

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: If condition Then action one-liner
« Reply #14 on: October 07, 2013, 09:15:34 AM »
Ah, the macro specialists have found the solution. It's a bit macro mania. :t

Gunther
Get your facts first, and then you can distort them.