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

qWord

Quote from: jj2007 on April 17, 2014, 03:04:09 AM
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 ;-)
yep, just copied that link from the old board...    :icon_confused:
MREAL macros - when you need floating point arithmetic while assembling!


qWord

MREAL macros - when you need floating point arithmetic while assembling!

jj2007

Looks more trustworthy indeed :t

QuoteLEA    Load Effective Address
Calculates the effective address (offset) of the source memory operand and stores the result in the destination register. If the source operand is a direct memory address, the assembler encodes the instruction in the more efficient MOV reg,immediate form (equivalent to MOV reg, OFFSET mem).

Hmmm... my assemblers don't do that :(

QuoteA Word About Instruction Timings
...
• Smaller is often better. For example, the instructions
   dec  bx
   sub  bx, 1
accomplish the same thing and have the same timings on 80386/486 processors.
But the first instruction is 3 bytes smaller than the second, and so may reach the
processor faster.
• When possible, use the string instructions described in Chapter 5, "Defining and
Using Complex Data Types."
:greensml:

Gunther

You have to know the facts before you can distort them.

dedndave

the masm assembler does make a few substitutions for you   :P
    xchg    edx,eax  ;2 bytes
    xchg    eax,edx  ;1 byte

qWord

Coming back to the topic, I just thought the macro could be generalized with a few modifications:
e macro expr
EXITM %expr
endm

FOR_ macro start:=<i=0>,condition:req,increment:=<i=i+1>,statement:=<>
% tmpMacro1 macro @SubStr(<&start>,1,@InStr(1,<&start>,<=>)-1)
% statement
endm
tmpMacro2 macro varName
LOCAL varName
start
WHILE condition
tmpMacro1 %varName
increment
ENDM
endm
%   tmpMacro2 @SubStr(<&start>,1,@InStr(1,<&start>,<=>)-1)
endm

-->
FOR_ k=0,k LT 7,k=k+1,<movdqa xmm&k,OWORD ptr [eax+k*OWORD]>
The variable name is freely selectable.
With some more lines and if one like that syntax:
FOR_ macro start:=<i=0>,condition:req,increment:=<i=i+1>,statements:=<>
for_vars TEXTEQU <>
for_expvars TEXTEQU <>
FOR arg,<&start>
for_vars TEXTEQU for_vars,<,>,@SubStr(<&arg>,1,@InStr(1,<&arg>,<=>)-1)
for_expvars TEXTEQU for_expvars,<,!%>,@SubStr(<&arg>,1,@InStr(1,<&arg>,<=>)-1)
ENDM
for_vars SUBSTR for_vars,2
for_expvars SUBSTR for_expvars,2
% tmpMacro1 macro &for_vars
FOR statement,<&statements>
% statement
ENDM
endm
tmpMacro2 macro varNames:VARARG
LOCAL varNames
FOR arg,<&start>
arg
ENDM
WHILE condition
% tmpMacro1 &for_expvars
FOR arg,<&increment>
arg
ENDM
ENDM
endm
% tmpMacro2 &for_vars
endm

-->
FOR_ i=0,i LT 7,i=i+1,\
<<movdqa xmm&e(i+1),OWORD ptr [eax+i*OWORD]>>
;...
FOR_ <i=0,j=7>,i LT 4,<i=i+1,j=j-1>,\
<\
<mov al,sz[i]>,\
<mov ah,sz[j]>,\
<mov sz[i],ah>,\
<mov sz[j],al>,\
>

:biggrin:

MREAL macros - when you need floating point arithmetic while assembling!

sergeyn

Hello again,

Sorry for noobie questions - trying to extend the expand macro to support multi-line generation, and stuck with a very simple thing.

For debugging purposes trying to print the value of a variable:
work MACRO args:VARARG
work MACRO
local ii
ii = 999
ECHO !ii is: %ii
ENDM
work


I get this printed:

!ii is: ??0000

What am I doing wrong?

jj2007

work MACRO args:VARARG
local ii, tmp$
  ii = 999
  tmp$ CATSTR <!ii is: >, %ii, < with !args=>, <args>
  % echo tmp$
ENDM

sergeyn

Ok, so my mistake was that everything after 'echo' is simply dumped to the screen without any substitutions.

How do you guys debug macros ? Is there a way to generate preprocessed file similar to what /P flag of cl.exe does?  The closest I found to see what's going on inside is "/Sa Maximize source listing". I basically want to see what the evaluator gets on it's input.

Another issue I've discovered - VARARG parameters can't properly parse commas, thus  a two argument call like <1,2>,<2,3> is seen as <1>,<2>,<3>,<4> by the vararg parsing code (as described in the docs). Do you know anyway to work around that ?

Thank you for you help!

jj2007

Quote from: sergeyn on April 18, 2014, 08:52:21 PMHow do you guys debug macros ?

With tmp$ CATSTR and .err as shown below.

QuoteAnother issue I've discovered - VARARG parameters can't properly parse commas, thus  a two argument call like <1,2>,<2,3> is seen as <1>,<2>,<3>,<4> by the vararg parsing code (as described in the docs). Do you know anyway to work around that ?

Masm separates arguments by commas, but you are not forced to use commas.
include \masm32\include\masm32rt.inc

xx MACRO args:VARARG
LOCAL tmp$
  tmp$ CATSTR <Myargs=>, <args>
  % echo tmp$
  for arg, <args>
echo arg
  endm
ENDM

.code
start:
  xx <1,2>, <2,3>
  xx <1:2>, <2:3>
  .err
end start

Echos:
Myargs=1,2,2,3
1
2
2
3
Myargs=1:2,2:3
1:2
2:3