Re: example in nasm

From: Ro (inp_out_at_sim.tim)
Date: 11/06/04


Date: Sat, 06 Nov 2004 10:52:02 GMT

On 05 Nov 2004 22:37:27 GMT, Betov <betov@free.fr> wrote:
>Ro <inp_out@sim.tim> écrivait news:01ono0h8odcjj0admt5g62hdobrn6jg51m@
>4ax.com:

>But if you want to make sure, for some reason, you can do
>some "or ecx 1", if you like.

ok thanks
>
>> Than the random functions seems to have lines in the some drawing
>
>:)
>
>Well, do the same tests with other Algos, and compare
>the results. This one is the best i ever found, and
>i have still improved it a little bit. But, if you
>get something even better (that does not take far too
>long, for each operation...), i will gladly change.
>
>:)

To Ng: excuse me for this long message but what about RND_Multilinear?
--------------------------------------------------------
// #include "resource.h"
#define IDR_MENU 2000
#define IDM_RANDOM 2001
#define IDM_MAX_RANDOM 2002
#define IDM_RANGE_RANDOM 2003
#define IDM_GAUSS 2004
#define IDM_LINEARE 2007
#define IDM_LINEARE1 2008

#define IDM_EXIT 2005
#define IDM_HELP_ABOUT 2006

#define IDI_ICON 100
IDI_ICON ICON DISCARDABLE "icon1.ico"

#define VERINF1 1
VERINF1 VERSIONINFO
FILEVERSION 1,0,2,8
PRODUCTVERSION 1,0,2,8
FILEOS 0x00000004
FILETYPE 0x00000001
BEGIN
  BLOCK "StringFileInfo"
  BEGIN
    BLOCK "041004E4"
    BEGIN
      VALUE "FileVersion", "0.0.0.1\0"
      VALUE "FileDescription", "Prova random text\0"
      VALUE "InternalName", "rnddemo\0"
      VALUE "LegalCopyright", "KetilO Betov Ro e altri (C) 2004\0"
      VALUE "OriginalFilename", "rnddemo.exe\0"
      VALUE "ProductName", "Prova random text\0"
      VALUE "ProductVersion", "0.0.0.1\0"
    END
  END
  BLOCK "VarFileInfo"
  BEGIN // langid=0x0409, charatsetid=0x04E4
    VALUE "Translation", 0x0410, 0x04E4
  END
END

IDR_MENU MENU DISCARDABLE
BEGIN
    POPUP "&Menu"
    BEGIN
        MENUITEM "&Random", IDM_RANDOM
        MENUITEM "GausMl", IDM_MAX_RANDOM
        MENUITEM "RANGE_RANDOM", IDM_RANGE_RANDOM
        MENUITEM "&Gauss", IDM_GAUSS
        MENUITEM "&Lineare", IDM_LINEARE
        MENUITEM "&MultiLineare", IDM_LINEARE1
        MENUITEM SEPARATOR
        MENUITEM "E&xit ALT+F4", IDM_EXIT
    END
    POPUP "&Aiuto"
    BEGIN
        MENUITEM "&Informazione F1", IDM_HELP_ABOUT
    END
END

-----------------------------------------------------
; con a6
; nasm -f obj prog.asm
; alink -oPE prog.obj dlg.res
section .text use32
section .data use32
section .const use32
section .text
section .data
    
%macro str 2+
    [section .data]
    align 4 , db 0
    %1 db %2
    dd 0
    __SECT__
%endmacro
    
%define NL 13, 10
%define STDIN -10
%define STDOUT -11
%define IDI_ICON 100
%define IDR_MENU 2000
%define IDM_RANDOM 2001
%define IDM_MAX_RANDOM 2002
%define IDM_RANGE_RANDOM 2003
%define IDM_GAUSS 2004
%define IDM_LINEARE 2007
%define IDM_LINEARE1 2008
    
%define IDM_EXIT 2005
%define IDM_HELP_ABOUT 2006
%define IDC_ARROW 07f00h
    IDCANCEL equ 2h
    
    extern ReadFile, WriteFile, GetStdHandle, ExitProcess,
GetModuleHandleA
    import ReadFile kernel32.dll
    import WriteFile kernel32.dll
    import GetStdHandle kernel32.dll
    import ExitProcess kernel32.dll
    import GetModuleHandleA kernel32.dll
    extern GetTickCount
    import GetTickCount kernel32.dll
    extern DialogBoxParamA, EndDialog, MessageBoxA, SetDlgItemTextA
    import DialogBoxParamA user32.dll
    import EndDialog user32.dll
    import MessageBoxA user32.dll
    import SetDlgItemTextA user32.dll
    extern LoadIconA, LoadCursorA, RegisterClassA, LoadMenuA,
ShowWindow
    import LoadIconA user32.dll
    import LoadCursorA user32.dll
    import RegisterClassA user32.dll
    import LoadMenuA user32.dll
    import ShowWindow user32.dll
    extern CreateWindowExA, DestroyWindow, PostQuitMessage,
UpdateWindow
    import CreateWindowExA user32.dll
    import DestroyWindow user32.dll
    import PostQuitMessage user32.dll
    import UpdateWindow user32.dll
    extern TranslateMessage, DispatchMessageA, GetMessageA
    import TranslateMessage user32.dll
    import DispatchMessageA user32.dll
    import GetMessageA user32.dll
    extern DefWindowProcA, ReleaseDC
    import DefWindowProcA user32.dll
    import ReleaseDC user32.dll
    
    extern SendMessageA, GetDlgItemTextA, LoadBitmapA, BeginPaint
    import SendMessageA user32.dll
    import GetDlgItemTextA user32.dll
    import LoadBitmapA user32.dll
    import BeginPaint user32.dll
    extern GetClientRect, EndPaint, GetDC, FillRect, RedrawWindow
    import GetClientRect user32.dll
    import EndPaint user32.dll
    import GetDC user32.dll
    import FillRect user32.dll
    import RedrawWindow user32.dll
    extern CreateCompatibleDC, SelectObject, BitBlt, DeleteDC,
