Search code examples
macrostasm

Does TASM allow a macro to be used as an operand?


I am attempting to port a macro from MASM6 to TASM5 (in IDEAL mode) and I am encountering errors. The macro itself assembles fine, but when I attempt to call it, I receive the following error during assembly:

Error xxx.asm(##) Can't use macro name in expression: M_SWAP16

The macro takes the numeric value from a text macro and performs a byte swap. The macro is generally called with ops that take immediate values or during variable initialization.

MACRO M_swap16 operand
 LOCAL result
 result = (((operand and 0FFh) shl 8) or ((operand and 0FF00h) shr 8))
 exitm %result
ENDM

IPPROTO_TCP EQU 6
.
.
.
mov  [protocol], M_swap16(IPPROTO_TCP)   ; fails
.
.
.
protocol  DW  ?
protocol_default  DW  M_swap16(IPPROTO_TCP)  ; fails

It works fine in MASM 6.11. Switching TASM from IDEAL to MASM mode doesn't help. Neither does moving the macro into the EQU statement. Ideas?


Solution

  • Unfortunately TASM5 doesn't appear to support macros returning results to expressions at least according to the last official docs. This is also what the error you are seeing is saying. More specifically, the EXITM directive doesn't take an argument like MASM can regardless of the mode you are in. However TASM's macros can still emit a line of code, so if you aren't worried about passing the expression in to the macro, I propose the following workaround (IDEAL mode):

    MACRO M_swap16_EXPRESSION expr,operand
      LOCAL result
      result = (((operand and 0FFh) shl 8) or ((operand and 0FF00h) shr 8))
      expr result
    ENDM
    

    The macro above takes an additional argument "expr" as the 1st argument which is the assembly expression you were trying to plug the original expression in. It will perform the assembly-time arithmetic on the operand and emit the final assembly line. It can be used like this:

    M_swap16_EXPRESSION <mov [protocol],>,IPPROTO_TCP
    ...
    M_swap16_EXPRESSION <protocol_default DW>,IPPROTO_TCP
    

    I admit its ugly, but it might be the next best thing if you must use TASM.