td win32asm 150.asm

20
td_win32asm_150.asm ;============================================================================== ; Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE's 150 ;============================================================================== ;============================================================================== ; ==> Part 150 : Mandelbrot fractal, FPU, Screen buffer ! ;------------------------------------------------------------------------------ ; Hello, why not analyze some complex values ... ; Clicking "Calculate" in the menu bar starts calculating. ; Choosing "Settings" will open a Popup menu, you can select your preferences. ; Press "Esc" to abort calculating. ; I wish you a nice journey through the world of Mr. Mandelbrot. ; In the windowproc WP1, reacting to a WM_CREATE message I get a ; MAIN device context for the client area of the window, creating a ; compatible BITMAP for that (!), creating a compatible (!) device context and ; now selecting an object (the BITMAP) into the compatible device context. ; Now I call API "FillRect" to clear (!?) the created back buffer and at last ; I call API "ReleaseDC" to release the MAIN device context for future use. ; Note: the compatible device context is still present (needed on WM_PAINT) ! ; We delete this only on WM_DESTROY. ; On WM_PAINT I perform a simple Bit transfer (API "BitBlt" ) from the created ; back buffer to update the screen. ; Another interested part of this win32asm example is located on WM_COMMAND. ; Here I calculate the fractal using the FPU, the calculated pixel/color will ; be written to the back buffer and also to the screen, which will increase ; the time to calculate up to 25% (right, that's the drawback here...). ; Thanks to Wayne J. Radburn for the WNDCLASSEX.style CS_OWNDC tip. ; ; Test Department [email protected] ;============================================================================== ; Assembler directives ;------------------------------------------------------------------------------ .386 ; specifies the processor our program want run on .Model Flat ,StdCall ; Flat for Win9x (32 Bit), Calling Convention option casemap:none ; case sensitive ! ;============================================================================== ; Include all files where API functions resist you want use, set correct path ;------------------------------------------------------------------------------ include D:\Masm32\include\windows.inc includelib kernel32.lib includelib user32.lib includelib gdi32.lib ;============================================================================== ; Declaration of used API functions,take a look into WIN32.HLP and *.inc files ;------------------------------------------------------------------------------ GetModuleHandleA PROTO :DWORD LoadIconA PROTO :DWORD,:DWORD CreateSolidBrush PROTO :DWORD LoadCursorA PROTO :DWORD,:DWORD RegisterClassExA PROTO :DWORD Page 1

Upload: z4rm4r

Post on 15-Dec-2015

2 views

Category:

Documents


0 download

DESCRIPTION

Test department asembler

TRANSCRIPT

Page 1: Td Win32asm 150.Asm

td_win32asm_150.asm;==============================================================================; Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE's 150;==============================================================================

;==============================================================================; ==> Part 150 : Mandelbrot fractal, FPU, Screen buffer !;------------------------------------------------------------------------------; Hello, why not analyze some complex values ...; Clicking "Calculate" in the menu bar starts calculating.; Choosing "Settings" will open a Popup menu, you can select your preferences.; Press "Esc" to abort calculating.; I wish you a nice journey through the world of Mr. Mandelbrot. ; In the windowproc WP1, reacting to a WM_CREATE message I get a; MAIN device context for the client area of the window, creating a; compatible BITMAP for that (!), creating a compatible (!) device context and; now selecting an object (the BITMAP) into the compatible device context.; Now I call API "FillRect" to clear (!?) the created back buffer and at last; I call API "ReleaseDC" to release the MAIN device context for future use.; Note: the compatible device context is still present (needed on WM_PAINT) !; We delete this only on WM_DESTROY.; On WM_PAINT I perform a simple Bit transfer (API "BitBlt" ) from the created; back buffer to update the screen.; Another interested part of this win32asm example is located on WM_COMMAND.; Here I calculate the fractal using the FPU, the calculated pixel/color will; be written to the back buffer and also to the screen, which will increase; the time to calculate up to 25% (right, that's the drawback here...).; Thanks to Wayne J. Radburn for the WNDCLASSEX.style CS_OWNDC tip.; ; Test Department [email protected]

;==============================================================================; Assembler directives;------------------------------------------------------------------------------.386 ; specifies the processor our program want run on.Model Flat ,StdCall ; Flat for Win9x (32 Bit), Calling Conventionoption casemap:none ; case sensitive !

;==============================================================================; Include all files where API functions resist you want use, set correct path;------------------------------------------------------------------------------include D:\Masm32\include\windows.incincludelib kernel32.libincludelib user32.libincludelib gdi32.lib

;==============================================================================; Declaration of used API functions,take a look into WIN32.HLP and *.inc files;------------------------------------------------------------------------------GetModuleHandleA PROTO :DWORDLoadIconA PROTO :DWORD,:DWORDCreateSolidBrush PROTO :DWORDLoadCursorA PROTO :DWORD,:DWORDRegisterClassExA PROTO :DWORD

Page 1

Page 2: Td Win32asm 150.Asm

td_win32asm_150.asmCreateWindowExA PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD, :DWORD,:DWORD,:DWORD,:DWORD,:DWORDShowWindow PROTO :DWORD,:DWORDUpdateWindow PROTO :DWORDGetMessageA PROTO :DWORD,:DWORD,:DWORD,:DWORDTranslateMessage PROTO :DWORDDispatchMessageA PROTO :DWORDPostQuitMessage PROTO :DWORDDefWindowProcA PROTO :DWORD,:DWORD,:DWORD,:DWORDExitProcess PROTO :DWORDGetMenu PROTO :DWORDCheckMenuItem PROTO :DWORD,:DWORD,:DWORDInvalidateRect PROTO :DWORD,:DWORD,:DWORDFillRect PROTO :DWORD,:DWORD,:DWORDGetDC PROTO :DWORDSetPixel PROTO :DWORD,:DWORD,:DWORD,:DWORDReleaseDC PROTO :DWORD,:DWORDCreateCompatibleDC PROTO :DWORDCreateCompatibleBitmap PROTO :DWORD,:DWORD,:DWORDSelectObject PROTO :DWORD,:DWORDDeleteObject PROTO :DWORDDeleteDC PROTO :DWORDBeginPaint PROTO :DWORD,:DWORDBitBlt PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD, :DWORD,:DWORD,:DWORDEndPaint PROTO :DWORD,:DWORDGetAsyncKeyState PROTO :DWORD

;==============================================================================; .const = the constants area starts here, constants are defined and fixed;------------------------------------------------------------------------------.const; - Parameter MAIN WINDOW CallBack Procedure ( API=RegisterClassExA ) -WP1_CallBack equ [ebp+4] ;return addressWP1_hWnd equ [ebp+8] ;handle of window who receives messageWP1_uMsg equ [ebp+12] ;the message numberWP1_wParam equ [ebp+16] ;extra info about the messageWP1_lParam equ [ebp+20] ;extra info about the message

;==============================================================================; .Data = the data area starts here, datas are defined but not fixed; MASM converts real4, real8, real10 values into FPU/IEEE standard.;------------------------------------------------------------------------------.DataIconName db "TDIcon",0 ;icon name in rc fileMenuName db "TDMenu",0 ;menu name in rc fileClass db "TDWinClass",0 ;name of window classWindowName db "Test Department - http://surf.to/TestD - Escape key stops calculating",0;

; - Screen parameter -Xpos dd 0 ;X orgin point - RECT struc.Ypos dd 0 ;Y orgin point - RECT struc.Xmax dd 512 ;screen width - RECT struc.

Page 2

Page 3: Td Win32asm 150.Asm

td_win32asm_150.asmYmax dd 512 ;screen height - RECT struc.Color dd 0h ;default color value (black)cmulti dd 00000F00h ;calculates color, CHANGE THIS_dx real8 0.0 ;calculated from Xmax_dy real8 0.0 ;calculated from Ymax

; - Mandelbrot parameter - itmax dd 256 ;iteration, maximal valueit dd 0 ;iteration, start valuezr real8 0.0 ;real part of complex valuezi real8 0.0 ;imagin part of complex valuenolimit real8 4.000000000 ;border, result value is too farrmin real8 -0.500000000 ;real min. valuermax real8 2.000000000 ;real max. valueimin real8 -1.250000000 ;imagin min. valueimax real8 1.250000000 ;imagin max. valuecr real8 0.0 ;ci real8 0.0 ;

; - Program parameter -screenFlag dd 1h ;autocalcFlag dd 1h ;zoomCounter dd 0h ;zoom real8 0.020000000 ;view real8 0.020000000 ;org_rmin real8 -0.500000000 ;real min. value, default valueorg_rmax real8 2.000000000 ;real max. value, default valueorg_imin real8 -1.250000000 ;imagin min. value, defaultorg_imax real8 1.250000000 ;imagin max. value, default

;==============================================================================; .Data? = the data? area starts here, not defined and not fixed;------------------------------------------------------------------------------.data?handleMenu dd ? ;handle, menuhandleDC dd ? ;handle, main device contexthandleBitmap dd ? ;handle, compatible bitmaphandleCDC dd ? ;handle, compatible device contexthandleOld dd ? ;handle, old object

align 4; - WndClassEx Structure ( API=RegisterClassExA ) -cbSize dd ? ;size in bytes of this structurestyle dd ? ;window stylelpfnWndProc dd ? ;address of user proc functioncbclsExtra dd ? ;extra bytes to allocate set to 0cbWndExtra dd ? ;extra bytes class directive, rc filehInstance dd ? ;program handle(API=GetModuleHandleA)hIcon dd ? ;handle of icon (API=LoadIconA)hcursor dd ? ;handle of cursor (API=LoadCursor)hbrBackground dd ? ;background color, 0=transparentlpszMenuName dd ? ;name of menu class in resource filelpszClassName dd ? ;name of windows this window class

Page 3

Page 4: Td Win32asm 150.Asm

td_win32asm_150.asmhIconSm dd ? ;iconhandle 0=search in resource filehdcDest dd ? ;handle of dest. device context

align 4; - Msg Structure ( API=GetMessageA ) - member POINT = POINT structurehWnd dd ? ;handle of window who receives messagemessage dd ? ;the message numberwParam dd ? ;extra info about the messagelParam dd ? ;extra info about the messagetime dd ? ;time the message was postedxpt dd ? ;cursor x-position, POINT strucypt dd ? ;cursor x-position, POINT struc

align 4; - Paint Structure ( API=BeginPaint ) - member rcPaint = RECT structurehdc dd ? ;id, display DC used for painting fErase dd ? ;must background must be erasedrcPaint_left dd ? ;RECT,x-coordinate upper-left cornerrcPaint_top dd ? ;RECT,y-coordinate upper-left cornerrcPaint_right dd ? ;RECT,x-coordinate lower-right cornerrcPaint_bottom dd ? ;RECT,y-coordinate lower-right corner fRestore dd ? ;reserved, used by Windows fIncUpdate dd ? ;reserved, used by Windows rgbReserved db 20h dup (?) ;reserved, used by Windows

;==============================================================================; .CODE = our code area starts here Main = label of our program code;------------------------------------------------------------------------------.CodeMain:

;==============================================================================; Always get your program ID first (API=GetModuleHandleA);------------------------------------------------------------------------------push 0h ;lpModuleHandle, 0=get program handlecall GetModuleHandleA ;- API Function -mov hInstance,eax ;return value in eax=handle of program

;==============================================================================; Init the Floating Point Unit.;------------------------------------------------------------------------------finit ;init FPU

;==============================================================================; The API function "RegisterClassExA" registers a window class.; This API needs a "WNDCLASSEX" structure so we fill it with correct values.;------------------------------------------------------------------------------mov cbSize,30h ;size in bytes of WNDCLASSEX structuremov style,23h ;window style,CS_OWNDC,CS_VREDRAW,CS_HREDRAWmov lpfnWndProc,OFFSET WP1 ;address of user lpfnWndProc functionmov cbclsExtra,0h ;extra bytes to allocate set to 0mov cbWndExtra,0h ;class directive in rc filemov lpszMenuName,OFFSET MenuName;menu name in resource file,0=no menu

Page 4

Page 5: Td Win32asm 150.Asm

td_win32asm_150.asmmov lpszClassName,OFFSET Class ;name of windows classmov hIconSm,0h ;iconhandle 0=search in rc file;------------------------------------------------------------------------------; API "CreateSolidBrush" creates a logical brush with the specified solid color;------------------------------------------------------------------------------push 00000000h ;crColor, brush color value RGBcall CreateSolidBrush ;- API Function -mov hbrBackground,eax ;handle brush, background color;------------------------------------------------------------------------------; API "LoadIconA" loads an icon defined in the resource file and stores the; handle in the "WNDCLASSEX" structure;------------------------------------------------------------------------------push OFFSET IconName ;icon-string or icon resource idpush hInstance ;our program handlecall LoadIconA ;- API Function -mov hIcon,eax ;store handle of newly loaded icon;------------------------------------------------------------------------------; API "LoadCursorA" loads a default system cursor, in this case we must set; hInstance to 0 and lpCursorName to a default system cursor value, here 32512; Then we store the cursor handle in the "WNDCLASSEX" structure;------------------------------------------------------------------------------push 32512 ;lpCursorName, default value in dezimalpush 0h ;hInstance, 0=default system cursorcall LoadCursorA ;- API Function -mov hcursor,eax ;store handle of the cursor;------------------------------------------------------------------------------; Now, after filled the "WNDCLASSEX" structure we call API "RegisterClassEx";------------------------------------------------------------------------------push OFFSET cbSize ;pointer to WNDCLASSEX structurecall RegisterClassExA ;- API Function -

;==============================================================================; API "CreateWindowExA" creates an overlapped, pop-up, or child window with an; extended style. The return value in EAX is the handle of the new window.; This API sends a WM_CREATE message to the window procedure (WP1).;------------------------------------------------------------------------------push 0h ;lpParam, extra pointer data 0=no datapush hInstance ;hInstance, handle of our programpush 0h ;hMenu, handle window menu 0=class menupush 0h ;hWndParent, handle parent window 0=nopush 00000230h ;intnHeight, window height pixelpush 0000020Ah ;intnWidth, window width pixelpush 20h ;inty, vertical position windowpush 10h ;intx, horizontal position windowpush 04CA0000h ;dwStyle, look into WIN32.HLPpush OFFSET WindowName ;lpWindowName, pointer to window namepush OFFSET Class ;lpClassName, pointer to class namepush 0300h ;dwExStyle, extra window style 0=nocall CreateWindowExA ;- API Function -mov hWnd,eax ;hwnd,return value=handle of window

;==============================================================================; API "ShowWindow" function sets the specified window's show state.

Page 5

Page 6: Td Win32asm 150.Asm

td_win32asm_150.asm;------------------------------------------------------------------------------push 1h ;nCmdShow, show state 1=SW_SHOWNORMALpush hWnd ;hwnd, handle of windowcall ShowWindow ;- API Function -

;==============================================================================; API "UpdateWindow" updates the area of the specified window by sending a; WM_PAINT message to the window if the window's update region is not empty.;------------------------------------------------------------------------------push hWnd ;hwnd, handle of windowcall UpdateWindow ;- API Function -

LoopGetMessage:;==============================================================================; API "GetMessageA" retrieves a message + places it in the specified structure.;------------------------------------------------------------------------------push 0h ;wMsgFilterMax, highest message valuepush 0h ;wMsgFilterMin, lowest message valuepush 0h ;hWnd, handle of window who gets msg.push OFFSET hWnd ;lpMsg, pointer to MSG structurecall GetMessageA ;- API Function -cmp eax,0h ;check if return value=0 (exit)je ExitPrg ;if return value is 0 goto LABEL

;==============================================================================; API "TranslateMessage" translates virtual-key messages in character messages;------------------------------------------------------------------------------push OFFSET hWnd ;lpMSG, pointer to msg structurecall TranslateMessage ;- API Function - keyboard code

;==============================================================================; API "DispatchMessageA" function dispatches a message to a window procedure.;------------------------------------------------------------------------------push OFFSET hWnd ;lpMSG, pointer to msg structurecall DispatchMessageA ;- API Function -jmp LoopGetMessage ;check for message again, goto LABEL

ExitPrg:;==============================================================================; Next we terminate our program (API=ExitProcess);------------------------------------------------------------------------------push hInstance ;push our programm handle to exitcall ExitProcess ;- API Function -

;##############################################################################; The Window Procedure (API=RegisterClassExA) for this registered window.;------------------------------------------------------------------------------WP1:push ebp ;create stack framemov ebp,esp ;pushad ;push all register to the stack

mov eax,WP1_uMsg ;move the message number to eax

Page 6

Page 7: Td Win32asm 150.Asm

td_win32asm_150.asm;==============================================================================; WM_CREATE (value=01h) message received ?;------------------------------------------------------------------------------WP1_uMsg_01h:cmp eax,1h ;check if WM_CREATE message recievedjne WP1_uMsg_02h ;if not goto LABEL;------------------------------------------------------------------------------; API "GetMenu" retrieves the handle of the menu assigned to the given window.;------------------------------------------------------------------------------push WP1_hWnd ;hWnd, handle of windowcall GetMenu ;- API Function -mov handleMenu,eax ;handle of menu;------------------------------------------------------------------------------; API "GetDC" retrieves a handle of a display device context (DC) for the; client area of the specified window.;------------------------------------------------------------------------------push WP1_hWnd ;hwnd, handle of windowcall GetDC ;- API Function -mov handleDC,eax ;handle of DC;------------------------------------------------------------------------------; API "CreateCompatibleBitmap" creates a bitmap compatible with the device that; is associated with the specified device context.;------------------------------------------------------------------------------push Ymax ;nHeight, height of bitmap, in pixelspush Xmax ;nWidth, width of bitmap, in pixelspush handleDC ;hdc, handle of device contextcall CreateCompatibleBitmap ;- API Function -mov handleBitmap,eax ;handle of bitmap;------------------------------------------------------------------------------; API "CreateCompatibleDC" creates a memory device context (DC) compatible with; the specified device.;------------------------------------------------------------------------------push handleDC ;hdc, handle memory device context,0=screencall CreateCompatibleDC ;- API Function -mov handleCDC,eax ;handle, compatible DC;------------------------------------------------------------------------------; API "SelectObject" selects an object into the specified device context.; The new object replaces the previous object of the same type.;------------------------------------------------------------------------------push handleBitmap ;hgdiobj, handle of objectpush handleCDC ;hdc, handle of device contextcall SelectObject ;- API Function -mov handleOld,eax ;handle of previously object;------------------------------------------------------------------------------; API "FillRect" fills a rectangle using the specified brush. Includes the left; and top borders, but excludes the right and bottom borders of the rectangle.;------------------------------------------------------------------------------push hbrBackground ;hbr, handle of brushpush OFFSET Xpos ;lprc, address of structure with rectanglepush handleCDC ;hDC, handle of device contextcall FillRect ;- API Function -jmp WP1_return

Page 7

Page 8: Td Win32asm 150.Asm

td_win32asm_150.asm;==============================================================================; WM_DESTROY (value=02h) message received ?;------------------------------------------------------------------------------WP1_uMsg_02h:cmp eax,2h ;check if value=2h (WM_DESTROY)jne WP1_uMsg_0Fh ;if not 2h go to LABELcall My_CleanSystem ;- SubRoutine -;------------------------------------------------------------------------------; API "PostQuitMessage" indicates to Windows a request to terminate;------------------------------------------------------------------------------push 0h ;nExitCode, exit code=wParamcall PostQuitMessage ;- API Function -popad ;pop all register back from stackxor eax,eax ;set eax to 0 to exit our programmov esp,ebp ;delete stack framepop ebp ;ret 10h ;return and clear stack

;==============================================================================; WM_PAINT (value=0Fh) message, used to repaint the window area;------------------------------------------------------------------------------WP1_uMsg_0Fh:cmp eax,0Fh ;check if WM_PAINT message recievedjne WP1_uMsg_111h ;if not goto label;------------------------------------------------------------------------------; API "BeginPaint" prepares the specified window for painting and fills a; PAINTSTRUCT structure with information about the painting;------------------------------------------------------------------------------push OFFSET hdc ;lpPaint, pointer to PAINTSTRUCTpush WP1_hWnd ;hwnd, handle of the windowcall BeginPaint ;- API Function -mov hdcDest,eax ;handle, display device (DC)=dest.;------------------------------------------------------------------------------; API "BitBlt" performs a bit-block transfer of the color data corresponding; to a rectangle of pixels from the specified source device context into a; destination device context. ;------------------------------------------------------------------------------push 00CC0020h ;dwRop, raster operation codepush 0h ;nYSrc, y=src. rect. up-left cornerpush 0h ;nXSrc, x=src. rect. up-left cornerpush handleCDC ;hdcSrc, handle of source devicepush Ymax ;nHeight, height of dest. rect.push Xmax ;nWidth, width of dest. rect.push 0h ;nYDest, y=dest. rect. up-left cornerpush 0h ;nXDest, x=dest. rect. up-left cornerpush hdcDest ;hdcDest, handle destination devicecall BitBlt ;- API Function -;------------------------------------------------------------------------------; API "EndPaint" marks the end of painting in the given window.; This function is required for each call to the BeginPaint function, but only; after painting is complete. ;------------------------------------------------------------------------------push OFFSET hdc ;lpPaint, pointer PAINTSTRUCT

Page 8

Page 9: Td Win32asm 150.Asm

td_win32asm_150.asmpush WP1_hWnd ;hwnd, handle of the windowcall EndPaint ;- API Function -jmp WP1_return

;==============================================================================; WM_COMMAND (value=111h) message recieved ?;------------------------------------------------------------------------------WP1_uMsg_111h:cmp eax,111h ;check if WM_COMMAND message recievedjne WP1_uMsg_112h ;if not goto labelmov eax,WP1_wParam ;extra info about the message in axcmp ax,1h ;"Start" clicked ?jne WP1_uMsg_111_wParam_02h ;

WP1_uMsg_111_wParam_01h:mov Xpos,0h ;reset Xposmov Ypos,0h ;reset Yposcall My_FillRect ;- SubRoutine - , clear screen and buffer;------------------------------------------------------------------------------; Init some real8 variables using the Floating Point Unit.; cr=rmin, ci=imax, _dx=(rmax-rmin)/xmax, _dy=(imax-imin)/ymax;------------------------------------------------------------------------------ffree ST(7) ;free ST(7), must be empty for loadfld rmin ;load rmin into TOSfstp cr ;store in cr and POPfld imax ;load imax into TOSfstp ci ;store in ci and POPfld rmax ;load rmax into TOSfsub rmin ;sub, TOS-rminfidiv Xmax ;div, TOS/Xmaxfstp _dx ;store result in _dx and POPfld imax ;load imax into TOSfsub imin ;sub, TOS-iminfidiv Ymax ;div, TOS/Ymaxfstp _dy ;store result in _dy and POP

Next_Pixel:;------------------------------------------------------------------------------; MANDELBROT --> Formula : z=z^2-c; First reset variables, color=0 (black), it=0, zr=0, zi=0;------------------------------------------------------------------------------mov Color,0h ;set Color to 0 (black), default valuemov it,0h ;set deep to 0, start valuefldz ;load constant 0.0 into TOS=ST(0)fst zr ;store it in zr (real)fstp zi ;store in zi (imagin) and POP

Iteration:;------------------------------------------------------------------------------; Iteration (itmax) = maximal depth of calculation;------------------------------------------------------------------------------mov eax,itmax ;load iteration max. value into EAXcmp it,eax ;compare

Page 9

Page 10: Td Win32asm 150.Asm

td_win32asm_150.asmjae Set_Pixel ;equal or above, color will be black ...inc it ;inc iteration;------------------------------------------------------------------------------; Calculate imagin part. zi=2*zi*zr-ci;------------------------------------------------------------------------------fld zr ;load zr into TOSfmul zi ;multiplicate, zr*zi, result=ST(0)fadd ST(0),ST(0) ;add, ST(0)+(ST0), result=ST(0), it works !fsub ci ;sub, ST(0)-ci, result=ST(0), new zi !;------------------------------------------------------------------------------; Calculate real part. zr=zr*zr-zi*zi-cr;------------------------------------------------------------------------------fld zi ;load zi into TOS, new zi moves ST(1)fmul zi ;multiplicate, zi*zi, result=ST(0)fld zr ;load zr into TOS, zi=ST(1), new zi=ST(2) fmul zr ;multiplicate, zr*zrfsub ST(0),ST(1) ;sub, ST(0)-ST(1), result=ST(0)fsub cr ;sub, ST(0)-cr, result=ST(0)fst zr ;store result in zr, new zr !fmul zr ;multiplicate, zr*zr, for future usefxch ST(2) ;exchange ST(0) with ST(2)fst zi ;store new zifmul zi ;multiplicate, zi*zi, for future use;------------------------------------------------------------------------------; Check if calculated value goes to infinity. (zr*zr+zi*zi)<4;------------------------------------------------------------------------------fadd ST(0),ST(2) ;add, ST(0)+ST(2), result in ST(0)fcomp nolimit ;compare ST(0) with variable nolimit + POPfstsw ax ;copy FPU flags into axsahf ;copy ah of ax into CPU flag register !ffree ST(0) ;clear FPU stackffree ST(1) ;jb Iteration ;ready to check CPU flags

Set_Color:;------------------------------------------------------------------------------; Assign a color to the pixel, depends on current iteration value.;------------------------------------------------------------------------------fild it ;load it into TOSfimul cmulti ;yes, we want more color'sfistp Color ;store value in variableand Color,00FFFFFFh ;mask highest byte out for RGB !

Set_Pixel:;------------------------------------------------------------------------------; API "SetPixel" sets the pixel at specified coordinates to a specified color.;------------------------------------------------------------------------------push Color ;crColor, pixel color push Ypos ;Y, y-coordinate of pixelpush Xpos ;X, x-coordinate of pixelpush handleCDC ;hdc, handle of device context, BUFFER !call SetPixel ;- API Function -cmp screenFlag,0h ;

Page 10

Page 11: Td Win32asm 150.Asm

td_win32asm_150.asmje Next_Xpos ;;------------------------------------------------------------------------------; API "SetPixel" sets the pixel at specified coordinates to specified color.;------------------------------------------------------------------------------push Color ;crColor, pixel color push Ypos ;Y, y-coordinate of pixelpush Xpos ;X, x-coordinate of pixelpush handleDC ;hdc, handle of device context, SCREEN !call SetPixel ;- API Function -

Next_Xpos:;------------------------------------------------------------------------------; cr=cr+_dx and inc Xpos;------------------------------------------------------------------------------fld cr ;load cr into TOSfadd _dx ;add, cr+_dxfstp cr ;store result in cr and POPinc Xpos ;time to increase Xposmov eax,Xpos ;cmp eax,Xmax ;check if right window border reachedjb Next_Pixel ;mov Xpos,0h ;reset Xpos

;------------------------------------------------------------------------------; API "GetAsyncKeyState" determines whether a key is up or down at the time the; function is called ...;------------------------------------------------------------------------------push 1Bh ;vKey, virtual-key code, VK_ESCAPE=1Bhcall GetAsyncKeyState ;- API Function -shr eax,1Fhcmp eax,1hje Force_WM_PAINT ;escape key pressed, stop calc and exit !

Next_Ypos:;------------------------------------------------------------------------------; cr=rmin, ci=ci-_dy and inc Ypos;------------------------------------------------------------------------------fld rmin ;load rmin into TOSfstp cr ;store value in cr and POPfld ci ;load ci into TOSfsub _dy ;sub, ci-_dyfstp ci ;store result in ci and POPinc Ypos ;time to increase Yposmov eax,Ypos ;cmp eax,Ymax ;check if bottom window border reachedjb Next_Pixel ;

Force_WM_PAINT:;------------------------------------------------------------------------------; API "InvalidateRect" adds a rectangle to specified window's update region.; This function performs a WM_PAINT message.;------------------------------------------------------------------------------push 0h ;bErase, erase-background flag

Page 11

Page 12: Td Win32asm 150.Asm

td_win32asm_150.asmpush 0h ;lpRect, rect structure, 0h=client areapush WP1_hWnd ;hWnd, handle window changed update regioncall InvalidateRect ;- API Function -jmp WP1_return

WP1_uMsg_111_wParam_02h:cmp ax,2h ;"Out" clicked ?, zoom outjne WP1_uMsg_111_wParam_03h ;;------------------------------------------------------------------------------; Zoom out. rmin=rmin-zoom, rmax=rmax+zoom, imin=imin-zoom, imax=imax+zoom;------------------------------------------------------------------------------cmp zoomCounter,0 ;limit for zooming outje WP1_returndec zoomCounter ;ffree ST(7) ;free ST(7), must be empty for loadfld rmin ;load rmin into TOSfsub zoom ;sub, TOS-zoomfstp rmin ;store result in rmin and POPfld rmax ;load rmax into TOSfadd zoom ;add, TOS+zoomfstp rmax ;store result in rmax and POPfld imin ;load imin into TOSfsub zoom ;sub, TOS-zoomfstp imin ;store result in imin and POPfld imax ;load imax into TOSfadd zoom ;add, TOS+zoomfstp imax ;store result in imax and POPcmp autocalcFlag,1h ;je WP1_uMsg_111_wParam_01h ;jmp WP1_return

WP1_uMsg_111_wParam_03h:cmp ax,3h ;"In" clicked ?, zoom injne WP1_uMsg_111_wParam_04h ;;------------------------------------------------------------------------------; Zoom in. rmin=rmin+zoom, rmax=rmax-zoom, imin=imin+zoom, imax=imax-zoom;------------------------------------------------------------------------------cmp zoomCounter,62 ;limit for zooming injae WP1_returninc zoomCounter ;ffree ST(7) ;free ST(7), must be empty for loadfld rmin ;load rmin into TOSfadd zoom ;add, TOS+zoomfstp rmin ;store result in rmin and POPfld rmax ;load rmax into TOS, rmin moves ST(1)fsub zoom ;sub, TOS-zoomfstp rmax ;store result in rmax and POPfld imin ;load imin into TOS, rmin=ST(2),rmax=ST(1)fadd zoom ;add, TOS+zoomfstp imin ;store result in imin and POPfld imax ;load imax into TOSfsub zoom ;sub, TOS-zoomfstp imax ;store result in imax and POP

Page 12

Page 13: Td Win32asm 150.Asm

td_win32asm_150.asmcmp autocalcFlag,1h ;je WP1_uMsg_111_wParam_01h ;jmp WP1_return

WP1_uMsg_111_wParam_04h:cmp ax,4h ;"Left" clicked ?, moves leftjne WP1_uMsg_111_wParam_05h ;;------------------------------------------------------------------------------; Changes the viewport to the left, scrolls left;------------------------------------------------------------------------------ffree ST(7) ;free ST(7), must be empty for loadfld rmin ;load rmin into TOSfadd view ;add, TOS-viewfstp rmin ;store result in rmin and POPfld rmax ;load rmax into TOSfadd view ;add, TOS-viewfstp rmax ;store result in rmax and POPcmp autocalcFlag,1h ;je WP1_uMsg_111_wParam_01h ;jmp WP1_return

WP1_uMsg_111_wParam_05h:cmp ax,5h ;"Right" clicked ?, moves rightjne WP1_uMsg_111_wParam_06h ;;------------------------------------------------------------------------------; Changes the viewport to the right, scrolls right;------------------------------------------------------------------------------ffree ST(7) ;free ST(7), must be empty for loadfld rmin ;load rmin into TOSfsub view ;sub, TOS-viewfstp rmin ;store result in rmin and POPfld rmax ;load rmax into TOSfsub view ;sub, TOS-viewfstp rmax ;store result in rmax and POPcmp autocalcFlag,1h ;je WP1_uMsg_111_wParam_01h ;jmp WP1_return

WP1_uMsg_111_wParam_06h:cmp ax,6h ;"Up" clicked ?, moves upjne WP1_uMsg_111_wParam_07h ;;------------------------------------------------------------------------------; Changes the viewport up, scrolls up;------------------------------------------------------------------------------ffree ST(7) ;free ST(7), must be empty for loadfld imin ;load imin into TOSfsub view ;sub, TOS-viewfstp imin ;store result in imin and POPfld imax ;load imax into TOSfsub view ;sub, TOS-viewfstp imax ;store result in imax and POPcmp autocalcFlag,1h ;je WP1_uMsg_111_wParam_01h ;

Page 13

Page 14: Td Win32asm 150.Asm

td_win32asm_150.asmjmp WP1_return

WP1_uMsg_111_wParam_07h:cmp ax,7h ;"Down" clicked ?, moves downjne WP1_uMsg_111_wParam_08h ;;------------------------------------------------------------------------------; Changes the viewport down, scrolls down;------------------------------------------------------------------------------ffree ST(7) ;free ST(7), must be empty for loadfld imin ;load imin into TOSfadd view ;add, TOS-viewfstp imin ;store result in imin and POPfld imax ;load imax into TOSfadd view ;add, TOS-viewfstp imax ;store result in imax and POPcmp autocalcFlag,1h ;je WP1_uMsg_111_wParam_01h ;jmp WP1_return

WP1_uMsg_111_wParam_08h:cmp ax,8h ;"Reset" clicked ?jne WP1_uMsg_111_wParam_0Ah ;;------------------------------------------------------------------------------; This function loads the default values for calculating the fractal;------------------------------------------------------------------------------mov zoomCounter,0h ;reset zoom counterffree ST(7) ;free ST(7), must be empty for loadfld org_rmin ;load org_rmin into TOSfstp rmin ;store in rmin and POPfld org_rmax ;load org_rmax into TOSfstp rmax ;store in rmax and POPfld org_imin ;load org_imin into TOSfstp imin ;store in imin and POPfld org_imax ;load org_imax into TOSfstp imax ;store in imax and POPcmp autocalcFlag,1h ;je WP1_uMsg_111_wParam_01h ;jmp WP1_return

WP1_uMsg_111_wParam_0Ah:cmp ax,0Ah ;"Screen On" clicked ?jne WP1_uMsg_111_wParam_0Bh ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flags ;MF_BYCOMMAND=0h | MF_CHECKED=8hpush 0Ah ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flags ;MF_BYCOMMAND=0h | MF_UNCHECKED=0h

Page 14

Page 15: Td Win32asm 150.Asm

td_win32asm_150.asmpush 0Bh ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov screenFlag,1h ;jmp WP1_return

WP1_uMsg_111_wParam_0Bh:cmp ax,0Bh ;"Screen Off" clicked ?jne WP1_uMsg_111_wParam_0Ch ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flagspush 0Bh ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 0Ah ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov screenFlag,0h ;jmp WP1_return

WP1_uMsg_111_wParam_0Ch:cmp ax,0Ch ;"Color 1" selected ?jne WP1_uMsg_111_wParam_0Dh ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flags ;MF_BYCOMMAND=0h | MF_CHECKED=8hpush 0Ch ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flags ;MF_BYCOMMAND=0h | MF_UNCHECKED=0hpush 0Dh ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov cmulti,00000F00h ;jmp WP1_return

WP1_uMsg_111_wParam_0Dh:cmp ax,0Dh ;"Color 2" selected ?jne WP1_uMsg_111_wParam_0Eh ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flagspush 0Dh ;uIDCheckItem, menu item to check/uncheck

Page 15

Page 16: Td Win32asm 150.Asm

td_win32asm_150.asmpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 0Ch ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov cmulti,00002F3Fh ;jmp WP1_return

WP1_uMsg_111_wParam_0Eh:cmp ax,0Eh ;"Autocalc On" clicked ?jne WP1_uMsg_111_wParam_0Fh ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flagspush 0Eh ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 0Fh ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov autocalcFlag,1h ;jmp WP1_return

WP1_uMsg_111_wParam_0Fh:cmp ax,0Fh ;"Autocalc Off" clicked ?jne WP1_uMsg_111_wParam_10h ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flagspush 0Fh ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 0Eh ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov autocalcFlag,0h ;jmp WP1_return

WP1_uMsg_111_wParam_10h:cmp ax,10h ;"Iteration 256" clicked ?jne WP1_uMsg_111_wParam_11h ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flags

Page 16

Page 17: Td Win32asm 150.Asm

td_win32asm_150.asmpush 10h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 11h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 12h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 13h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov itmax,256 ;jmp WP1_return

WP1_uMsg_111_wParam_11h:cmp ax,11h ;"Iteration 128" clicked ?jne WP1_uMsg_111_wParam_12h ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flagspush 11h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 10h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 12h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 13h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov itmax,128 ;jmp WP1_return

WP1_uMsg_111_wParam_12h:cmp ax,12h ;"Iteration 064" clicked ?jne WP1_uMsg_111_wParam_13h ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flagspush 12h ;uIDCheckItem, menu item to check/uncheck

Page 17

Page 18: Td Win32asm 150.Asm

td_win32asm_150.asmpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 10h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 11h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 13h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov itmax,064 ;jmp WP1_return

WP1_uMsg_111_wParam_13h:cmp ax,13h ;"Iteration 032" clicked ?jne WP1_return ;;------------------------------------------------------------------------------; API "CheckMenuItem" sets the state of the specified menu item's check mark; attribute to either checked or unchecked.;------------------------------------------------------------------------------push 8h ;uCheck, menu item flagspush 13h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 10h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 11h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -push 0h ;uCheck, menu item flagspush 12h ;uIDCheckItem, menu item to check/uncheckpush handleMenu ;hmenu, handle of menucall CheckMenuItem ;- API Function -mov itmax,032 ;jmp WP1_return

;==============================================================================; WM_SYSCOMMAND (value=112h) message recieved ?;------------------------------------------------------------------------------WP1_uMsg_112h:cmp eax,112h ;check if WM_COMMAND message recievedjne WP1_return ;if not goto labelmov eax,WP1_wParam ;extra info about the messagecmp eax,0F060h ;SC_CLOSE=0F060h received ?jne WP1_return ;call My_CleanSystem ;- SubRoutine -

Page 18

Page 19: Td Win32asm 150.Asm

td_win32asm_150.asmjmp WP1_return

;==============================================================================; API "DefWindowProcA" calls the window procedure to provide default processing; for any window messages that an application does not process.; This function ensures that every message is processed.; It is called with the same parameters received by the window procedure.;------------------------------------------------------------------------------WP1_return:popad ;pop all register from stackpush WP1_lParam ;extra info about the messagepush WP1_wParam ;extra info about the messagepush WP1_uMsg ;the message numberpush WP1_hWnd ;handle of window who receives messagecall DefWindowProcA ;- API Function -mov esp,ebp ;delete stack framepop ebp ;ret 10h ;return and clear stack;##############################################################################

;******************************************************************************; My own subroutine(s) for a compacter code resist here ...;------------------------------------------------------------------------------My_CleanSystem:;------------------------------------------------------------------------------; API "DeleteObject" deletes a logical pen, brush, font, bitmap, region, or; palette, freeing all system resources associated with the object.;------------------------------------------------------------------------------push handleBitmap ;hObject, handle compatible graphic objectcall DeleteObject ;- API Function -;------------------------------------------------------------------------------; API "DeleteDC" deletes the specified device context (DC).;------------------------------------------------------------------------------push handleCDC ;hdc, handle of compatible device contextcall DeleteDC ;- API Function -;------------------------------------------------------------------------------; API "ReleaseDC" releases a device context (DC), freeing it for use by other.;------------------------------------------------------------------------------push handleDC ;hdc, handle of device contextpush WP1_hWnd ;hwnd, handle of windowcall ReleaseDC ;- API Function -ret

My_FillRect:;------------------------------------------------------------------------------; API "GetDC" retrieves a handle of a display device context (DC) for the; client area of the specified window.;------------------------------------------------------------------------------push WP1_hWnd ;hwnd, handle of windowcall GetDC ;- API Function -mov handleDC,eax ;hDC, handle of main device context;------------------------------------------------------------------------------; API "FillRect" fills a rectangle using the specified brush. Includes the left

Page 19

Page 20: Td Win32asm 150.Asm

td_win32asm_150.asm; and top borders, but excludes the right and bottom borders of the rectangle.;------------------------------------------------------------------------------push hbrBackground ;hbr, handle of brushpush OFFSET Xpos ;lprc, address of structure with rectanglepush handleDC ;hDC, handle of main device contextcall FillRect ;- API Function -;------------------------------------------------------------------------------; API "FillRect" fills a rectangle using the specified brush. Includes the left; and top borders, but excludes the right and bottom borders of the rectangle.;------------------------------------------------------------------------------push hbrBackground ;hbr, handle of brushpush OFFSET Xpos ;lprc, address of structure with rectanglepush handleCDC ;hDC, handle of compatible device contextcall FillRect ;- API Function -;------------------------------------------------------------------------------; API "ReleaseDC" releases a device context (DC), freeing it for use by other.;------------------------------------------------------------------------------push handleDC ;hdc, handle of main device contextpush WP1_hWnd ;hwnd, handle of windowcall ReleaseDC ;- API Function -ret;******************************************************************************

;==============================================================================; end Main = end of our program code;------------------------------------------------------------------------------end Main ;end of our program code, entry point

;==============================================================================; To create the exe file use this commands with your Microsoft Assembler/Linker;------------------------------------------------------------------------------; ml.exe /c /coff td_win32asm_150.asm ;asm command; rc.exe /v rsrc.rc ;rc command; cvtres.exe /machine:ix86 rsrc.res; link.exe /subsystem:windows td_win32asm_150.obj rsrc.obj ;link command;==============================================================================

Page 20