News:

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

Main Menu

Simple test piece for random mutation test.

Started by hutch--, August 06, 2015, 09:26:49 AM

Previous topic - Next topic

hutch--

This was my inspiration while reading the news this morning.  :biggrin:

#IF 0  ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    This test piece is a very simple version of random mutation. It starts with a reversed string
    of numbers and randomly replaces any of the 5 numbers one at a time then tests it against a test
    string of a predetermined order. It will run up to a million iterations but will exit on a
    match displaying the iteration or will show "No match found" if there is no match.

#ENDIF ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION PBmain as LONG

    LOCAL cnt as DWORD
    LOCAL rvl as DWORD

    st1$ = "54321"                              ' the start string
    tst$ = "12345"                              ' the test string

    cnt = 0                                     ' zero the counter

    randomize                                   ' get a random seed from the timer

    Do
      rvl = rnd(0, 5)                           ' get a random location between 0 and 5
      char$ = format$(rnd(1, 5))                ' get a random integer string between 1 and 5
      mid$(st1$, rvl, 1) = char$                ' replace character with "char$" variable
      StdOut st1$+" - iteration "+format$(cnt)  ' needed to provide delay for RND generator

    ' ---------------------
    ' test for string match
    ' ---------------------
      If st1$ = tst$ Then
        StdOut "match found on iteration "+format$(cnt)
        ! jmp outlbl
      End if

    ! add cnt, 1                                ' increment counter
    Loop while cnt < 1000000

    StdOut "No match found"                     ' if no match found, display result

  outlbl:

    waitkey$                                    ' pause so you can see the result

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

Gunther

Hi Steve,

the code is very clear. But which news was it which inspired you?

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

hutch--

Here is a later version, the console output is a lot faster which has allowed 6 numbers over 5 which has a much larger set of combinations.


#IF 0  ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    This test piece is a very simple version of random mutation. It starts with a reversed string
    of numbers and randomly replaces any of the 6 numbers one at a time then tests it against a test
    string of a predetermined order. It will run up to a million iterations but will exit on a
    match displaying the iteration or will show "No match found" if there is no match.

#ENDIF ' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    #include "\basic\include\win32api.inc"

FUNCTION PBmain as LONG

    LOCAL cnt as DWORD
    LOCAL rvl as DWORD

    st1$ = "654321"                             ' the start string
    tst$ = "123456"                             ' the test string

    cnt = 0                                     ' zero the counter

    conout "Emulate random mutation over 6 numbers, from 654321 to 123456"

    randomize                                   ' get a random seed from the timer

    Do
      rvl = rnd(0, 6)                           ' get a random location between 0 and 6
      char$ = format$(rnd(1, 6))                ' get a random integer string between 1 and 6
      mid$(st1$, rvl, 1) = char$                ' replace character with "char$" variable

    ' -----------------------------------------------------------------
    ' display current string + iteration count at fixed screen location
    ' -----------------------------------------------------------------
      printat st1$+" - iteration "+format$(cnt),0,1  ' needed to provide delay for RND generator

    ' ---------------------
    ' test for string match
    ' ---------------------
      If st1$ = tst$ Then
        conout "match found on iteration "+format$(cnt)
        ! jmp outlbl
      End if

    ! add cnt, 1                                ' increment counter
    Loop while cnt < 1000000

    conout "No match found"                     ' if no match found, display result

  outlbl:

    conout "Press any key to close ...."
    waitkey$                                    ' pause so you can see the result

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION printat(text$,ByVal x as DWORD,ByVal y as DWORD) as DWORD

    #REGISTER NONE

    LOCAL hOutPut  as DWORD
    LOCAL xyVar    as DWORD
    LOCAL var      as DWORD

    GetStdHandle %STD_OUTPUT_HANDLE

  PREFIX "!"
    mov hOutPut, eax
  ; -----------------------------------
  ; make both co-ordinates into a DWORD
  ; -----------------------------------
    mov  ecx, x
    mov  eax, y
    shl  eax, 16
    mov  ax, cx
    mov var, eax
  END PREFIX

    SetConsoleCursorPosition hOutPut,var

    conout text$+"        "

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

DECLARE FUNCTION CO_GetStdHandle LIB "KERNEL32.DLL" ALIAS "GetStdHandle" (BYVAL nStdHandle AS DWORD) AS DWORD
DECLARE FUNCTION CO_WriteFile LIB "KERNEL32.DLL" ALIAS "WriteFile" (BYVAL hFile AS DWORD, _
                 ByVal lpBuffer AS DWORD, BYVAL nNumberOfBytesToWrite AS DWORD, _
                 ByVal lpNumberOfBytesWritten AS DWORD, ByVal lpOverlapped AS DWORD) AS LONG
                 
MACRO CO_STD_OUTPUT_HANDLE = -11&

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION conout(txt$,OPT ByVal arg as DWORD) as DWORD

  ' -------------------------------------------
  ' conout basic$       = default appends CRLF
  ' conout basic$, 0    = default appends CRLF
  ' conout basic$, 1    = non zero omits CRLF
  ' bytes_written = conout(basic$,[opt arg])
  ' -------------------------------------------

    #REGISTER NONE

    LOCAL hOutPut  as DWORD
    LOCAL bWritten as DWORD
    LOCAL slen     as DWORD
    LOCAL ptxt     as DWORD

    If arg = 0 Then
      txt$ = txt$ + $CRLF
    End if

    ptxt = StrPtr(txt$)

    PREFIX "! "
    mov eax, ptxt           ; load address into EAX
    mov eax, [eax-4]        ; get length store 4 bytes below
    mov slen, eax           ; write it to a variable
    END PREFIX

    hOutPut = CO_GetStdHandle(CO_STD_OUTPUT_HANDLE)
    CO_WriteFile hOutPut,ptxt,slen,VarPtr(bWritten),0

    FUNCTION = bWritten

END FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