News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

No if statement.

Started by thefs18, March 25, 2007, 09:10:54 PM

Previous topic - Next topic

thefs18

Hello. I was wondering if anybody may tell me if there is a kind of case and or switch method which i can use to check "numbers" with instead of using .ifs all the time. Cheers.  :dance:

BogdanOntanu

.IF does contain a case statement ....

.IF eax ==1
; case 1 code here
.ELSEIF eax == 2
; case 2 code here
.ELSEIF eax == 3
; case 3 code here
.ELSEIF ebx==7 .OR. ecx !=32
; funny powefull switch
.ELSE
; default/ no match case here
.ENDIF

Please note that unlike CASE / SWITCH in C here you can use another register or variable in .ELSEIF statements .
And you can also use expressions.

Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

thefs18

Yeah i already knew that. It's a bit... weird though for me. Is their anything else i can use to check the numbers with?. :\ Thanks.

ramguru

The best way, is not using both of them. SWITCH is actually IF, it's the same thing, you're not doing anything faster or better with SWITCH

thefs18

Can you show me a quick example on how to use the switch method in masm32 please ramguru ? :)

ramguru

c:\masm32\examples\exampl08\switch\

dsouza123

An alternative using what in basic was called a computed goto
implemented by a jump table.

It requires extra data to hold the jump table and code to fill the table,
(unless there is an assembly time technique to eliminate the fill code).

It doesn't use any cmp ( if ) just loads the computed jump address then jumps.


.data
   jtable    dd 4 dup (0)
   szCaption db "Example of computed goto via jump table",0
   szItem0   db "    Item 0 ",0
   szItem1   db "    Item 1 ",0
   szItem2   db "    Item 2 ",0
   szItem3   db "    Item 3 ",0

.code
start:
   mov jtable+0*4, offset G0
   mov jtable+1*4, offset G1
   mov jtable+2*4, offset G2
   mov jtable+3*4, offset G3

   mov ebx, 0
L0:
   mov eax, [jtable+ebx*4]
   jmp eax
G0:
   invoke MessageBox, NULL, ADDR szItem0, ADDR szCaption, MB_OK
   jmp Past
G1:
   invoke MessageBox, NULL, ADDR szItem1, ADDR szCaption, MB_OK
   jmp Past

[attachment deleted by admin]

thefs18

I appreciate every bodies suggestions.

dsouza123

Success !

The fill code is eliminated.  Now it is done at assembly time.


.data
   jtable    dd G0, G1, G2, G3
   szCaption db "Example of computed goto via jump table",0

[attachment deleted by admin]