SetPixel
    import CreateCompatibleDC gdi32.dll
    import SelectObject gdi32.dll
    import BitBlt gdi32.dll
    import DeleteDC gdi32.dll
    import SetPixel gdi32.dll
    
    
    WM_INITDIALOG equ 0110h
    WM_COMMAND equ 0111h
    WM_PAINT equ 0fh
    WM_DESTROY equ 2h
    WM_CLOSE equ 10h
    WM_NCPAINT equ 085h
    WM_ERASEBKGND equ 14h
    WM_CTLCOLOREDIT equ 133h
    WM_MOUSEMOVE equ 200h
    WM_NCMOUSEMOVE equ 0A0h
    
    MB_OK equ 0
    MB_SYSTEMMODAL equ 01000h
    EM_SETSEL equ 0b1h
    SRCCOPY equ 0cc0020h
    SW_SHOW equ 5h
    RDW_INVALIDATE equ 01h
    RDW_INTERNALPAINT equ 02h
    RDW_ERASE equ 04h
    WS_EX_CLIENTEDGE equ 200h
    WS_OVERLAPPEDWINDOW equ 0cf0000h
    WS_VISIBLE equ 10000000h
    FirstMessage dd 0 , 0 , 0 , 0 , 0 , 0 , 0
    
    struc rect1
    Left resd 1
    Top resd 1
    Right resd 1
    Bottom resd 1
    Stw resd 1
    Sth resd 1
    endstruc
    
%macro rect1_fill 7
    [section .data]
    %1 dd %2
    dd %3
    dd %4
    dd %5
    dd %6
    dd %7
    __SECT__
%endm
    
    struc WindowClass
    style resd 1
    lpfnWndProc resd 1
    cbClsExtra resd 1
    cbWndExtra resd 1
    hInstance resd 1
    hIcon resd 1
    hCursor resd 1
    hbrBackground resd 1
    lpszMenuName resd 1
    lpszClassName resd 1
    endstruc
    
%macro window_fill 11
    [section .data]
    %1
    dd %2
    dd %3
    dd %4
    dd %5
    dd %6
    dd %7
    dd %8
    dd %9
    dd %10
    dd %11
    __SECT__
%endm
    
    window_fill wstr , 3 , MainWindowProc , 0 , 0 , 0 , 0 , 0 , 6 , 0
, WindowClassName
    [section .data]
    WindowClassName db 'Anything' , 0
    WindowCaption db 'Base App' , 0
    __SECT__
    [section .data]
    WindowHandle dd 0 ,0
    MenuHandle dd 0 ,0
    WindowX dd 50 ,0
    WindowY dd 50 ,0
    WindowW dd 500 ,0
    WindowH dd 350 ,0
    __SECT__
    
    
section .text
    
..start:
    pushad
    push 0
    call [GetModuleHandleA]
    mov [wstr+ hInstance], eax
    push IDI_ICON
    push dword[wstr+hInstance]
    call [LoadIconA]
    mov [wstr+ hIcon] , eax
    push IDC_ARROW
    push 0
    call [LoadCursorA]
    mov [wstr+ hCursor] , eax
    push wstr
    call [RegisterClassA]
    push IDR_MENU
    push dword[wstr+hInstance]
    call [LoadMenuA]
    mov [MenuHandle] , eax
    push 0
    push dword[wstr+hInstance]
    push dword[MenuHandle]
    push 0
    push dword[WindowH]
    push dword[WindowW]
    push dword[WindowY]
    push dword[WindowX]
    push WS_OVERLAPPEDWINDOW | WS_VISIBLE
    push WindowCaption
    push WindowClassName
    push WS_EX_CLIENTEDGE
    call [CreateWindowExA]
    mov [WindowHandle], eax ; 12 argomenti
    push SW_SHOW
    push dword[WindowHandle]
    call [ShowWindow]
    push dword[WindowHandle]
    call [UpdateWindow]
    call RND_RandomizeTimer
    jmp short .L1
.While:
    push FirstMessage
    call [TranslateMessage]
    push FirstMessage
    call [DispatchMessageA]
.L1:
push 0
push 0
push 0
push FirstMessage
call [GetMessageA]
    cmp eax, 0
    jne .While
    
    popad
    push 0
    call [ExitProcess]
    
    
    [section .data]
    base dd 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
, 0 , 0 , 0
    seed dd 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
, 0 , 0 , 0
    seed0 dd 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
, 0 , 0 , 0 , 0 , 0
    index dd 0 , 0
    index1 dd 0 , 0
    buffer times 512 db 0
    __SECT__
    
; void MainWindowProc(Adr, msg, wpar, lpar)
; 0 k, 4 Ra, 8 P_Adr, 12 P_msg, 16 P_wpar, 20 P_lpar
MainWindowProc:
    push ebp
    mov ebp, esp
    %define @Adressee [ebp+8]
    %define @Message [ebp+12]
    %define @wParam [ebp+16]
    %define @lParam [ebp+20]
;---------------------------------------
    cmp dword @Message, WM_CLOSE
    jne .Else_If0
    push dword @Adressee
    call [DestroyWindow]
    mov eax, 1
    jmp .fine
    
.Else_If0:
cmp dword @Message, WM_DESTROY
jne .Else_If1
    push 0
    call [PostQuitMessage]
    mov eax, 1
    jmp .fine
    
.Else_If1:
cmp dword @Message, WM_COMMAND
je .cont
    jmp .Else
.errore:
    str .j00 , "Errore"
    str .j01 , "Devi aspettare che si inizializzino tutti i buffer
per i seeds casuali" , NL , \
    "Muovi il mouse sopra questa finestra!"
    push MB_OK | MB_SYSTEMMODAL
    push .j00
    push .j01
    push 0
    call [MessageBoxA]
    mov eax, 1
    jmp .fine
.cont:
    cmp dword @wParam, IDM_EXIT
    jne .Else_If2
    push 0
    push 0
    push WM_CLOSE
    push dword @Adressee
    call [SendMessageA]
    mov eax, 1
    jmp .fine
.Else_If2:
cmp dword @wParam, IDM_RANDOM
jne .Else_If3
    cmp dword[index], 31
    jbe .errore
    call Random
    mov eax, 1
    jmp .fine
.Else_If3:
cmp dword @wParam, IDM_MAX_RANDOM
jne .Else_If4
    call Clear
    push 400000
    push RND_GaussMl
    call Test_random_a
    mov eax, 1
    jmp .fine
