原创
用汇编写的一个小玩意
2008-3-31 21:29
4758
7
7
分类:
软件与OS
昨晚无聊,用汇编写了一个小玩意,确定CPU的频率.没什么希奇的,玩啊.代码贴在下面:
/********************************************************************************************/
.686
.Model Flat, StdCall
Option Casemap :None
Include windows.inc
Include user32.inc
Include kernel32.inc
Include gdi32.inc
includelib gdi32.lib
IncludeLib user32.lib
IncludeLib kernel32.lib
include macro.asm
WinMain PROTO WORD,WORD,WORD,WORD
WndProc PROTO WORD,WORD,WORD,WORD
.DATA
szClassName db "MASMPlus_Class",0
szCpuFreq db "你的cpu主频是:%uMhz",0
.DATA?
hInstancedd ?
dwTickCount dd ?
dwCpuFreq dd ?
dwTsc dd ?,?
szBuffer db 256 dup (?)
.CODE
START:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke WinMain,hInstance,NULL,NULL,SW_SHOWDEFAULT
invoke ExitProcess,0
WinMain proc hInstWORD,hPrevInstWORD,CmdLineWORD,CmdShowWORD
LOCAL wc :WNDCLASSEX
LOCAL msg :MSG
local hWnd :HWND
mov wc.cbSize,sizeof WNDCLASSEX
mov wc.style,CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
mov wc.lpfnWndProc,offset WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_BTNFACE+1
mov wc.lpszMenuName,NULL
mov wc.lpszClassName,offset szClassName
invoke LoadIcon,hInst,100
mov wc.hIcon,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
mov wc.hIconSm,0
invoke RegisterClassEx, ADDR wc
invoke CreateWindowEx,NULL,ADDR szClassName,CTXT("CPUFrequency"),WS_OVERLAPPEDWINDOW,200,200,400,200,NULL,NULL,hInst,NULL
mov hWnd,eax
invoke ShowWindow,hWnd,SW_SHOWNORMAL
invoke UpdateWindow,hWnd
StartLoop:
invoke GetMessage,ADDR msg,NULL,0,0
cmp eax, 0
je ExitLoop
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
jmp StartLoop
ExitLoop:
mov eax,msg.wParam
ret
WinMain endp
WndProc proc hWinWORD,uMsgWORD,wParam WORD,lParam WORD
LOCAL hdc:HDC
LOCAL psAINTSTRUCT
LOCAL rect:RECT
.if uMsg==WM_CREATE
invoke GetTickCount
mov dwTickCount,eax
rdtsc
mov dwTsc,eax
mov dwTsc+4,edx
invoke SetTimer,hWin,1,1000,NULL
.elseif uMsg == WM_DESTROY
invoke KillTimer,hWin,1
invoke PostQuitMessage,NULL
.elseif uMsg==WM_TIMER
invoke GetTickCount
push eax
sub eax,dwTickCount
pop dwTickCount
push eax;store the elapse time
rdtsc
push eax
push edx
sub eax,dwTsc
sbb edx,dwTsc+4
pop dwTsc+4
pop dwTsc
mov ecx,1000000;MHZ
div ecx
cmp edx,500000
ja round
;.if edx>=500000
;inc eax
;.endif
compute:
mov ecx,1000;turn to ms
mul ecx
pop ecx;load elapse time
div ecx
invoke wsprintf,offset szBuffer,offset szCpuFreq,eax
invoke GetClientRect,hWin,addr rect
invoke InvalidateRect,hWin,addr rect,TRUE
;invoke SendMessage,hWin,WM_PAINT,NULL,NULL
jmp end_this
round:
inc eax
jmp compute
end_this:
.elseif uMsg==WM_PAINT
invoke BeginPaint,hWin,addr ps
mov hdc,eax
invoke GetClientRect,hWin,addr rect
invoke DrawText,hdc,offset szBuffer,-1,addr rect,DT_CENTER or DT_SINGLELINE or DT_VCENTER
invoke EndPaint,hWin,addr ps
.else
invoke DefWindowProc,hWin,uMsg,wParam,lParam
.endif
ret
WndProc endp
END START
关闭
站长推荐
/3
文章评论(0条评论)
登录后参与讨论