Author Topic: Macro for checking previous instructions  (Read 6550 times)

jj2007

  • Member
  • *****
  • Posts: 11526
  • Assembler is fun ;-)
    • MasmBasic
Macro for checking previous instructions
« 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

qWord

  • Member
  • *****
  • Posts: 1476
  • The base type of a type is the type itself
    • SmplMath macros
Re: Macro for checking previous instructions
« Reply #1 on: July 22, 2013, 07:55:51 AM »
That should be possible for the assembler
I'm afraid this is not possible with MASM.
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

  • Member
  • *****
  • Posts: 11526
  • Assembler is fun ;-)
    • MasmBasic
Re: Macro for checking previous instructions
« Reply #2 on: July 22, 2013, 08:06:43 AM »
That's what I suspected. Bad luck :(
Thanks, qWord.

dedndave

  • Member
  • *****
  • Posts: 8829
  • Still using Abacus 2.0
    • DednDave
Re: Macro for checking previous instructions
« Reply #3 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

jj2007

  • Member
  • *****
  • Posts: 11526
  • Assembler is fun ;-)
    • MasmBasic
Re: Macro for checking previous instructions
« Reply #4 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...

japheth

  • Guest
Re: Macro for checking previous instructions
« Reply #5 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.

GoneFishing

  • Member
  • *****
  • Posts: 1071
  • Gone fishing
Re: Macro for checking previous instructions
« Reply #6 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


sinsi

  • Guest
Re: Macro for checking previous instructions
« Reply #7 on: July 22, 2013, 10:27:50 PM »
Nitpickers corner: 90 isn't NOP :biggrin:

GoneFishing

  • Member
  • *****
  • Posts: 1071
  • Gone fishing
Re: Macro for checking previous instructions
« Reply #8 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 ?

jj2007

  • Member
  • *****
  • Posts: 11526
  • Assembler is fun ;-)
    • MasmBasic
Re: Macro for checking previous instructions
« Reply #9 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, search inside the *.asm file for 22.7.13

Thanks to everybody :icon14: