| Index | Manual | Download | Source | Macros | Embedded word Case ins.
Sitemap | Links | Forum | Tests | Projects

# cpuext.htm
Macros
Dispatch

This file can be included to programs written in Euro Assembler.
The library contains OS-independent and width-independent macroinstructions with calling convention similar to the standard Intel machine instruction set.

See also cpuext16.htm, cpuext32.htm, cpuext64.htm.

    cpuext HEAD

↑ Dispatch Control, Case1, Case2, Case3,...

  Macro Dispatch implements the programming figure switch. It compares value of the Control operand (register or memory) with one or more Case values and, when equal, it jumps to the corresponding label .Case: constructed from the compared value.
  Macro does nothing when none of Case values was found equal to Control at run-time.

  The label of target jump is constructed from the value by prefixing its verbatim notation with fullstop `.` and appending it with colon `:`, so it creates a local label. The label must exist somewhere in Dispatch's namespace, of course.

  > When the macro Dispatch is used several times in the same namespace, we can exploit different notations of the same number to avoid collision. `Dispatch AL, 2` jumps to local label .2:, in other expansion of this macro we may dispatch to alternative labels which retain the value 2, such as `2d, 2D, 0n2, 0x02, 0x00_02`.

Input
  Control is a register or typed memory variable of arbitrary width.
  Case* specifies the value, comparable with Control. It can be a register of the same width as Control, or memory variable, or immediate operand.
  Immediate value can also be in single apostrophes ( character constant), both apostrophes will be stripped off to construct a valid label from character constant.

  > Only letters and digits can be used in character constants, we cannot code `Dispatch AL, '+', '-'` because `.+:` and `.-:` are invalid labels. Use `Dispatch AL, 0x2B, 0x2D` or `Dispatch AL, 43, 45` instead.

Output
  ZF=0 if the control was not dispatched, thread continues below the macro. All registers are preserved.
Examples
  Dispatch DL,0x20,'y','n',10,13 ; Jump to label .0x20: .y: .n: .10: .13: respectively. Dispatch EAX, WM_CREATE, WM_PAINT ; Jump to label .WM_CREATE:, .WM_PAINT:, respectively. JMP WinDefProc: ; Otherwise jump to WinDefProc if the value in EAX was not dispatched.

    Dispatch  %MACRO Control, Case1, Case2, , ,
     %DispatchNr  %SETA 2                     ; Start with the second operand.
          %WHILE %DispatchNr <= %#            ; Repeat with each Case* operand.                        >
            %DispatchArg %SET %*{%DispatchNr} ; Load the operand value to %DispatchArg.
            CMP %Control,%DispatchArg         ; Emit instruction for the actual comparison.
            %IF "%DispatchArg[1]%DispatchArg[1]" === "''" ; Is the operand in apostrophes?
              JE .%DispatchArg[2..%&-1]:      ; Strip them off and jump to this label.
            %ELSE
              JE .%DispatchArg:               ; Use local label made from Case.
            %ENDIF
            %DispatchNr %SETA %DispatchNr+1   ; The next Case*.
          %ENDWHILE
     %ENDMACRO Dispatch

     ENDHEAD

▲Back to the top▲