News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Randomly select a macro MASM

Started by Activia, July 01, 2009, 11:59:34 PM

Previous topic - Next topic

Activia

Hello,
How to make a macro that chose one of two macros without arguments ?

Thank you !

Useless example :


mov eax, 1
mov ebx, 2
mov edi, 3
mov esi, 4
random ; macro chosen macro_1 or macro_2 without argument
invoke sleep, 1000



random macro
  ; macro chosen macro_1 or macro_2 or macro_3 or ....
endm

macro_1 macro
  push eax
  push ebx
endm

macro_2 macro
  push esi
  push edi
endm



dedndave

Hi Activia,
welcome to masm32 forum
i would use QueryPerformanceCounter to get a timer value
take the least signifigant bit and use that to select the macro for you

        push    eax
        sub     esp,8
        INVOKE  QueryPerformanceCounter,esp
        pop     eax
        pop     edx
        and     al,1
        pop     eax
        jz      ChoosePair1

ChoosePair0:
        push    eax
        push    ebx
        jmp short PairChosen

ChoosePair1:
        push    esi
        push    edi

PairChosen:
.
.
.

Activia

Thank you :p but it's not that.

Sorry, I had difficulty explaining.
I don't want add code... 100% random macro

Example after compilation :


mov eax, 1
mov ebx, 2
mov edi, 3
mov esi, 4
RANDOM ; this macro chosen macro_1 or macro_2 without argument


AFTER COMPILATION :

mov eax, 1
mov ebx, 2
mov edi, 3
mov esi, 4
PUSH EAX   ; RANDOM macro chose macro_1
PUSH EBX


OR

mov eax, 1
mov ebx, 2
mov edi, 3
mov esi, 4 
PUSH ESI  ; RANDOM macro chose macro_2
PUSH EDI



MichaelW

I'm not sure what you are asking for, but perhaps this will do what you want:

http://www.masm32.com/board/index.php?topic=9018.0

eschew obfuscation

dedndave

QuoteI'm not sure what you are asking for
me either - i thought i understood - lol
i wonder if he (or she) tried my code - lol
many people may not realize that QueryPerformanceCounter spits out more or less pseudo-random stuff

but, i see no need for use of the stack for these values
and, i see no need for use of macros
so, either we are not getting the big picture, or it is more complicated than it needs to be

Activia, if you tell use what you want the end result to be and what you are using it for, we may be able to simplify it for you

Activia

I have a sample in NASM.
I want the same code in MASM.

The guy use "nasmw.exe -w-number-overflow -fobj -dkey=%key% -O3 %name%.asm " for choose randomly macros.

So, I can use "/c /coff /Cp /nologo /DRANDOM=%key%

Thank you for you link MichaelW and thank you dedndave ;)

[attachment deleted by admin]

NightWare

hmm, a compiler isn't supposed to make random choice... it could be used to generate many files doing the same thing with differents signatures, possibly interesting for app/video game to avoid patchs (but disallow a possible update later). now i also see another usage, but you don't want to do that ! do you ?

anyway, where is the interest to "protect" your code like that ? if you have read the french doc included, IT DOES EXACTLY THE SAME THING. you should better use a key/crypting method (plus, here updates will be possible). here an example (code in french) : http://www.masm32.com/board/index.php?topic=8429.0

dedndave

if it smells like a fish and flaps its tail....
prolly a fish

Activia

LOL, it's not for virus !

Yes, my sample NASM is confusing lol... but I am interested in the random choice when compiling.

Quoteif you have read the french doc included, IT DOES EXACTLY THE SAME THING.
Yes yes, that's why I do not want to encrypt my code ;)

hmm, a compiler isn't supposed to make random choice...

hmm, this code works to start :

@ECHO OFF
SET h1=%time:~0,1%
SET h2=%time:~1,1%
SET m1=%time:~3,1%
SET m2=%time:~4,1%
SET s1=%time:~6,1%
SET s2=%time:~7,1%
SET c1=%time:~9,1%
SET c2=%time:~10,1%

SET j1=%date:~0,1%
SET j2=%date:~1,1%
SET m1=%date:~3,1%
SET m2=%date:~4,1%

SET a1=%date:~6,1%
SET a2=%date:~7,1%
SET a3=%date:~8,1%
SET a4=%date:~9,1%

SET key=%c1%%s2%%c2%%c1%%m2%%c2%%j2%%m2%

@ECHO ON
C:\masm32\bin\ml.exe /c /coff /Cp /nologo /DRANDOM=%key%  "Body.asm"
C:\masm32\bin\link.exe  /SUBSYSTEM:WINDOWS /RELEASE /VERSION:4.0 body.obj

del *.obj
pause



testm macro

ifndef RANDOM
mov eax, 1
else
mov eax, RANDOM
endif
PrintDec eax

endm

jj2007

If you want it at assembly time, there is not much choice. Here is one:

include \masm32\include\masm32rt.inc

random MACRO
  if @Line and 1
print "Very odd!", 13, 10
  else
print "Even that one works", 13, 10
  endif
ENDM

.code
start:
random
nop
random
random
random
nop
random
random
random
getkey
exit

end start