.Else_If4:
cmp dword @wParam, IDM_RANGE_RANDOM
jne .Else_If5
    call Clear
    push 400000
    push RND_RangeRandom
    call Test_random_a
    mov eax, 1
    jmp .fine
.Else_If5:
cmp dword @wParam, IDM_GAUSS
jne .Else_If5a
    call Clear
    push 200000
    push RND_Gauss
    call Test_random_a
    mov eax, 1
    jmp .fine
.Else_If5a:
cmp dword @wParam, IDM_LINEARE
jne .Else_If5b
    call Clear
    push 400000
    push RND_RangeLineare
    call Test_random_a
    mov eax, 1
    jmp .fine
.Else_If5b:
cmp dword @wParam, IDM_LINEARE1
jne .Else_If6
    cmp dword[index], 31
    jbe .errore
    call Clear
    push 400000
    push RND_RangeMLineare
    call Test_random_a
    mov eax, 1
    jmp .fine
.Else_If6:
cmp dword @wParam, IDM_HELP_ABOUT
jne .End_Ifn
; ...
    str .InfoTitle , NL , \
    " RND DLL Demo. (Betov)." , NL , NL , \
    " Original Algo from G. Adam Stanislav." , NL , NL , \
    " Demonstrates SpAsm DLLs programming. " , NL , NL , \
    " If you are in need of Random Routines" , NL , \
    " inside your Applications, and you have" , NL , \
    " no particular reason having them DLLed," , NL , \
    " Copy / paste, instead." , NL , NL
    str .titolo , "Informazione"
    push MB_OK | MB_SYSTEMMODAL
    push .titolo
    push .InfoTitle
    push 0
    call [MessageBoxA]
    mov eax, 1
    jmp .fine
    
.End_Ifn:
    mov eax, 0
    jmp .fine
    
.Else:
    
    cmp dword[index], 31
    jbe .v0
    jmp .vai
.v0:
cmp dword @Message, WM_MOUSEMOVE
je .fai
    cmp dword @Message, WM_NCMOUSEMOVE
    je .fai
    jmp .vai
.fai:
push edi
    push dword @lParam
    call RND_RTimer
    mov edi, [index]
    mov [base+4*edi], eax
    inc dword[index]
    cmp dword[index], 32
    je .l12
    jmp .l0
.l12:
    str .tit , "SetRand Base + Seeds "
    str .In , "Completato Premi OK"
    push MB_OK | MB_SYSTEMMODAL
    push .tit
    push .In
    push 0
    call [MessageBoxA]
    mov edi, 0
.l1:
    mov eax, [seed + 4*edi] ; Copia seed in seed0
    mov [seed0+ 4*edi], eax
    inc edi
    cmp edi, 16
    jb .l1
    
    push edx
    push ebx
    push esi
    mov ebx, buffer
    mov edi, 0
.l3: ; Scive la stringa dei seed in buffer
push dword[base+4*edi]
push ebx
call utoa
    add ebx, eax
    mov byte[ebx], '#'
    inc edi
    inc ebx
    mov eax, ebx
    sub eax, buffer
    cmp eax, 500
    jae .e0
    cmp edi, 32
    jb .l3
.e0:
push MB_OK | MB_SYSTEMMODAL
push .tit
push buffer
push 0
call [MessageBoxA]
    pop esi
    pop ebx
    pop edx
    
.l0:
    pop edi
    
.vai:
    push dword @lParam
    push dword @wParam
    push dword @Message
    push dword @Adressee
    call [DefWindowProcA]
    
.fine:
    %undef @Adressee
    %undef @Message
    %undef @wParam
    %undef @lParam
    mov esp, ebp
    pop ebp
    ret 16
    
    
Random:
    str .titolo , "Numero Casuale"
    call RND_Random
    push eax
    push .titolo
    call utoaz
    ret
    
Random1:
    str .titolo , "Numero Casuale"
    call RND_Multilinear
    push eax
    push .titolo
    call utoaz
    ret
    
    
    str DecString , " in decimale"
    
; uso: str titolo, "Numero " ; utoaz(titolo, D*num);
; utoaz( a < char* Title, unsigned n)
; 0 a, 4 Ra, 8 1P_Title, 12 1P_n
utoaz:
    push eax
    %define @Title [esp+8]
    %define @n [esp+12]
;--------------------*/
    mov eax, @n
    push eax
    push DecString
    call utoa
    mov eax, @Title
    push MB_OK | MB_SYSTEMMODAL
    push eax
    push dword DecString
    push 0
    call [MessageBoxA]
    %undef @Title
    %undef @n
    pop eax
    ret 8
    
    
    
; utoa(char* string, unsigned n)
; 0 k, 4 j, 8 i, 12 r, 16 b, 20 Ra, 24 1P_string, 28 1P_n
utoa:
    push ebx
    push edx
    push esi
    push edi
    push ebp
    %define @string [esp+24]
    %define @n [esp+28]
;--------------------*/
    mov esi, @string
    mov ebx, 10
    mov eax, @n
    mov edi, esi
    mov ebp, 0
.utoaz_l:
    xor edx, edx
    div ebx ; a= (r:a)/b r= (r:a)%b
    add dl, '0'
    mov [edi], dl
    inc edi
    inc ebp
    cmp eax, 0
    jne .utoaz_l
    
    mov byte[edi], 0
    dec edi
    jmp short .vai
.utoaz_f:
    mov al, [esi]
    mov ah, [edi]
    mov [edi], al
    mov [esi], ah
    inc esi
    dec edi
.vai:
    cmp esi, edi
    jb .utoaz_f
    
    mov eax, ebp
.utoaz_z:
    %undef @string
    %undef @n
    pop ebp
    pop edi
    pop esi
    pop edx
    pop ebx
    ret 8
    
    
    [section .data]
    hdc dd 0 , 0
    X dd 0 , 0
    Y dd 0 , 0
    Color dd 0 , 0
    __SECT__
    rect1_fill RECT , 0 , 0 , 0 , 0 , 0 , 0
    
    
MaxRandom:
    push RECT
    push dword[WindowHandle]
    call [GetClientRect]
    push dword[WindowHandle]
    call [GetDC]
    mov [hdc], eax
    mov ecx, 100000
