News:

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

Main Menu

HJWasm .SWITCH - .CASE - .DEFAULT - .ENDSWITCH

Started by habran, April 10, 2016, 08:26:42 PM

Previous topic - Next topic

habran

Hi nidud, I can see that there is a problem with 32 bit in first case, second and third work fine
I will work on that today and hopefuly fix it
thanks for the bugfind :t
Cod-Father

nidud

#16
deleted

nidud

#17
deleted

habran

I am following hutch's suggestion and I have already make it work without .break but without having to use JMP,
jump is built inside the case.
I have only to find a solution for multiple cases like
.case 1
.case 2
.case 3
   mov edx, 3
.case 4
   mov edx,4
Cod-Father

nidud

#19
deleted

habran

nidud, that is pretty good solution, however, I want to make it similar to C stile, I know there is the way to do that but I have to take some time to find that way
I will still go through everything and try to fix 32 bit. It works on my computer but if I change the position of those three examples I've got error A2168: General Failure
Please, be patient
Cod-Father

nidud

#21
deleted

habran

the problem appears if we have EG: 
  case 2 
  case 5
  case 9
in that case 1, case 3,  case 4, case 6 , case 7 and case 8 are default
so it would be incorect to use 2..9
I like idea to depreciate .break, so I will find the solution how to build in  the:
.case 2
.case 5
.case 9
   mov edx, 9
.case 11
   mov edx,11
I am going to do that now 8)
Cod-Father

habran

After a little bit of pondering and cooling off period, I decided to disappoint hutch (sorry mate) and give up on removing
the .BREAK
It gives to programmer more control over the code.

Because of a FIXED STACK FRAME we can not just jump to subroutines because it will cause  a chaos in the program,
we have to use invoke or call inside the case but not jmp

So, I will only work on fixing 32 bit and enhancing the intelligence of the .SWITCH - .CASE - .DEFAULT - .ENDSWITCH
Cod-Father

hutch--

Perhaps I have misunderstood what you are doing but  a switch block should have a label at the end of it once the macro is expanded so that instead of having to add "break" like old format C, if an case matches the comparison, it will perform that act then branch to the end label.

In 64 bit it will be something like this for each case statement.

lbl:
  cmp rax, value
  jne nxtlbl
  perform action
  jmp end_lbl

I take you point that a direct jump out of any procedure is dangerous as it will mess up the allocated spill space but that is not part of the design of a switch block that should only operate within an given procedure. If you want to construct a branch address table you probably can do it a bit faster but a standard sequential compare is probably fast enough in almost all cases.

habran

 .break is exactly doing this but we don't need to give name to label exit because .break is doing just that 
JMP LOOP_EXIT

  cmp rax, value
  jne nxtlbl
  perform action
  .break    ;jmp end_lbl
Cod-Father

hutch--

 :biggrin:

Yeah, I know exactly what "break" does but my comment was in the first place, you gain nothing from adding it into a switch block where the win is supposed to be simplicity. Long ago I read an interview with both K and R and one of their laments was that the C language required the extra instruction "break" after each "case" in a switch block but that it was too late to change it. Unless you are directly aping this characteristic in C, it serves no purpose as part of the required structure of a switch block.

Still, if that's what you want, I hope it works well for you, as long as the macro system works well, there are enough folks around who can bypass this with a MASM style macro.

jj2007

Quote from: hutch-- on April 12, 2016, 05:08:34 PMthere are enough folks around who can bypass this with a MASM style macro
;)
include \masm32\include\masm32rt.inc
option dotname
.Switch equ Switch
.Case equ Case
.Default equ Default
.Endsw equ Endsw

.code
start:
  or ebx, -1
  m2m esi, 7
  .Repeat
print str$(ebx), 9
.Switch ebx
.Case 1
print "Case 1", 13, 10
.Case 0
print "NULL", 13, 10
.Case 2
print "Case 2", 13, 10
.Case 3 .. 5
print "Case three to five", 13, 10
.Case esi
print "matches esi", 13, 10
.Default
print "Default", 13, 10
.Endsw
inc ebx
  .Until ebx>9
  inkey "that was difficult, right?"
  exit
end start

nidud

#28
deleted

nidud

#29
deleted