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 (http://masm32.com/board/index.php?topic=94.0)
.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
I totally agree Jochen. This MACRO is really full of
possibilities. Thanks to Greg Falen :t
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:
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
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 ::)
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
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:
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:
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:
Quote from: jj2007 on January 01, 2013, 07:44:03 PMHappy Durchfall!?
Wirklich besorgniserregend um was sich dein Gedankenwelt dreht...
Hi Japheth,
Congratulations.
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
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 (http://translate.google.it/#auto/en/Wer%20in%20C%2B%2B%20keinen%20Durchfall%20will%2C%20muss%20vorher%20brechen) ::)
Anyway, this thread was seriously derailed by the German brigade, starting with replies #3 & #6 - apologies to the rest of the World :P
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:
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
google translate can only do so much
i suspect the translation loses some of its' "flavour" :lol:
Hi Dave,
Quote from: dedndave on January 02, 2013, 05:17:13 AM
google translate can only do so much
i suspect the translation loses some of its' "flavour" :lol:
no, not really. :lol:
Gunther
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.