Author Topic: Switch/Case/Default/Endsw  (Read 21854 times)

jj2007

  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Switch/Case/Default/Endsw
« on: December 30, 2012, 09:11:24 AM »
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

  • Member
  • ****
  • Posts: 512
Re: Switch/Case/Default/Endsw
« Reply #1 on: December 30, 2012, 09:25:01 AM »
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--

  • Administrator
  • Member
  • ******
  • Posts: 10572
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Switch/Case/Default/Endsw
« Reply #2 on: December 30, 2012, 11:35:45 AM »
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:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

japheth

  • Guest
Re: Switch/Case/Default/Endsw
« Reply #3 on: January 01, 2013, 03:24:20 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:

Code: [Select]
mov eax, 1
switch eax
case 1
nop
case 2
nop
case 3
nop
case 1
nop
default
nop
endsw
ret

jj2007

  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Switch/Case/Default/Endsw
« Reply #4 on: January 01, 2013, 04:33:45 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:
Code: [Select]
#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

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: Switch/Case/Default/Endsw
« Reply #5 on: January 01, 2013, 05:18:44 AM »
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

  • Member
  • *****
  • Posts: 1475
  • The base type of a type is the type itself
    • SmplMath macros
Re: Switch/Case/Default/Endsw
« Reply #6 on: January 01, 2013, 05:33:10 AM »
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

  • Guest
Re: Switch/Case/Default/Endsw
« Reply #7 on: January 01, 2013, 04:24:51 PM »
Quote
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:

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

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

  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Switch/Case/Default/Endsw
« Reply #8 on: January 01, 2013, 07:44:03 PM »
BTW: I all along miss the C-specific fall through behavior for these macros   :icon_redface:

Happy Durchfall!?

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

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

  • Member
  • *****
  • Posts: 1475
  • The base type of a type is the type itself
    • SmplMath macros
Re: Switch/Case/Default/Endsw
« Reply #9 on: January 01, 2013, 09:42:06 PM »
Happy Durchfall!?
Wirklich besorgniserregend um was sich dein Gedankenwelt dreht...
MREAL macros - when you need floating point arithmetic while assembling!

Vortex

  • Member
  • *****
  • Posts: 2768
Re: Switch/Case/Default/Endsw
« Reply #10 on: January 01, 2013, 10:01:41 PM »
Hi Japheth,

Congratulations.

anta40

  • Member
  • ***
  • Posts: 315
Re: Switch/Case/Default/Endsw
« Reply #11 on: January 01, 2013, 11:27:19 PM »
... but output for dummy is 105 - verrry strange ::)

you forgot the break part  :P

jj2007

  • Member
  • *****
  • Posts: 13871
  • Assembly is fun ;-)
    • MasmBasic
Re: Switch/Case/Default/Endsw
« Reply #12 on: January 02, 2013, 12:22:58 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--

  • Administrator
  • Member
  • ******
  • Posts: 10572
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Switch/Case/Default/Endsw
« Reply #13 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:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Gunther

  • Member
  • *****
  • Posts: 4178
  • Forgive your enemies, but never forget their names
Re: Switch/Case/Default/Endsw
« Reply #14 on: January 02, 2013, 04:55:34 AM »
Steve,

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.