.L0:
push ecx
    push dword[RECT+ Right ]
    call RND_MaxRandom
    mov [X] , eax
    push dword[RECT+ Bottom]
    call RND_MaxRandom
    mov [Y] , eax
    push 0FFFFFFh
    call RND_MaxRandom
    mov [Color], eax
    push dword[Color]
    push dword[Y]
    push dword[X]
    push dword[hdc]
    call [SetPixel]
    pop ecx
    loop .L0
    
    push dword[hdc]
    push dword[WindowHandle]
    call [ReleaseDC]
    ret
    
    
; void Test_random(D@func, D@Points)
; 0 k, 4Ra, 8 D@func, 12D@points (k)
Test_random_a:
    push ebp
    mov ebp, esp
    push ecx
    push ebx
    %define @func [ebp+8]
    %define @Points [ebp+12]
    
    push RECT
    push dword[WindowHandle]
    call [GetClientRect]
    push dword[WindowHandle]
    call [GetDC]
    mov [hdc], eax
    
    mov ecx, @Points
.L0:
push ecx
    push dword[RECT+ Right ]
    push 0
    call dword @func
    mov [X] , eax
    push dword[RECT+ Bottom]
    push 0
    call dword @func
    mov [Y] , eax
    push dword 0FFFFFFh
    push 0
    call dword @func
    mov [Color], eax
    push dword[Color]
    push dword[Y]
    push dword[X]
    push dword[hdc]
    call [SetPixel]
    pop ecx
    loop .L0
    
    push dword[hdc]
    push dword[WindowHandle]
    call [ReleaseDC]
    %undef @func
    %undef @Points
    pop ebx
    pop ecx
    mov esp, ebp
    pop ebp
    ret 8
    
    
    
Clear:
    push RECT
    push dword[WindowHandle]
    call [GetClientRect]
    push dword[WindowHandle]
    call [GetDC]
    mov [hdc], eax
    push 0
    push RECT
    push dword[hdc]
    call [FillRect]
    ret
    
    
    [section .data]
    RandomPass dd 0
    Rand dd 062278134h
    Rb dd 0011101b
    __SECT__
    
RND_Random:
    push ecx
    push edx
    mov eax, [Rand]
    mov ecx, [Rb]
    shrd eax, ecx, 1
    adc ch, 0
    shr cl, 1
    mov dl, cl
    shr dl, 2
    and dl, 1
    xor ch, dl
    shl ch, 6
    or cl, ch
    mov [Rb], cl
    
    xor byte[RandomPass], 1
    cmp byte[RandomPass], 1
    jne .Else
    bswap eax
    jmp short .End_If
.Else:
    xor byte[RandomPass+1], 1
    cmp byte[RandomPass+1], 1
    jne .End_If
    ror eax , cl
.End_If:
    mov [Rand], eax
    pop edx
    pop ecx
    ret
    
;_______________________________________________________
    
; 0 r, 4 c, 8 Ra, 12 @Max
RND_MaxRandom:
    push ecx
    push edx
    %define @Max [esp+12]
    mov eax, [Rand]
    mov ecx, [Rb]
    shrd eax, ecx, 1
    adc ch, 0
    shr cl, 1
    mov dl, cl
    shr dl, 2
    and dl, 1
    xor ch, dl
    shl ch, 6
    or cl, ch
    mov [Rb], cl
    
    xor byte[RandomPass], 1
    cmp byte[RandomPass], 1
    jne .Else
    bswap eax
    jmp short .End_If
.Else:
    xor byte[RandomPass+1], 1
    cmp byte[RandomPass+1], 1
    jne .End_If
    ror eax , cl
.End_If:
    mov [Rand], eax
    mov ecx, @Max
    mov edx, 0
    div ecx
    mov eax, edx
    %undef @Max
    pop edx
    pop ecx
    ret 4
    
;_____________________________________________________
    
; 0 r, 4 c, 8 Ra, 12 @Min, 16 @Max
RND_RangeRandom:
    push ecx
    push edx
    %define @Min [esp+12]
    %define @Max [esp+16]
    
    mov eax, [Rand]
    mov ecx, [Rb]
    shrd eax, ecx, 1
    adc ch, 0
    shr cl, 1
    mov dl, cl
    shr dl, 2
    and dl, 1
    xor ch, dl
    shl ch, 6
    or cl, ch
    mov [Rb], cl
    
    xor byte[RandomPass], 1
    cmp byte[RandomPass], 1
    jne .Else
    bswap eax
    jmp short .End_If
.Else:
    xor byte[RandomPass+1], 1
    cmp byte[RandomPass+1], 1
    jne .End_If
    ror eax , cl
.End_If:
    mov [Rand], eax
    mov ecx, @Max
    mov edx, 0
    sub ecx, @Min
    cmp ecx, 0
    jne .nz
    inc ecx
.nz:
div ecx
mov eax, edx
add eax, @Min
    %undef @Min
    %undef @Max
    pop edx
    pop ecx
    ret 8
    
; uint RND_lineare()
; dd, .base=31415821, .seed=123;
RND_lineare:
    push edx
    [section .data]
    .base dd 31415821 , 0
    .seed dd 123 , 0
    __SECT__
;---------------
    mov eax, [.base]
    mul dword[.seed]
    inc eax
    mov [.seed], eax
    pop edx
    ret
    
    
; 0 r, 4 c, 8 Ra, 12 @Min, 16 @Max
RND_RangeLineare:
    push ecx
    push edx
    %define @Min [esp+12]
    %define @Max [esp+16]
    call RND_lineare
    mov ecx, @Max
    sub ecx, @Min
    cmp ecx, 0
    jne .co
    inc ecx
.co:
mov edx, 0
div ecx
    mov eax, edx
    add eax, @Min
    %undef @Min
    %undef @Max
    pop edx
    pop ecx
    ret 8
    
    
; uint RND_MultiL(base, seed, seed0)
; 0 b, 4 r, 8 c, 12 Ra, 16@Base, 20@Seed, 24@Seed0
RND_MultiL:
    push ecx
    push edx
    push ebx
    %define @Base [esp+16]
    %define @Seed [esp+20]
    %define @Seed0 [esp+24]
    [section .data]
    .libero dd 0 , 0
    __SECT__
    mov edx, @Base
    mov ecx, @Seed
    mov ebx, [edx]
    mov eax, [ecx]
    mul ebx
    inc eax
    mov edx, @Seed0
    mov edx, [edx]
    mov [ecx], eax
    cmp eax, edx
    jne .con
    call RND_lineare
    xor eax, ebx
    test eax, 1
    jne .l0
    inc eax
