2012年7月5日星期四

Lorenz.asm


; "Lorenz's Attractor", an example of Chaos

; Solves the differential equations:

; dx/dt = 10(y - x)
; dy/dt = -xz + 28x - y
; dz/dt = xy - (8/3)z

; These equations are integrated numerically by the finite
; increment method. i.e

; x(n)=x(n-1)+dt*x'(n-1); y(n)=y(n-1)+dt*y'(n-1); z(n)=z(n-1)+dt*z'(n-1)

; where x',y' and z' are the x,y & z derivatives and dt is a small time
; increment.

; x,y & z are the 3 orthogonal dimensions. t is time

;   Ron Thomas Ron_Thom@Compuserve.com 15/5/99 
;---------------------------------------------
;The Lorenz Attractor

;This is graphical demo which reproduces the clasic "Lorenz Attractor". It solves three 
;differential equations to compute the orbit created by the chaotic attractor.

;Try resizing the window and you will see a completely different orbit each time ! 

;Enjoy

;Ron Thomas

;Ron_Thom@Compuserve.com

;www.rbthomas.freeserve.co.uk

;15/5/99
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;以上是原作者的说明,
;这是我从国外(俄国)的一个网站下载的。
;本文是关于混沌学的。
;第一次运行本程序,在下方与右方有的地方会显示不出来,改变窗口的大小后会显示正常。
;这个程序我存放半年多了,特放出来与需要的人共享。
;QQ:112426112
;Email:leguanyuan at 126 dot com
;Homepage:http://correy.webs.com
;以下是我改编的代码:
.386
.model flat,stdcall
option casemap:none

include windows.inc

include kernel32.inc
includelib kernel32.lib

include user32.inc
includelib user32.lib

include gdi32.inc 
includelib gdi32.lib 

.code
hInstance dd 0
hWinMain dd 0
szClassName db "made by correy",0
pwndclassex dd 48,3,offset liuchunli,0,0,0,0,0,6,0,offset szClassName,0
stMsg MSG <>

cxClient DD ?
cyClient DD ?
npoints  DD 10000  ; No of points in orbit
xx  DD ?  ; Coordinates of point to be plotted
yy  DD ?
x_offset DD ?   ; Screen offsets
y_offset DD ?  
scaleX  DD ?  ; Scaling factors
scaleY  DD ?

h real4 0.003   ;Time step
ten_h real4 0.03
x real4 0.6
y real4 0.6
z real4 0.6
x_new real4 ?   ; New orbit coords (F.P values)
y_new real4 ?   ;
z_new real4 ?   ;
twenty8 real4 28.0
fract real4 2.6666666  ;Fraction 8/3

liuchunli proc uses ebx edi esi,hWnd,uMsg,wParam,lParam
LOCAL hdc:HDC, ps:PAINTSTRUCT
.if uMsg == WM_CLOSE;放置后面会反应更快点。
  invoke DestroyWindow,hWinMain
  invoke PostQuitMessage,0
.elseif uMsg == WM_SIZE

  mov eax,lParam
  and eax,0FFFFh

  mov cxClient,eax
  shr eax,1
  mov x_offset,eax  ; Compute offset

  mov ecx,40   ; Compute scaling factor
  mov eax,cxClient
  mov edx,0
  div ecx
  mov scaleX,eax  

  mov ebx,lParam
  shr ebx,16

  mov cyClient,ebx
  shr ebx,1
  mov y_offset,ebx

  mov ecx,50   ; Compute scaling factor    
  mov eax,cyClient
  mov edx,0
  div ecx
  mov scaleY,eax  
.elseif uMsg == WM_PAINT  
  invoke BeginPaint,hWnd, ADDR ps
  mov    hdc,eax

  finit     ;Initialise the coprocessor

  mov esi,0

  .WHILE esi < npoints

    fld y    ;Y
    fsub x   ;Y - X
    fmul ten_h   ;10*H*(Y - X)
    fadd x   ;X + 10*H*(Y - X)
    fstp x_new   ;

    fld  x
    fld  twenty8   ;28,X
    fsub z   ;28-Z,X
    fmul    ;X*(28-Z)
    fsub y   ;X*(28-Z)-Y
    fmul h
    fadd y   ;Y+H*(X(28-Z)-Y)
    fstp y_new

    fld  x
    fmul y   ;X*Y
    fld  fract
    fmul z   ;frac*Z, X*Y
    fsub    ;X*Y - FRACT*Z
    fmul h
    fadd z   ;
    fstp z_new   ;Z+H*(X*Y - FRACT*Z)

    fld x_new
    fst x    ;Update X
    fimul scaleX   
    fiadd x_offset
    fistp xx    ;Convert to integer 

    fld y_new
    fst y    ;Update Y
    fimul scaleY
    fiadd y_offset
    fistp yy   

    fld z_new
    fstp z    ;Update Z

    invoke SetPixel, hdc, xx, yy, 255 ;Plot orbit in red pixels @ coords xx,yy 

    inc  esi

  .ENDW    
  invoke EndPaint,hWnd, ADDR ps
.else
  invoke DefWindowProc,hWnd,uMsg,wParam,lParam
  ret;不能去掉。
.endif
xor eax,eax;这两行可以去掉?
ret
liuchunli endp

start: invoke GetModuleHandle,0
mov hInstance,eax
mov pwndclassex+20,eax

invoke LoadCursor,0,32512;加载箭头鼠标。
mov pwndclassex+28,eax

invoke RegisterClassEx,addr pwndclassex
invoke CreateWindowEx,200h,offset szClassName,offset szClassName,0Cf0000h,80000000h,80000000h,710,530,0,0,hInstance,0;0Ca0000h
mov hWinMain,eax

invoke ShowWindow,hWinMain,1;若不想显示,此行也可以去掉。

again:invoke GetMessage,addr stMsg,0,0,0
  cmp eax,0
  je exit
  invoke DispatchMessage,addr stMsg
jmp again

exit:invoke ExitProcess,0
end start
;made at 2011.09.24 

没有评论:

发表评论