News:

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

Using the switch macro

Started by Robert Collins, December 23, 2004, 09:12:01 PM

Previous topic - Next topic

Robert Collins

Can anyone tell me how to use the switch macro? I keep getting an error "invalid instruction operands"


Here is a snippet of my code:


.386
.model flat, stdcall
option casemap:none

ExitProcess PROTO :DWORD 
                         
include    \masm32\include\windows.inc
include    \masm32\include\kernel32.inc
include    \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

include    \masm32\macros\MACROS.ASM

.data
myvalue          db   25
TextMsg          db   "Hello",0
CaptionMsg       db   "My First Assembly",0

.code
start:
        switch myvalue
          case 20
            ; do something if myvalue == 20
          case 23
            ; do something if myvalue == 23
          case 25
            ; do something if myvalue == 25
        endsw   

       invoke    ExitProcess,0
       END       start

Vortex

The switch macro expects a DWORD value:

myvalue dd 25

Robert Collins

Quote from: Vortex on December 23, 2004, 09:28:29 PM
The switch macro expects a DWORD value:

myvalue dd 25

OK, thanks. I was fighting it too much maybe. BTW: can switch use strings?

hutch--

Robert,

Here is how to do sequential string comparisons manually.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

test_string proc lpstring:DWORD

    invoke szCmp,lpstring,chr$("word1")
    test eax, eax
    jz @F
    mov eax, 1
    ret
  @@:

    invoke szCmp,lpstring,chr$("word2")
    test eax, eax
    jz @F
    mov eax, 2
    ret
  @@:

    invoke szCmp,lpstring,chr$("word3")
    test eax, eax
    jz @F
    mov eax, 3
    ret
  @@:

    xor eax, eax
    ret

test_string endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

I will have a play and see if it can be fited into an .IF block.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

hutch--

This works as well.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

test_stringx proc lpstring:DWORD

    .if FUNC(szCmp,lpstring,chr$("word1")) != 0
      mov eax, 1
      ret
    .endif

    .if FUNC(szCmp,lpstring,chr$("word2")) != 0
      mov eax, 2
      ret
    .endif

    .if FUNC(szCmp,lpstring,chr$("word3")) != 0
      mov eax, 3
      ret
    .endif

    xor eax, eax
    ret

test_stringx endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

I have not nutted out yet why it won't work using .ELSEIF .
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

gfalen

Hutch - It does'nt work with .elseif because the code for FUNC is never executed.

.if ...
cmp eax, ...
jne if_lblxxx

.elseif FUNC(...) - generates the macro code BEFORE the label for the .if test
push ...
call Szcmp...
if_lblxxx:   -  *** the previous .if code jumps here w/o executing the macro code
cmp eax, ...
jne if_lblxx1

hutch--

 :bg

Thanks Greg, I thought it was senile decay setting in.  :P
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php