.l0: ; cambia base ma dispari
mov edx, @Base
mov [edx], eax
    call RND_lineare ; cambia seed0
    mov edx, @Seed0
    xor [edx], eax
    mov eax, @Seed ; cambia seed=seed0 e lo ritorna
    mov [eax], edx
    mov eax, edx
    inc dword[.libero]
; D*.libero < 20 !#.jj
; {str .titolo, "Cambio di base " ; utoaz(.titolo, a);}
.jj:
    
.con:
    %undef @Base
    %undef @Seed
    %undef @Seed0
    pop ebx
    pop edx
    pop ecx
    ret 12
    
RND_Multilinear:
    push ebx
    push ecx
    push edx
    mov eax, [index1]
    lea ebx, [base+ 4*eax]
    lea ecx, [seed+ 4*eax]
    lea edx, [seed0+ 4*eax]
    push edx
    push ecx
    push ebx
    call RND_MultiL
    shr eax, 1
    mov bl, ah
    and ebx, 0Fh
    mov [index1], ebx
    bswap eax
    pop edx
    pop ecx
    pop ebx
    ret
    
    
; 0 r, 4 c, 8 Ra, 12 @Min, 16 @Max
RND_RangeMLineare:
    push ecx
    push edx
    %define @Min [esp+12]
    %define @Max [esp+16]
    call RND_Multilinear
    mov ecx, @Max
    sub ecx, @Min
    cmp ecx, 0
    jne .co
    inc ecx
.co:
mov edx, 0
div ecx
    mov eax, edx
    add eax, @Min
    %undef @Min
    %undef @Max
    pop edx
    pop ecx
    ret 8
    
    
;____________________________________________________
    
RND_RandomizeTimer:
    pushad
    call [GetTickCount]
    mov [Rand], al
    shl dword[Rand], 8
    mov [Rand], ah
    shl dword[Rand], 8
    mov [Rand], al
    shl dword[Rand], 8
    mov [Rand], al
    xor dword[Rand], 062278134h
    popad
    ret
    
    
; 0 b, 4 Ra, 8 @MousePos
RND_RTimer:
    push ebx
    %define @MousePos [ebp+8]
    call [GetTickCount]
    shl eax, 16
    mov ebx, @MousePos
    mov ax, bx
    push eax
    call RND_lineare
    pop ebx
    xor eax, ebx ; Dispari allora non funziona
    or ax, 1
    %undef @MousePos
    pop ebx
    ret 4
    
    
    
    
;____________________________________________________
    
; s' 0 k, 4 Ra, 8 @Min, 12 @Max (per k)
; s 0 r, 4 c, 8 b, 12 k, 16 Ra, 20 @Min, 24 @Max
RND_Gauss:
    push ebp
    mov ebp, esp
    push ecx
    push edx
    push ebx
    %define @Min [ebp+8]
    %define @Max [ebp+12]
    push dword @Max
    push dword @Min
    call RND_RangeRandom
    push eax
    push dword @Max
    push dword @Min
    call RND_RangeRandom
    pop ebx
    add eax, ebx
    shr eax, 1
    %undef @Min
    %undef @Max
    pop ebx
    pop edx
    pop ecx
    mov esp, ebp
    pop ebp
    ret 8
    
; s' 0 k, 4 Ra, 8 @Min, 12 @Max (per k)
; s 0 r, 4 c, 8 b, 12 k, 16 Ra, 20 @Min, 24 @Max
RND_GaussMl:
    push ebp
    mov ebp, esp
    push ecx
    push edx
    push ebx
    %define @Min [ebp+8]
    %define @Max [ebp+12]
    push dword @Max
    push dword @Min
    call RND_RangeMLineare
    push eax
    push dword @Max
    push dword @Min
    call RND_RangeMLineare
    pop ebx
    add eax, ebx
    shr eax, 1
    %undef @Min
    %undef @Max
    pop ebx
    pop edx
    pop ecx
    mov esp, ebp
    pop ebp
    ret 8
    

