Author Topic: Macro to test if arg is a direct value or memorry address/register ?  (Read 3881 times)

gelatine1

  • Member
  • **
  • Posts: 217
Hello, I don't know if this is possible but I would like to create a macro that can tell whether or not the argument is an immediate operand or not.

so for example

Code: [Select]
.data
value dd 5

.code
Tst MACRO arg:REQ
     .IF arg=immediate ... ;obviously should be something different that does work
         EXITM %1
    .ELSE
        EXITM %0
    .ENDIF
ENDM

start:
    mov eax,Tst(5) ;should put value of 1 in eax
    mov eax,Tst(value) ;should put value of 0 in eax
end start

Is this impossible to achieve ? or isn't it ?

Thanks in advance

GoneFishing

  • Member
  • *****
  • Posts: 1071
  • Gone fishing
Re: Macro to test if arg is a direct value or memorry address/register ?
« Reply #1 on: October 22, 2015, 03:01:13 AM »
sure you can :
Quote
Tst MACRO arg:REQ
     IF OPATTR(arg) EQ 24h
         %echo arg is immediate operand
    ELSE
         %echo arg is  not immediate operand
    ENDIF
ENDM

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: Macro to test if arg is a direct value or memorry address/register ?
« Reply #2 on: October 22, 2015, 04:09:17 AM »
Excerpt from MasmBasic.inc (there were differences between Masm and JWasm, but not sure if they still exist):

;     OPATTR guide
;     Bit    Set If...
;     0      References a code label
;     1      Is a memory expression or has a relocatable data label
;     2      Is an immediate expression
;     3      Uses direct memory addressing, i.e. is an absolute memory reference
;     4      Is a register expression
;     5      References no undefined symbols and is without error
;     6      References a stack location (usually a LOCAL variable or parameter)
;     7      References an external label
;     8-10   Language type (0=no type)
                        ; 76543210      ; use and 127 to mask external label and language bits
atMemory      = 34      ;
00100010      ; [edx+20], [ebx+20], [eax+edx+20], JWasm: [eax+4*eax+20], [eax+20]
atImmediate   = 36      ;
00100100
atLabel       = 37      ;
10100101
atOffset      = 38      ;
10100110      ; offset CrLf$ (immediate and mem expression)
atGlobal      = 42      ;
10101010      ; CrLf$, Masm: [eax+4*eax+20], [eax+20]
atRegLabel    = 43      ;
10101011      ; Masm: [eax+start] (Jwasm yields 37)
atRegister    = 48      ;
00110000      ; also xmm
atXmm         = 77      ; xxxxxxxx      ; reg starting with x
atLocal       = 98      ; 01100010      ; [esp+20], [ebp+20]

gelatine1

  • Member
  • **
  • Posts: 217
Re: Macro to test if arg is a direct value or memorry address/register ?
« Reply #3 on: October 22, 2015, 06:24:31 AM »
Thanks! Seems to be exactly what I needed  :t

gelatine1

  • Member
  • **
  • Posts: 217
Re: Macro to test if arg is a direct value or memorry address/register ?
« Reply #4 on: October 23, 2015, 05:45:22 AM »
Well.. I just figured out that OPATTR(1.0) is 0. Is this supposed to be happening or is that just because floating point constants are not supported ?

qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: Macro to test if arg is a direct value or memorry address/register ?
« Reply #5 on: October 23, 2015, 06:15:38 AM »
Is this supposed to be happening or is that just because floating point constants are not supported ?
yes, FP constants are not supported.
Some days ago, there was a similar question: http://masm32.com/board/index.php?topic=4709.0
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: Macro to test if arg is a direct value or memorry address/register ?
« Reply #6 on: October 23, 2015, 06:45:30 AM »
Well.. I just figured out that OPATTR(1.0) is 0.

See qWord's answer. Don't rely on opattr(...) eq 0, sometimes the assembler throws an error instead, especially earlier ML versions.

gelatine1

  • Member
  • **
  • Posts: 217
Re: Macro to test if arg is a direct value or memorry address/register ?
« Reply #7 on: October 23, 2015, 06:51:26 AM »
Well.. I just figured out that OPATTR(1.0) is 0.

See qWord's answer. Don't rely on opattr(...) eq 0, sometimes the assembler throws an error instead, especially earlier ML versions.

yep I had just tried that :p

Is this supposed to be happening or is that just because floating point constants are not supported ?
yes, FP constants are not supported.
Some days ago, there was a similar question: http://masm32.com/board/index.php?topic=4709.0

 :icon_eek: :shock: Well...... Why didn't I see this topic before ???  :dazzled: Thanks alot anyway!! And I never actually realised that I could treat the 1.0 as a string too (or any other xxx.yyyyy number) :redface: