News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Help creating macro to duplicate instructions

Started by sergeyn, April 15, 2014, 08:19:51 AM

Previous topic - Next topic

sergeyn

Hi,
I'm struggling to create a macro which duplicates instructions:
what I want to achieve is instead of writing this:
mov ymm1, [rax + 32*0]
mov ymm2, [rax + 32*1]
mov ymm3, [rax + 32*2]

I want to write like this:

DUPINSTR <mov ymm&i, [rax + 32*(&i+1)]>

How do I create such a macro ?

Thanks

jj2007

Like this, for example (with xmm because my CPU doesn't know about ymm):

include \masm32\include\masm32rt.inc
.686
.xmm

DUPINSTR MACRO diLine, range
LOCAL isdots, iseq, is, ctStart, ctEnd, ct, tmp$, di$, diR$
  iseq INSTR <range>, <=>
  isdots INSTR <range>, <..>
  tmp$ SUBSTR <range>, iseq+1, isdots-iseq-1
  ctEnd SUBSTR <range>, isdots+2
  ct=ctEnd-tmp$
  ctStart=tmp$
  REPEAT ct
   di$ CATSTR <diLine>, <  >
   is INSTR <diLine>, <&i>
   While is
      diR$ SUBSTR di$, is+2
      diL$ SUBSTR di$, 1, is-1
      di$ CATSTR diL$, %ctStart, diR$
      is INSTR di$, <&>
   ENDM
   % echo ## di$
   di$
   ctStart=ctStart+1
  ENDM
ENDM

.code
TestArray   dq 100h, 200h, 300h, 400h, 500h, 600h, 700h, 800h, 900h, 1000h

start:
  mov eax, offset TestArray
  int 3   ; for Olly
  ; DUPINSTR <mov ymm&i, [rax + 32*(&i+1)]>
  DUPINSTR <movlps xmm&i, qword ptr [eax + 32*(&i+1)]>, i=0..3
  exit

end start

<ModuleEntryPoint>  Ú$  B8 00104000       mov eax, 00401000
00401055            ³.  CC                int3
00401056            ³.  0F1240 20         movlps xmm0, [eax+20]
0040105A            ³.  0F1248 40         movlps xmm1, [eax+40]
0040105E            ³.  0F1250 60         movlps xmm2, [eax+60]
00401062            ³.  6A 00             push 0                                 ; ÚExitCode = 0
00401064            À.  E8 01000000       call <jmp.&kernel32.ExitProcess>       ; ÀKERNEL32.ExitProcess

sergeyn

Thank you very much, that works.

However, to make it more generic , do you think it is possible to support subscripts like this xmm&i+1  ?  Or maybe written like this : xmm&%i+1 ? Just some way to get computed register index.

Another question- why doesn't this code work:

expand macro text,i
  text
endm

...

expand <mov xmm&i,xmm&i>, 2

Thanks.

jj2007

Works with ML 6.14 and 6.15 but not with higher ML versions or JWasm:

  DUPINSTR <movlps xmm(&i+1), qword ptr [eax + 32*(&i+1)]>, i=0..3

Re expand: Why should it work?

sergeyn

QuoteWorks with ML 6.14 and 6.15 but not with higher ML versions or JWasm
No way to make it work with ML 12 ?

QuoteWhy should it work?
Because '&' is a text substitution operator and there is a variable 'i' inside macro 'expand' ?

Thanks.

qWord

Quote from: sergeyn on April 15, 2014, 09:24:03 AMBecause '&' is a text substitution operator and there is a variable 'i' inside macro 'expand' ?
As you said right, it does substitute text: search string x an replace it with text y. A simple solution that might not look that intuitive is:
e macro expr
    EXITM %expr
endm

DUPINSTR macro iStart:=<0>,iEnd:req,iIncr:=<1>,txt:=<>
    tmpMacro macro i
    %   txt
    endm
    dupi_cntr = iStart
    WHILE dupi_cntr LE iEnd
        tmpMacro %dupi_cntr
        dupi_cntr = dupi_cntr + iIncr
    ENDM
endm
;...
DUPINSTR 0,6,1,<movaps xmm&e(&i+1), OWORD ptr [eax + 32*&i]>

If evaluation is needed, use "&e(expression)". Remarks that the angle brackets <> are needed.
(you might change the macro name "e"...)
MREAL macros - when you need floating point arithmetic while assembling!

sergeyn


qWord

Addendum: the exclamation mark is not needed: "&e(&i+1) also works.
MREAL macros - when you need floating point arithmetic while assembling!

jj2007


sergeyn

Hi Again

Exactly the last version you've posted gives me an error

error A2006: undefined symbol : dupi_cntr

any clue ?

Thanks.

Edit:
This works for me:
expand macro iStart:req,iEnd:req,txt:req
    local ii
    ii = iStart
    WHILE ii LE iEnd
        for i,<%ii>
            %   txt
   endm
        ii = ii + 1
    ENDM
endm

I really want to understand how preprocessor works. If you have a clue, please enlighten me why this doesn't work:

expand macro iStart:req,iEnd:req,txt:req
    local i
    i = iStart
    WHILE i LE iEnd
        %   txt
        i = i + 1
    ENDM
endm

and this doesn't work either:

expand macro i:req,iEnd:req,txt:req
    WHILE i LE iEnd
        %   txt
        i = i + 1
    ENDM
endm


Thanks again!

qWord

Quote from: sergeyn on April 17, 2014, 12:51:42 AMExactly the last version you've posted gives me an error

error A2006: undefined symbol : dupi_cntr

any clue ?
probably you used nonnumeric input for first macro parameter thus dupi_cntr is never assigned/created.

Quote from: sergeyn on April 17, 2014, 12:51:42 AMwhy this doesn't work:

expand macro iStart:req,iEnd:req,txt:req
    local i
    i = iStart
    WHILE i LE iEnd
        %   txt
        i = i + 1
    ENDM
endm
the expansion operator as first char. in line only substitute text macros - i is not a text macro.
Quote from: sergeyn on April 17, 2014, 12:51:42 AM
expand macro i:req,iEnd:req,txt:req
    WHILE i LE iEnd
        %   txt
        i = i + 1
    ENDM
endm
macro parameters are no variables - they will be substitute (text replacement) before evaluating the macro.

For more details see the MASM programmers guide chapter 9.
MREAL macros - when you need floating point arithmetic while assembling!

sergeyn

QuoteFor more details see the MASM programmers guide chapter 9.
Could you share the link ?

qWord

#12
Quote from: sergeyn on April 17, 2014, 01:54:00 AMCould you share the link ?
search engine broken?  ;)

MASM_6.1_Manuals_[High_Quality_PDF].zip

EDIT: link removed.
MREAL macros - when you need floating point arithmetic while assembling!

sergeyn

Thanks. Just wanted to have the exact version you are referring to. I've read a bunch of them and all are not providing too deep details about differences in numeric and textual variables and their use.

jj2007

Quote from: qWord on April 17, 2014, 02:02:29 AM
MASM_6.1_Manuals_[High_Quality_PDF].zip

Once you have bypassed all the ads, you may succeed in downloading MASM_6.1_Manuals_[High_Quality_PDF].exe

According to Eset, it contains Win32/4Shared.R, a trojan.

PeView reveals that the exe has lots of imports from WinHTTP.dll, such as WinHttpReadData etc...

The limit for attachments is 512 k ;-)