---------------------------------------------------------------
/* con a6
/* nasm -f obj prog.asm
/* alink -oPE prog.obj dlg.res
section .text use32
section .data use32
section .const use32
section .text
section .data

%macro str 2+
     [section .data]
  align 4, db 0
  %1 db %2
           dd 0
      __SECT__
%endmacro

%define NL 13, 10
%define STDIN -10
%define STDOUT -11
%define IDI_ICON 100
%define IDR_MENU 2000
%define IDM_RANDOM 2001
%define IDM_MAX_RANDOM 2002
%define IDM_RANGE_RANDOM 2003
%define IDM_GAUSS 2004
%define IDM_LINEARE 2007
%define IDM_LINEARE1 2008

%define IDM_EXIT 2005
%define IDM_HELP_ABOUT 2006
%define IDC_ARROW 07f00h
IDCANCEL equ 2h ;

== ReadFile, WriteFile, GetStdHandle, ExitProcess, GetModuleHandleA ==
kernel32.dll
== GetTickCount == kernel32.dll
== DialogBoxParamA, EndDialog, MessageBoxA, SetDlgItemTextA ==
user32.dll
== LoadIconA, LoadCursorA, RegisterClassA, LoadMenuA, ShowWindow ==
user32.dll
== CreateWindowExA, DestroyWindow, PostQuitMessage, UpdateWindow ==
user32.dll
== TranslateMessage, DispatchMessageA, GetMessageA ==
user32.dll
== DefWindowProcA, ReleaseDC == user32.dll

== SendMessageA, GetDlgItemTextA, LoadBitmapA, BeginPaint ==
user32.dll
== GetClientRect, EndPaint, GetDC, FillRect, RedrawWindow ==
user32.dll
== CreateCompatibleDC, SelectObject, BitBlt, DeleteDC, SetPixel ==
gdi32.dll

WM_INITDIALOG equ 0110h; WM_COMMAND equ 0111h; WM_PAINT equ
0fh;
WM_DESTROY equ 2h; WM_CLOSE equ 10h; WM_NCPAINT equ
085h ;
WM_ERASEBKGND equ 14h; WM_CTLCOLOREDIT equ 133h;
WM_MOUSEMOVE equ 200h; WM_NCMOUSEMOVE equ 0A0h;

MB_OK equ 0; MB_SYSTEMMODAL equ 01000h;
EM_SETSEL equ 0b1h; SRCCOPY equ 0cc0020h; SW_SHOW equ
5h
RDW_INVALIDATE equ 01h; RDW_INTERNALPAINT equ 02h; RDW_ERASE equ
04h
WS_EX_CLIENTEDGE equ 200h; WS_OVERLAPPEDWINDOW equ 0cf0000h;
WS_VISIBLE equ 10000000h
FirstMessage dd 0, 0, 0, 0, 0, 0, 0

struc rect1
 Left resd 1 | Top resd 1 | Right resd 1
 Bottom resd 1 | Stw resd 1 | Sth resd 1
endstruc

%macro rect1_fill 7
 [section .data]
  %1 dd %2|dd %3|dd %4|dd %5|dd %6|dd %7
  __SECT__
%endm

struc WindowClass
  style resd 1 | lpfnWndProc resd 1 | cbClsExtra resd 1 |
cbWndExtra resd 1
  hInstance resd 1| hIcon resd 1 | hCursor resd 1 | hbrBackground
resd 1
  lpszMenuName resd 1| lpszClassName resd 1
endstruc

%macro window_fill 11
 [section .data]
  %1 | dd %2|dd %3|dd %4|dd %5|dd %6|dd %7|dd %8|dd %9|dd %10|dd %11
   __SECT__
%endm

window_fill wstr, 3, MainWindowProc, 0, 0, 0, 0, 0, 6, 0,
WindowClassName
db, WindowClassName='Anything', WindowCaption='Base App';
dd, WindowHandle=0, MenuHandle=0, WindowX=50, WindowY=50,
WindowW=500, WindowH=350;

section .text

..start:
{pushad
    [wstr+ hInstance] = *GetModuleHandleA(0);
    [wstr+ hIcon] = *LoadIconA( D[wstr+hInstance] , IDI_ICON);
    [wstr+ hCursor] = *LoadCursorA( 0, IDC_ARROW);
    *RegisterClassA(wstr);
    *MenuHandle = *LoadMenuA( D[wstr+hInstance], IDR_MENU );
 *WindowHandle = *CreateWindowExA( WS_EX_CLIENTEDGE, WindowClassName,
            WindowCaption, _ WS_OVERLAPPEDWINDOW || WS_VISIBLE _ ,
                    D*WindowX, D*WindowY, D*WindowW, D*WindowH,
                           0, D*MenuHandle, D[wstr+hInstance], 0);
/* 12 argomenti
    *ShowWindow( D*WindowHandle, SW_SHOW);
    *UpdateWindow( D*WindowHandle );
    RND_RandomizeTimer(); #.L1
    .While:
        {*TranslateMessage(FirstMessage);
         *DispatchMessageA(FirstMessage);
    .L1: *GetMessageA(FirstMessage, 0, 0, 0);
         a#.While
        }
 popad
   *ExitProcess(0);
}

[section .data]
base dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
seed dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
seed0 dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
index dd 0, 0
index1 dd 0, 0
buffer times 512 db 0
__SECT__

/* void MainWindowProc(Adr, msg, wpar, lpar)
/* 0 k, 4 Ra, 8 P_Adr, 12 P_msg, 16 P_wpar, 20 P_lpar
MainWindowProc:
{< k
   k = s;
   << @Adressee=[k+8], @Message=[k+12], @wParam=[k+16], @lParam=[k+20]
 /*---------------------------------------
       D @Message == WM_CLOSE !#.Else_If0
                    {*DestroyWindow( D @Adressee ); a=1; ##.fine; }
      .Else_If0: D @Message == WM_DESTROY !#.Else_If1
                    {*PostQuitMessage(0); a=1; ##.fine; }
      .Else_If1: D @Message == WM_COMMAND #.cont
                    ##.Else
          .errore:
           str .j00, "Errore";
           str .j01, "Devi aspettare che si inizializzino tutti i
buffer per i seeds casuali", NL,\
                       "Muovi il mouse sopra questa finestra!"
        *MessageBoxA( 0, .j01, .j00, _ MB_OK || MB_SYSTEMMODAL _ );
a=1; ##.fine
          .cont:
          { D @wParam == IDM_EXIT !#.Else_If2
            {*SendMessageA( D @Adressee, WM_CLOSE, 0, 0); a=1;
##.fine}
          .Else_If2: D @wParam == IDM_RANDOM !#.Else_If3
                {D*index<=31 #.errore; Random(); a=1; ##.fine
}
          .Else_If3: D @wParam == IDM_MAX_RANDOM !#.Else_If4
                {Clear(); Test_random_a(RND_GaussMl, 400000);
a=1; ##.fine }
          .Else_If4: D @wParam == IDM_RANGE_RANDOM !#.Else_If5
                {Clear(); Test_random_a(RND_RangeRandom, 400000);
a=1; ##.fine }
          .Else_If5: D @wParam == IDM_GAUSS !#.Else_If5a
                {Clear(); Test_random_a(RND_Gauss, 200000);
a=1; ##.fine }
          .Else_If5a: D @wParam == IDM_LINEARE !#.Else_If5b
                {Clear(); Test_random_a(RND_RangeLineare, 400000);
a=1; ##.fine }
          .Else_If5b: D @wParam == IDM_LINEARE1 !#.Else_If6
                {D*index<=31 #.errore;
                 Clear(); Test_random_a(RND_RangeMLineare, 400000);
a=1; ##.fine }
          .Else_If6: D @wParam == IDM_HELP_ABOUT !#.End_Ifn
               /* ...
              { str .InfoTitle, NL,\
                       " RND DLL Demo. (Betov).", NL,NL,\
                       " Original Algo from G. Adam Stanislav.",
NL,NL,\
                       " Demonstrates SpAsm DLLs programming. ",
NL,NL,\
                       " If you are in need of Random Routines",
NL,\
                       " inside your Applications, and you have",
NL,\
                       " no particular reason having them DLLed,",
NL,\
                       " Copy / paste, instead.", NL,NL
               str .titolo, "Informazione"
              *MessageBoxA( 0, .InfoTitle, .titolo, _ MB_OK ||
MB_SYSTEMMODAL _ );
               a=1; ##.fine;
              }
          .End_Ifn:
           a=0; ##.fine;
          }
      .Else:
          {
           D*index<=31 #.v0| ##.vai
        .v0: D @Message == WM_MOUSEMOVE #.fai
                  D @Message == WM_NCMOUSEMOVE #.fai; ##.vai
                   .fai: {< j
                             RND_RTimer( D @lParam );
                             j= *index; [base+4*j]=a; ++D*index;
                             D*index==32 #.l12 | ##.l0
                                 .l12:
                                  {str .tit, "SetRand Base + Seeds
"; str .In, "Completato Premi OK"
                                   *MessageBoxA( 0, .In, .tit, _ MB_OK
|| MB_SYSTEMMODAL _ );
                                   j=0;
                                  .l1:
                                      {a=[seed + 4*j] /* Copia seed
in seed0
                                       [seed0+ 4*j]=a
                                       ++j | j<16#.l1
                                      }
                                   < r, b, i
                                   b=buffer; j=0;
                                  .l3: utoa(b, D[base+4*j]); /*
Scive la stringa dei seed in buffer
                                         b+=a; B*b='#'; ++j; ++b;
                                         a = b; a-=buffer; a>=500#.e0
                                         j<32#.l3
                                  .e0: *MessageBoxA( 0, buffer, .tit,
_ MB_OK || MB_SYSTEMMODAL _ );
> r, b, i
                                  }
                           .l0:
> j
                         }
          .vai:
           *DefWindowProcA( D @Adressee, D @Message, D @wParam, D
@lParam);
          }
 .fine:
>> @Adressee, @Message, @wParam, @lParam
  s = k
> k;
 ret 16
}

Random:
    str .titolo, "Numero Casuale" ;
    RND_Random();
    utoaz( .titolo, a );
ret

Random1:
    str .titolo, "Numero Casuale" ;
    RND_Multilinear();
    utoaz( .titolo, a );
ret

str DecString, " in decimale"

/* uso: str titolo, "Numero " ; utoaz(titolo, D*num);
/* utoaz( a < char* Title, unsigned n)
/* 0 a, 4 Ra, 8 1P_Title, 12 1P_n
utoaz:
{< a
 << @Title=[s+8], @n=[s+12]
/*--------------------*/
 a=@n; utoa(DecString, a);
 a=@Title;
 *MessageBoxA( 0, D DecString, a, _ MB_OK || MB_SYSTEMMODAL _ );
>> @Title, @n
> a
 ret 8
}

/* utoa(char* string, unsigned n)
/* 0 k, 4 j, 8 i, 12 r, 16 b, 20 Ra, 24 1P_string, 28 1P_n
utoa:
{< b, r, i, j, k
 << @string=[s+24], @n=[s+28]
/*--------------------*/
 i=@string; b=10; a=@n; j=i; k=0;
 .utoaz_l:
        {r ^= r
         div b /* a= (r:a)/b r= (r:a)%b
         rl+='0'; *j=rl; ++j; ++k
         a#.utoaz_l
        }
  B*j=0; --j | #.vai
  .utoaz_f:
      {al=*i; ah=*j;
       *j=al; *i=ah;
        ++i | --j;
      .vai:
       i<j #.utoaz_f
      }
 a=k;
 .utoaz_z:
>> @string, @n
> b, r, i, j, k
 ret 8
}

dd, hdc=0, X=0, Y=0, Color=0
rect1_fill RECT, 0,0,0,0,0,0
 

MaxRandom:
 *GetClientRect( D*WindowHandle, RECT );
 *hdc = *GetDC( D*WindowHandle );
  c = 100000
.L0: { < c
        *X = RND_MaxRandom(D[RECT+ Right ]);
        *Y = RND_MaxRandom(D[RECT+ Bottom]);
        *Color = RND_MaxRandom(0FFFFFFh);
        *SetPixel( D*hdc, D*X, D*Y, D*Color);
> c
       loop .L0
     }
 *ReleaseDC(D*WindowHandle, D*hdc);
ret

/* void Test_random(D@func, D@Points)
/* 0 k, 4Ra, 8 D@func, 12D@points (k)
Test_random_a:
{< k
   k = s;
   < c, b
   << @func=[k+8], @Points=[k+12]

     *GetClientRect( D*WindowHandle, RECT);
     *hdc = *GetDC( D*WindowHandle);
 
     c=@Points
 .L0: { < c
         *X = D @func( 0, D[RECT+ Right ]);
         *Y = D @func( 0, D[RECT+ Bottom]);
         *Color = D @func( 0, D 0FFFFFFh);
         *SetPixel( D*hdc, D*X, D*Y, D*Color);
> c
        loop .L0
     }
    *ReleaseDC(D*WindowHandle, D*hdc);
>> @func, @Points
> c, b
   s = k
> k;
 ret 8
}

Clear:
 *GetClientRect(D*WindowHandle, RECT );
 *hdc = *GetDC( D*WindowHandle );
 *FillRect( D*hdc, RECT, 0 );
ret

[section .data]
RandomPass dd 0; Rand dd 062278134h; Rb dd 0011101b
__SECT__

RND_Random:
< c, r
  a=*Rand; c=*Rb;
  (a,c)>>=1; ch++=0; cl>>= 1;
  dl =cl; dl>>=2; dl&= 1;
  ch^=dl; ch<<=6; cl|=ch;
  *Rb=cl

  B*RandomPass ^= 1
  B*RandomPass == 1 !#.Else
         {bswap a; #.End_If }
 .Else:
     B[RandomPass+1]^= 1
     B[RandomPass+1]== 1 !#.End_If
                ror eax, cl
 .End_If:
  *Rand=a
> c, r
ret

/_______________________________________________________

/* 0 r, 4 c, 8 Ra, 12 @Max
RND_MaxRandom:
< c, r
<< @Max=[s+12]
  a=*Rand; c=*Rb;
  (a,c)>>=1; ch++=0; cl>>= 1;
  dl =cl; dl>>=2; dl&= 1;
  ch^=dl; ch<<=6; cl|=ch;
  *Rb=cl;

  B*RandomPass ^= 1;
  B*RandomPass == 1 !#.Else
         {bswap a; #.End_If }
 .Else:
     B[RandomPass+1]^=1
     B[RandomPass+1]== 1 !#.End_If
                ror eax, cl
 .End_If:
  *Rand=a; c=@Max; r=0 | div c | a=r
>> @Max
> c, r
ret 4

/_____________________________________________________

/* 0 r, 4 c, 8 Ra, 12 @Min, 16 @Max
RND_RangeRandom:
< c, r
<< @Min=[s+12], @Max=[s+16]

  a=*Rand; c=*Rb;
  (a,c)>>=1; ch++=0; cl>>= 1;
  dl =cl; dl>>=2; dl&= 1;
  ch^=dl; ch<<=6; cl|=ch;
  *Rb=cl;
  
  B*RandomPass ^= 1;
  B*RandomPass == 1 !#.Else
         {bswap a; #.End_If }
 .Else:
     B[RandomPass+1]^=1
     B[RandomPass+1]== 1 !#.End_If
                ror eax, cl
 .End_If:
  *Rand=a; c=@Max; r=0; c-=@Min;
   c#.nz | ++c;
.nz: div c; a=r; a+=@Min;
>> @Min, @Max
> c, r
ret 8

/* uint RND_lineare()
/* dd, .base=31415821, .seed=123;
RND_lineare:
< r
  dd, .base=31415821, .seed=123;
/---------------
  a = *.base;
  mul D*.seed
  ++a;
  *.seed=a;
> r
ret

/* 0 r, 4 c, 8 Ra, 12 @Min, 16 @Max
RND_RangeLineare:
< c, r
<< @Min=[s+12], @Max=[s+16]
     RND_lineare();
     c=@Max; c-=@Min;
     c#.co|++c;
.co: r=0; div c
     a=r; a+=@Min;
>> @Min, @Max
> c, r
ret 8

/* uint RND_MultiL(base, seed, seed0)
/* 0 b, 4 r, 8 c, 12 Ra, 16@Base, 20@Seed, 24@Seed0
RND_MultiL:
< c, r, b
<< @Base=[s+16], @Seed=[s+20], @Seed0=[s+24]
dd, .libero=0;
  r=@Base; c=@Seed;
  b=*r; a=*c; mul b; ++a;
  r=@Seed0; r=*r; *c=a;
  a == r !#.con
    {RND_lineare(); a^=b;
     a&1 | !=#.l0 | ++a;
     .l0: r=@Base; *r=a; /* cambia base ma dispari
     RND_lineare(); r=@Seed0; *r^=a; /* cambia seed0
                    a=@Seed; *a =r; a=r; /* cambia seed=seed0 e lo
ritorna
     ++D*.libero;
    /* D*.libero < 20 !#.jj
      /* {str .titolo, "Cambio di base " ; utoaz(.titolo, a);}
    .jj:
    }
.con:
>> @Base, @Seed, @Seed0
> c, r, b
ret 12

RND_Multilinear:
< b, c, r
 a=*index1
 b= &[base+ 4*a]; c= &[seed+ 4*a]; r= &[seed0+ 4*a];
 RND_MultiL(b, c, r);
 a>>=1; bl=ah; b&=0Fh; *index1=b;
 bswap a;
> b, c, r
ret

/* 0 r, 4 c, 8 Ra, 12 @Min, 16 @Max
RND_RangeMLineare:
< c, r
<< @Min=[s+12], @Max=[s+16]
     RND_Multilinear();
     c=@Max; c-=@Min;
     c#.co|++c;
.co: r=0; div c
     a=r; a+=@Min;
>> @Min, @Max
> c, r
ret 8

/____________________________________________________

RND_RandomizeTimer:
    pushad
        *GetTickCount()
        *Rand=al; D*Rand <<= 8
        *Rand=ah; D*Rand <<= 8
        *Rand=al; D*Rand <<= 8
        *Rand=al; D*Rand ^= 062278134h
    popad
ret

/* 0 b, 4 Ra, 8 @MousePos
RND_RTimer:
{< b
 << @MousePos=[k+8]
    *GetTickCount();
    a<<=16;
    b=@MousePos; ax=bx;
    < a | RND_lineare() | > b
    a^=b; ax|=1; /* Dispari allora non funziona
>> @MousePos
> b
 ret 4
}

/____________________________________________________

/* s' 0 k, 4 Ra, 8 @Min, 12 @Max (per k)
/* s 0 r, 4 c, 8 b, 12 k, 16 Ra, 20 @Min, 24 @Max
RND_Gauss:
< k
k = s;
< c, r, b
<< @Min=[k+8], @Max=[k+12]
     RND_RangeRandom( D @Min, D @Max );
     <a|RND_RangeRandom( D @Min, D @Max)|>b;
     a+=b; a>>=1;
>> @Min, @Max
> c, r, b
s = k
> k
ret 8

/* s' 0 k, 4 Ra, 8 @Min, 12 @Max (per k)
/* s 0 r, 4 c, 8 b, 12 k, 16 Ra, 20 @Min, 24 @Max
RND_GaussMl:
< k
k = s;
< c, r, b
<< @Min=[k+8], @Max=[k+12]
     RND_RangeMLineare( D @Min, D @Max );
     <a|RND_RangeMLineare( D @Min, D @Max)|>b;
     a+=b; a>>=1;
>> @Min, @Max
> c, r, b
s = k
> k
ret 8



Relevant Pages

  • ascii to st0
    ... push IDC_ARROW ... cmp eax, 0 ... mov ebp, esp ... cmp dword @Message, WM_CLOSE ...
    (alt.lang.asm)
  • Re: Fastcode Library Design
    ... cmp ecx, SMALLMOVESIZE ... lea edx, ... fild qword ptr [eax] ... mov ecx, ...
    (borland.public.delphi.language.basm)
  • Re: Interesting Web Site on Open Source Development
    ... mov D§esp 0DEADBEEF call Code04013A0 ... call Code0401100 push eax lea eax D§esp+014 push Data0402124 ... push eax call 'USER32.wsprintfA' add esp 0C push 030 lea ecx D§esp+014 push ecx ...
    (alt.lang.asm)
  • a problem in n5.dll in windows 95
    ... eax srandMl ... mov ebp, esp ... push dword @lpm ... mov esi, @adw ...
    (alt.lang.asm)
  • Re: recvfrom
    ... push ebx ... mov eax, ... cmp eax, SOCKET_ERROR ...
    (alt.lang.asm)