The MASM Forum Archive 2004 to 2012

General Forums => The Workshop => Topic started by: thefs18 on March 25, 2007, 09:10:54 PM

Title: No if statement.
Post by: thefs18 on March 25, 2007, 09:10:54 PM
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:
Title: Re: No if statement.
Post by: BogdanOntanu on March 25, 2007, 09:33:22 PM
.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.

Title: Re: No if statement.
Post by: thefs18 on March 25, 2007, 09:36:29 PM
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.
Title: Re: No if statement.
Post by: ramguru on March 25, 2007, 09:41:36 PM
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
Title: Re: No if statement.
Post by: thefs18 on March 25, 2007, 10:01:03 PM
Can you show me a quick example on how to use the switch method in masm32 please ramguru ? :)
Title: Re: No if statement.
Post by: ramguru on March 25, 2007, 10:04:56 PM
c:\masm32\examples\exampl08\switch\
Title: Re: No if statement.
Post by: dsouza123 on March 26, 2007, 12:52:14 AM
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]
Title: Re: No if statement.
Post by: thefs18 on March 27, 2007, 11:51:28 AM
I appreciate every bodies suggestions.
Title: Re: No if statement.
Post by: dsouza123 on March 27, 2007, 09:13:56 PM
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]