The MASM Forum

General => The Laboratory => Topic started by: jj2007 on July 22, 2013, 06:48:49 AM

Title: Macro for checking previous instructions
Post by: jj2007 on July 22, 2013, 06:48:49 AM
I'd like to have a macro that tells me the content of the previous byte in the code section, like this:

include \masm32\include\masm32rt.inc

mytest MACRO
  if [$-1] eq 90   ; error A2094: operand must be relocatable
   echo NOP
  else
   echo not a nop
  endif
ENDM

.code
start:
   db 90   ; nop
   mytest   ; tell me if the previous instruction is a nop
   exit

end start


That should be possible for the assembler, but what I tried just throws errors. Any ideas?

Thanks, jj
Title: Re: Macro for checking previous instructions
Post by: qWord on July 22, 2013, 07:55:51 AM
That should be possible for the assembler
I'm afraid this is not possible with MASM.
Title: Re: Macro for checking previous instructions
Post by: jj2007 on July 22, 2013, 08:06:43 AM
That's what I suspected. Bad luck :(
Thanks, qWord.
Title: Re: Macro for checking previous instructions
Post by: dedndave on July 22, 2013, 08:12:08 AM
Code: [Select]
  if [$-1] eq 90   ; error A2094: operand must be relocatable
it's an assembly-time conditional, as i'm sure you know
they have to be resolved on the first pass (i.e., before the value of the byte is known)

you can do it with a run-time conditional, but that's probably not what you want
Title: Re: Macro for checking previous instructions
Post by: jj2007 on July 22, 2013, 08:20:58 AM
they have to be resolved on the first pass (i.e., before the value of the byte is known)

Right. Would be difficult indeed...
Title: Re: Macro for checking previous instructions
Post by: japheth on July 22, 2013, 03:35:59 PM
I'm afraid this is not possible with MASM.

True. However, FASM can do it - it has LOAD and STORE directives. STORE is needed because FASM's ORG directive works fairly differently than MASM's.
Title: Re: Macro for checking previous instructions
Post by: GoneFishing on July 22, 2013, 10:07:14 PM
Hi Jochen!
I didn't try to write such a macro but next lines worked for me ( I checked it in Olly)

Code: [Select]
.code
start:
   db 90   ; nop
   mov eax,$-1
   fn MessageBox,0,uhex$([eax]),"AL=90?",MB_OK
   exit

end start

Title: Re: Macro for checking previous instructions
Post by: sinsi on July 22, 2013, 10:27:50 PM
Nitpickers corner: 90 isn't NOP :biggrin:
Title: Re: Macro for checking previous instructions
Post by: GoneFishing on July 23, 2013, 12:21:45 AM
ATTENTION! : Execute it in debugger
 I used Olly. All worked well.
 
Code: [Select]
mytest MACRO
       mov eax,$-1
       mov eax,[eax]
       EXITM <eax>
ENDM

    .code

start:

db 0cch  ; hardcoded BreakPoint
db 90h   ; not NOP ;)
fn MessageBox,0,uhex$(mytest()),"AL=90?",MB_OK
exit

sinsi:
        did you mean that opcode 90h performs some operation inside the register ?
Title: Re: Macro for checking previous instructions
Post by: jj2007 on July 23, 2013, 12:27:45 AM
sinsi:
        did you mean that opcode 90h performs some operation inside the register ?

No, he's just teasing me. I had used 90d, it should be 90h :P

In the meantime, I've solved the problem with runtime instructions; see last attachment (http://masm32.com/board/index.php?topic=2137.msg22252#new), search inside the *.asm file for 22.7.13

Thanks to everybody :icon14: