News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Switch/Case/Default/Endsw

Started by jj2007, December 30, 2012, 09:11:24 AM

Previous topic - Next topic

jj2007

Only recently I discovered that the Switch macro is a real jewel, with more features than some high-level languages. Kudos to Greg Falen :t

; Masm32 is enough for this example, but it works with MasmBasic, too
include \masm32\include\masm32rt.inc
; include \masm32\MasmBasic\MasmBasic.inc        ; download

.code
start:                        ; ### Switch with lists and ranges ###
  mov eax, 17                ; try any combination you like,..
  mov edx, 77
  mov ecx, 99
  mov ebx, 17

  Switch eax
  Case 1
          print "eax is one", 13, 10
  Case edx .. ecx
          print "eax is between edx and ecx", 13, 10
  Case 10 .. 15
          print "eax is between 10 and 15", 13, 10
  Case ebx
          print "eax is the same as ebx", 13, 10
  Case 16, 18, 20
          print "eax is sixteen or eighteen or twenty", 13, 10
  Default
          print str$(eax), " is nowhere in the ranges above", 13, 10
  Endsw

  inkey "OK?"
  exit
end start

frktons

I totally agree Jochen. This MACRO is really full of
possibilities. Thanks to Greg Falen  :t
There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama

hutch--

What took you guys so long, Greg wrote that years ago and it has been super reliable. You can try out the switch$ macro as well thanks to Michael Webster, something you cannot normally do in a C compiler and there is a caseI version as well.  :biggrin:

japheth

Quote from: jj2007 on December 30, 2012, 09:11:24 AM
the Switch macro is a real jewel, with more features than some high-level languages. Kudos to Greg Falen :t

Well, yes, perhaps... but "some high-level languages" may at least do error checking:


mov eax, 1
switch eax
case 1
nop
case 2
nop
case 3
nop
case 1
nop
default
nop
endsw
ret


jj2007

Quote from: japheth on January 01, 2013, 03:24:20 AM
Well, yes, perhaps... but "some high-level languages" may at least do error checking:

I tried this in C++ but it fails miserably with "error C2051: case expression not constant" - does it mean C++ allows only immediate integers?  :dazzled:
#include "stdafx.h"
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int dummy=100;
int MyCase=2;
int cx1=1;
int cx2=2;
int cx3=3;
switch (MyCase)
{
case cx1:
dummy++;
case cx2:
dummy++;
case cx3:
dummy++;
case cx1:
dummy++;
default:
dummy++;
}
printf_s("dummy=%d", dummy);
void getch();
return 0;
}


And you are right, C++ is clever enough to see that an immediate integer has been used before. One could add that capacity to the existing macro, but I guess Forum members are smart enough to live without that.

By the way, this compiles fine:
   int dummy=100;
   int MyCase=1;
   switch (MyCase)
   {
   case 1:
      dummy++;
   case 2:
      dummy++;
   case 3:
      dummy++;
   case 4:
      dummy++;
   default:
      dummy++;
   }
   printf_s("dummy=%d\n", dummy);


... but output for dummy is 105 - verrry strange ::)

dedndave

it's assembly language
one of the reasons we like assembler is we get away from all the overhead of higher-level languages
and - we take the responsibility of doing a certain amount of error checking ourselves   :P

qWord

It is really surprising that jj needs years to find that macro for all that he is macro-power-user  :shock:

BTW: I all along miss the C-specific fall though behavior for these macros   :icon_redface:
MREAL macros - when you need floating point arithmetic while assembling!

japheth

QuoteI tried this in C++ but it fails miserably with "error C2051: case expression not constant" - does it mean C++ allows only immediate integers?  :dazzled:

It allows constants only, yes. This restriction is a feature.  :icon_eek:

QuoteOne could add that capacity to the existing macro ...

Since it allows variables that might turn out to be rather tricky ... or "impossible".

Quote... but I guess Forum members are smart enough to live without that.

Hm, I probably wouldn't give such a generic absolution.

Quote... but output for dummy is 105 - verrry strange ::)
:bgrin:

jj2007

Quote from: qWord on January 01, 2013, 05:33:10 AM
BTW: I all along miss the C-specific fall through behavior for these macros   :icon_redface:

Happy Durchfall!?

Quote from: japheth on January 01, 2013, 04:24:51 PM
QuoteI tried this in C++ but it fails miserably with "error C2051: case expression not constant" - does it mean C++ allows only immediate integers?  :dazzled:

It allows constants only, yes. This restriction is a feature.  :icon_eek:

Congrats for your new job in Redmond, Japheth :t

and Happy New Year to all of you :icon14:

qWord

Quote from: jj2007 on January 01, 2013, 07:44:03 PMHappy Durchfall!?
Wirklich besorgniserregend um was sich dein Gedankenwelt dreht...
MREAL macros - when you need floating point arithmetic while assembling!

Vortex


anta40

Quote from: jj2007 on January 01, 2013, 04:33:45 AM
... but output for dummy is 105 - verrry strange ::)

you forgot the break part  :P

jj2007

Quote from: anta40 on January 01, 2013, 11:27:19 PM
Quote from: jj2007 on January 01, 2013, 04:33:45 AM
... but output for dummy is 105 - verrry strange ::)

you forgot the break part  :P

Y'know, Google translate gives me very strange advice ::)
Anyway, this thread was seriously derailed by the German brigade, starting with replies #3 & #6 - apologies to the rest of the World :P

hutch--

Would a more tolerant translation of "Wer in C++ keinen Durchfall will, muss vorher brechen" be something like this ?

"If you don't wish to end up in the sewerage, exit the branch before you run into the next one."   :biggrin:

Gunther

Steve,

Quote from: hutch-- on January 02, 2013, 12:35:26 AM
Would a more tolerant translation of "Wer in C++ keinen Durchfall will, muss vorher brechen" be something like this ?

"If you don't wish to end up in the sewerage, exit the branch before you run into the next one."   :biggrin:

it's exactly your translation proposal.  :lol: :lol: :lol: Well done!

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