News:

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

Main Menu

bitRAKE's SWITCH MACRO

Started by habran, February 22, 2016, 08:57:29 PM

Previous topic - Next topic

habran

jj2007, I am looking forward for a fixed version :biggrin:
  Take your time :bgrin:
Cod-Father

jj2007

Quote from: habran on February 25, 2016, 06:23:12 AM
jj2007, I am looking forward for a fixed version :biggrin:

No way, my friend :greensml:

I am forever stuck with 32-bit code. If you prefer 64 bits, fix it yourself - shouldn't be too difficult ;-)

habran

In another words: "If you want a thing done well, do it yourself." - Napoleon Bonaparte :bgrin:
Can you, at least, put some comments to the 32 bit one, please ;)
I am impressed with your macro, and I think that this one is a pearl between pebbles :eusa_clap:
When I make it work in 64 bit, with your permission, I will implement it in HJWasm, and I will give you credits for it :t   
Cod-Father

habran

Don't worry about comments, I've got it 8)
Cod-Father

jj2007

#34
Quote from: habran on February 25, 2016, 10:47:29 AM
Don't worry about comments, I've got it 8)

Great! Here is Switch_ itself with comments, as it will appear in today's release of MasmBasic. And of course, you have my permission ;-)

MbSct=0
Switch_ MACRO arg:REQ, smode
Local tmp$
  MbSct=MbSct+1
  MbC0=0 ; case 0 doesn't need first jmp, all other and endsw yes
  MbSwMax=0
  MbSwMin=07fffffffh
  MbSw$ equ <#>
  @MbSw CATSTR <@MbSw>, %MbSct
  if (opattr arg) eq atRegister
tmp$ equ <arg>
  else
mov eax, arg
tmp$ equ <eax>
  endif
  @CatStr(<cmp >, tmp$, <, MbSw>, %MbSct, <L[4]>) ; max: cmp arg, MbSw1L[4]
  @CatStr(<mov edx, MbSw>, %MbSct, <L>) ; min: mov edx, MbSw1L[0]
  ifdifi <smode>, <unsafe>
@CatStr(<jg @MbSw>, %MbSct, <D>) ; jg @MbSw1D ; default
  endif
  @CatStr(<add edx, >, tmp$) ; add edx, arg ; min+arg
  ifdifi <smode>, <unsafe>
@CatStr(<jl @MbSw>, %MbSct, <D>) ; jl @MbSw1D
  endif
  @CatStr(<jmp MbSw>, %MbSct, <L[4*edx+8]>)
ENDM


habran

Thank you so much :t
That is a WOODOSWITCH macro :bgrin:
Cod-Father

habran

After a little bit of testing I found out that this macro works fine as long as cases are close to each other,
however, if we have for example:
  .switch  somevar
  .case 3
    dosomething...
   .break
  .case 5
    dosomethingelse...
    .break
  .case 1000
     nowdodifferentthing...
  .default
    dousualthing...
  .endswitch
 
it will allocate 1000 * 4   bytes for 32 bit or 1000 * 8 bytes for 64 bit

So I will have to combine several algos for different types of cases

Anyway, this macro is amazing and I will take it as a base to develop the complex one.

bitRAKE's macro with a little bit of tweaking can be combined with jj's one for a binary tree
Cod-Father