²Ä¤Q¤G³¹¡@­p®É¾¹

¥Á°ê 70 ¦~¡A°ê»Ú°Ó·~¾÷¾¹¤½¥q¡]IBM¡^¶}©l¥X²£ IBM PC ­Ó¤H¹q¸£®É¡A¨º®É¦b IBM PC µwÅ餺³¡´N¦³¤@­Ó½s¸¹¬O 8253 ªº´¹¤ù¡A±Mªù­t³d­p®É¡C³o­Ó´¹¤ù¥HÀW²v 18.2Hz ªº¤è¦¡²£¥Í²Ä 8 ¸¹¤¤Â_¡]18.2Hz ªº·N«ä´N¬O»¡¨C¬íIJµo²Ä 8 ¸¹¤¤Â_ 18.2 ¦¸¡^¡A´«¥y¸Ü»¡¨C¹j 55 ²@¬í¡]1¡Ò18.2≑0.0549¡^´N°õ¦æ¤@¦¸²Ä 8 ¸¹¤¤Â_¡CIBM PC ¦@¦³ 256 ­Ó¤¤Â_¡A½s¸¹¥Ñ¹s¶}©l¨ì¤Q¤»¶i¦ìªº 0FFH¡A§Ú­Ì¥i¥H§â¤¤Â_·Q¦¨¬O¬YºØ°Æµ{¦¡¡C

¨ì¤F²{¦b¡A8253 ´¹¤ù¤w¸g³Q°ªºë«×¨Æ¥ó­p®É¾¹¡]high precision event timer¡AÁY¼g HPET¡^¨ú¥N¡A¨Ã¾ã¦X¨ì¤F«n¾ô´¹¤ù¤¤¡A¦ý«n¾ô´¹¤ù¤]«O¯d¤F 8253 ªº¥\¯à¡C¦]¦¹²{¦bªº¹q¸£¡A¤´µM¯à°õ¦æ¥H«eªº§@·~¨t²Î¡A¨Ò¦p MS¡ÐDOS¡A¥u­n§A¯à§ä¨ì¶}¾÷®É±Ò°Êªº¤èªk¡C

¦b¤j³¡¤Àªº±¡§Î¤U¡A§Ú­Ì¤£»Ý­nºë±K«×«Ü°ªªº­p®É¾¹¡A¨C 55 ²@¬íIJµo¤@¦¸ªººë±K«×¤w¸g¨¬°÷¤F¡C³o¤@³¹­n¤¶²Ð¦b Windows §@·~¨t²Î¤¤ªº­p®É¾¹¡A¨Ã»¡©ú¦p¦óÀ³¥Î¡C


­p®É¾¹

¦b Windows §@·~¨t²Î¤¤¡A­p®É¾¹¥i¥H¬Ý°µ¬O¤@ºØ¿é¤J³]³Æ¡A§Ú­Ì¥i¥H«ü©w¥¦¨C¹j¤@¬q©T©wªº®É¶¡¶¡¹j¡A¹ïÀ³¥Îµ{¦¡µo¥X WM_TIMER °T®§¡C¥E¬Ý¤§¤U¡A­p®É¾¹¦ü¥G¤£¹³Áä½L¡B·Æ¹«¨º»ò­«­n¡A¦ý¬O¥¦¤]¦³¤@¨Ç¤£¥i¿i·Àªº°^Äm¡A¨Ò¦p¡G

  1. ¦Û°ÊÀx¦s¡G«ü©w¤@¬q®É¶¡«á¡A¨Ò¦p¤­¤ÀÄÁ¡AÀ³¥Îµ{¦¡¥i¥H¦Û°ÊÀx¦s¸ê®Æ¨ìµwºÐ¤W¡C
  2. ¦h¤u¡GÁöµM¦Û Windows 95 ¤§«áªº Windwos §@·~¨t²Î¬OÀu¥ýÅv¦¡ªº¦h¤u¡]µù¤@¡^¡A¦ý¬O¦pªGÀ³¥Îµ{¦¡­n³B²z¤j¶q¸ê®Æ®É¡A¥i±N³o¨Ç¤u§@¤Á³Î¦¨³\¦h³¡¥÷¡A§Q¥Î©w®É¾¹¶Ç¨Ó°T®§®É³B²z¤@³¡¥÷¡C
  3. ¼½©ñ­µ¼Ö¡G§Q¥Î©w®É¾¹¦b­I´º¼½©ñ­µ¼Ö¡C
  4. ¦Û°Êµ²§ôªº¸Õ¥Îª©³nÅé©Î¸Õª±ª©¹q¸£¹CÀ¸¡G³]©w¥u¯àÅý¨Ï¥ÎªÌ¸Õ¥Î©Î¸Õª±¤@¬q©T©w®É¶¡¡A´Nµ²§ôµ{¦¡¡C
  5. ½Õ¾ã³t«×¡G¥i¥H§Q¥Î©w®É¾¹®ø°£¦]¬° CPU §ÖºC¦Ó³y¦¨ªº³t«×®t²§¡C

¥H¤W¬O­p®É¾¹ªºÀ³¥Î¡A¨º»ò­n¦p¦ó¨Ï¥Î­p®É¾¹©O¡H­º¥ý¡A§Ú­Ì¥²¶·¥ý«Ø¥ß­p®É¾¹¡C

SetTimer API

¦b Windows §@·~¨t²Î¤¤¡AÀ³¥Îµ{¦¡µLªkª½±µÅª¨ú²Ä 8 ¸¹¤¤Â_¡A³o¨ÇµwÅé¸ê·½³£¥Ñ Windows ²ÎÄw¹B¥Î¡CÀ³¥Îµ{¦¡¥²¶·³z¹L Windows ¨Ó¨Ï¥Î³o¨ÇµwÅé¸ê·½¡A¥i¥H©I¥s SetTimer¡A«Ø¥ß­p®É¾¹¡CWindows ·|¬°¦¹­p®É¾¹«Ø¥ß¤@­Ó­p¼Æ¾¹¨Ãªì©l¥¦¡AµM«á¨C¹j 55 ²@¬í­p¼Æ¾¹«K´î¤@¡Aª½¨ì´î¦Ü¹s¡A«K¹ïÀ³¥Îµ{¦¡µo¥X WM_TIMER °T®§¡CÀ³¥Îµ{¦¡¦¬¨ì WM_TIMER °T®§«á¡A«K¯à¦b³B²z¦¹°T®§°µ¤@¨Ç¤u§@¡C©³¤U¬O SetTimer ªº¥Îªk¡G

invoke  SetTimer,\
        hWnd,\      ; handle of window for timer messages
        nIDEvent,\  ; timer identifier
        uElapse,\   ; time-out value
        lpTimerFunc ; address of timer procedure

¤j³¡¤Àªº±¡§Î¤U¡AlpTimerFunc ¬°¹s¡A¨º»ò Windows ¨C¹j¤@¬q®É¶¡¶¡¹j¡]¦¹®É¶¡¶¡¹j¥Ñ²Ä¤T­Ó°Ñ¼Æ uElapse «ü©w¡^´N·|§â WM_TIMER µo°eµ¹©I¥s SetTimer ®É²Ä¤@­Ó°Ñ¼Æ©Ò«ü©wªºµøµ¡¡Cµo°eªº WM_TIMER °T®§ùتº wParam µ¥©ó­p®É¾¹ÃѧO½X¡A¤]´N¬O©I¥s SetTimer ®Éªº²Ä¤G­Ó°Ñ¼Æ¡F¦ÓlParam µ¥©ó¹s¡C¦]¬°¥i¥H¦b¦P­ÓÀ³¥Îµ{¦¡¤¤¥Î¦h­Ó¤£¦Pªº nIDEvent ¨Ó¦h¦¸©I¥s SetTimer¡A¦p¦¹´N«Ø¥ß¦h­Ó­p®É¾¹¡A³o¼Ë´N­n¥Î wParam ¤À¿ë¤£¦Pªº­p®É¾¹¡C¨ä®æ¦¡¦p¤U¡G

        invoke  SetTimer,hwnd,TIMER_SEC,1000,0  ;«Ø¥ß¨C¬í°e¥X WM_TIMER ªº­p®É¾¹
        invoke  SetTimer,hwnd,TIMER_MIN,60000,0 ;«Ø¥ß¨C¤À°e¥X WM_TIMER ªº­p®É¾¹
        ⁝
case WM_TIMER:
    switch wParam
    case TIMER_SEC:
        ;¨C¬í¤@¦¸ªº³B²z

    case TIMER_MIN:
        ;¨C¤ÀÄÁ¤@¦¸ªº³B²z
    endsw
        ⁝

°²¦p²Ä¥|­Ó°Ñ¼Æ lpTimerFunc ¤£¬O¹s¡A¦Ó¬O¬Y­Ó°Æµ{¦¡¦ì§}ªº¸Ü¡A¨º»ò Windows ¨C¹j¤@­Ó®É¶¡¶¡¹j¡A¤£¦A¬Oµo°e WM_TIMER °T®§µ¹©I¥s SetTimer ®É²Ä¤@­Ó°Ñ¼Æ©Ò«ü©wªºµøµ¡¡A¦Ó¬O©I¥s¸Ó°Æµ{¦¡¤@¦¸¡C¥Ñ¦¹¬Ý¨Ó¡A¦¹°Æµ{¦¡¨ä¹ê¬O¤@ºØ¦^©I¨ç¦¡¡C°²³]¦¹¦^©I¨ç¦¡¦WºÙ¬O TimerProc¡A¨ä­ì«¬¬°¡G

TimerProc   PROC    hwnd:HWND, message:UNIT, iTimerID:UNIT, dwTime:DWORD
            ; ³o¨à¬O¨C¹j¤@­Ó®É¶¡¶¡¹j­n³B²zªº¤u§@
TimerProc   ENDP

Windows ¶Çµ¹ TimerProc ªº²Ä¤@­Ó°Ñ¼Æ hwnd¡A¨ä¹ê´N¬O©I¥s SetTimer ®Éªºµøµ¡¥N½X¡Amessage ©l²×¬O WM_TIMER¡AiTimerID ¬O©I¥s SetTimer ®Éªº¦^¶Ç­È¡AdwTimer ¬O±Ò°Ê Windows ®É¤w¸g¹L¦h¤Ö²@¬í¡C

¦pªG­p®É¾¹«Ø¥ß¥¢±Ñ¡A¦^¶Ç­È¬°¹s¡C¦pªG¦¨¥\«Ø¥ß­p®É¾¹¡A¨ä¦^¶Ç­È¤S¤À¨âºØ±¡§Î¡G

  1. ¦pªG²Ä¤@­Ó°Ñ¼Æ hWnd ¬°µøµ¡¥N½X¡A¨º»ò¦^¶Ç­È¬°«D¹s­È¡C
  2. ¦pªG²Ä¤@­Ó°Ñ¼Æ hWnd ¬°¹s¡A¨º»ò SetTimer ·|©¿²¤ nIDEvent °Ñ¼Æ¡A¦Ó¦^¶Ç­È¬°­p®É¾¹ÃѧO½X¡Cº¸«á­n¾P·´­p®É¾¹®É¡A©I¥s KillTimer ®É¡A²Ä¤@­Ó°Ñ¼Æ hWnd ³]¬°¹s¡A²Ä¤G­Ó°Ñ¼Æ´N¬O¦¹¦^¶Ç­È¡C

²Ä¤T­Ó°Ñ¼Æ¬O®É¶¡¶¡¹j¡AÁöµM³æ¦ì¬O²@¬í¡A¦ý¬O¥Ñ©ó³\¦h¦]¯À¡AµLªkºë½T¨ì¤@²@¬í¡A¦]¦¹¦pªG¯u­nºë½T­p®É¡A³Ì¦n¤£­n¨Ï¥Î SetTimes ­p®É¾¹¡C¦pªG uElapse ¤p©ó USER_TIMER_MINIMUM¡]USER_TIMER_MINIMUM¡×0AH¡^¡A«h uElapse ·|³]¬° USER_TIMER_MINIMUM¡F¦pªG uElapse ¤j©ó USER_TIMER_MAXIMUM¡]USER_TIMER_MINIMUM¡×07FFFFFFFH¡^¡A«h uElapse ·|³]¬° USER_TIMER_MAXIMUM¡C

KillTimer API

·í¥Î¤£µÛ­p®É¾¹®É¡A©Î¬Oµ{¦¡µ²§ô®É¡A¥²¶·±N­p®É¾¹¾P·´¡C¾P·´­p®É¾¹ªº¤èªk¬O©I¥s KillTimer¡A¨ä»yªk¦p¤U¡G

invoke  KillTimer,\
        hWnd,\      ; handle of window that installed timer
        uIDEvent    ; timer identifier

¦pªG KillTimer ¦¨¥\¾P·´­p®É¾¹¡A¦^¶Ç­È¬°«D¹s¡F¥¢±Ñ¡A¦^¶Ç­È¬°¹s¡C


­p®É¾¹ªºÀ³¥Î¡GRNDLINE1.ASM

©³¤U¹ê§@¤@­Ó¨Ò¤l¡A»¡©ú­p®É¾¹ªº¥Îªk¡C³o­Ó¨Ò¤l¬O RNDLINE1.ASM¡A¥¦·|¦bµøµ¡ªº¤u§@°Ï¤º¥H¥ª¤W¨¤¬°°_ÂI¡AÀH¾÷¨ú¤@ÂI¬°²×ÂIø¥X¤@­ÓÀH¾÷ÃC¦âªº½u¬q¡A¦Ó«á§â¸Ó½u¬qªº²×ÂI§@¬°¥t¤@½u¬qªº°_ÂI¡A¦AÀH¾÷¨ú¤@ÂI¬°²×ÂIø¥X²Ä¤G­ÓÀH¾÷ÃC¦âªº½u¬q¡A¦p¦¹¤@ª½­«½Æª½¨ì¨Ï¥ÎªÌÃö³¬ RNDLINE1 ¬°¤î¡C

RNDLINE1.ASM ²o¯A¨ì·í«e¹³¯À¡Bø»s½u¬q¤Î±m¦â¤å¦r¡A¦]¦¹¥ý¸É¥R¦³Ãö³o¤è­±ªºª¾ÃÑ¡C©³¤U©Ò­n½Í½×ªº¬O GDI ª«¥ó¤§¤@¡Aµeµ§¡Cµeµ§»Pµe¨ê¬Û¦ü¡A­n¥ý«Ø¥ß«á¤~¯à¨Ï¥Î¡Aµeµ§¬O¥Î¨Óµe¥X½u¬q¡B¦±½u©Î¬O¹Ï§Î¡]¨Ò¦p¯x§Î¡B¾ò¶êµ¥¡^¡C

CreatePen API

CreatePen ¯à¥Î«ü©wªº­·®æ¡B¼e«×¥H¤ÎÃC¦â«Ø¥ßÅÞ¿èµeµ§¡CCreatePen ªº­ì«¬¬O¡G

invoke  CreatePen,\
        fnPenStyle,\    ; pen style
        nWidth,\        ; pen width
        crColor         ; pen color

¦pªG¦¨¥\«Ø¥ßÅÞ¿èµeµ§¡A¦^¶Ç­È¬O¦¹ÅÞ¿èµeµ§ªº¥N½X¡A¨º»ò¥i¥H¥Î¦¹ÅÞ¿èµeµ§¥N½X¬°°Ñ¼Æ¡A©I¥s SelectObject ±N¦¹ÅÞ¿èµeµ§¿ï¤J¸Ë¸m¤º®e¡Cº¸«áµe¥Xªº½u¬q¡B¦±½u³£¬O¥Î³o­ÓÅÞ¿èµeµ§Ã¸»s¡C»P«e¤@³¹µe¨ê¤@¼Ë¡A·í©Ò«Ø¥ßªºµeµ§¤£¥Î®É¡A¥²¶·©I¥s DeleteObject §R°£¡A¥H¸`¬Ù¸ê·½¡C¦pªG«Ø¥ßÅÞ¿èµeµ§¥¢±Ñ¡ACreatePen ªº¦^¶Ç­È¬O 0¡C

SelectObject API

SelectObject ¨Ï¸Ë¸m¤º®e¤ºªº GDI ª«¥ó³]©w¬°·s«ü©wªº GDI ª«¥ó¡C¨Ò¦p¥i¥H©I¥s SelectObject ±N¸Ë¸m¤º®e¤¤Â¦³ªºµeµ§¡A¥H·s«Ø¥ß¦nªºµeµ§¨ú¥N¡CSelectObject ªº­ì«¬¬O¡G

invoke  SelectObject,\
        hdc,\       ; handle to DC
        hgdiobj     ; handle to object

¯à°÷³Q SelectObject ¿ï¤Jªº GDI ª«¥ó¦³¤U­±´XºØÃþ«¬¡G①¦ì¤¸¹Ï¡]bitmap¡^¡B②µe¨ê¡]brush¡^¡B③¦r§Î¡]font¡^¡B④µeµ§¡]pen¡^¡B⑤°Ï°ì¡]region¡^¡C

¦pªG SelectObject ªº²Ä¤G­Ó°Ñ¼Æ¤£¬O°Ï°ì¡A¦Ó SelectObject ¤S°õ¦æ¥¢±Ñ¡A¨º»ò¦^¶Ç­È¬°¹s¡F¦pªG°õ¦æ¦¨¥\¡A¦^¶Ç­È¬O­ì¥ý GDI ª«¥ó¥N½X¡C¦pªG SelectObject ªº²Ä¤G­Ó°Ñ¼Æ¬O°Ï°ì¡A¦Ó SelectObject ¤S°õ¦æ¥¢±Ñ¡A¨º¼¯¦^¶Ç­È¬O HGDI_ERROR¡F¦pªG°õ¦æ¦¨¥\¡A¨º»ò¦^¶Ç­È¬O¤U¦C¤§¤@¡G

  1. SIMPLEREGION¡G°Ï°ì¶È¥]§t¤@­Ó¯x§Î¡C
  2. COMPLEXREGION¡G°Ï°ì¥]§t¨â­Ó©Î¨â­Ó¥H¤Wªº¯x§Î¡C
  3. NULLREGION¡G°Ï°ì¬OªÅªº¡C

MoveToEx API

·íµ{¦¡©I¥s BeginPaint ©Î GetDC ¥HÀò±oµøµ¡¤u§@°Ïªº¸Ë¸m¤º®e¥N½X®É¡A·|±N¤u§@°Ï¥ª¤W¨¤·í°µ·í«eªº¹³¯À¡]¹³¯À­^¤å¬O pixel¡A¥i¥H¬Ý¦¨¬O¿Ã¹õ¤WªºÂI¡^¡A¦Ó MoveToEx ¥i¥H§â·í«e¹³¯À²¾¨ì«ü©wªº¹³¯À¤W¡CMoveToEx ªº­ì«¬¬O¡G

invoke  MoveToEx,\
        hdc,\       ; handle of device context
        X,\         ; x-coordinate of new current position
        Y,\         ; y-coordinate of new current position
        lpPoint     ; address of old current position

¦pªG MoveToEx ¦¨¥\³]©w·í«e¹³¯À¡A¦^¶Ç­È¬°«D¹s¡F¦pªG¥¢±Ñ¡A¦^¶Ç­È¬°¹s¡C

LineTo API

LineTo ¥i¥H§â·í«e¹³¯À»P«ü©wªº²×ÂI¹³¯À¨âÂI³s±µ°_¨Ó¡Aø¥X¤@½u¬q¨Ó¡CLineTo ªº­ì«¬¬O¡G

invoke  LineTo,\
        hdc,\       ; device context handle
        nXEnd,\     ; x-coordinate of line's ending point
        nYEnd       ; y-coordinate of line's ending point

LineTo ·|¥Î·í«eªºµeµ§Ã¸¥X½u¬q¡A·í«eµeµ§¥i¥H¥Î©I¥s CreatePen¡BCreatePenIndirect µ¥ API «Ø¥ß¡AµM«á¥H SelectObject ¿ï¤J¨ì¸Ë¸m¤º®e¤¤¡CLineTo ¦pªG¦¨¥\ø¥X½u¬q¡A¨º»ò¦^¶Ç­È¬°«D¹s¡A¦P®É·í«e¹³¯À³]©w¬° nXEnd¡BnYEnd ©Ò«üªº¹³¯À¡F¦pªG¥¢±Ñ¡A¦^¶Ç­È¬°¹s¡C

SetTextColor API

SetTextColor ±N«ü©wªº¸Ë¸m¤º®eªº¤å¦r³]©w¬°¬YºØÃC¦â¡C¨ä­ì«¬¬°¡G

invoke  SetTextColor,\
        hdc,\       ; handle of device context
        crColor     ; text color

¦pªG¦¨¥\³]©w¤å¦rÃC¦â¡A¦^¶Ç­È¬°­ì¨Óªº¤å¦rÃC¦â¡F¦pªG¥¢±Ñ¡A¦^¶Ç­È¬° CLR_INVALID¡A¥i¥H©I¥s GetLastError ±o¨ì§ó¶i¤@¨Bªº¿ù»~­ì¦]¡C

RNDLINE1.ASM ­ì©lµ{¦¡

©³¤U¬O RNDLINE1.ASM ­ì©lµ{¦¡ªº§¹¾ã¦Cªí¡C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
;RNDLINE1.ASM¡G¦b¿Ã¹õ¤WÅã¥Üµøµ¡¡A§Q¥Î­p®É¾¹¨C¹j¤@¬q®É¶¡¡A¦bµøµ¡¤ºÀH¾÷ø»s½u¬q
;²ÕĶ¤è¦¡¡G
;SET INCLUDE=C:\UASM\Include
;SET LIB=C:\UASM\lib64
;SET PATH=C:\UASM;C:\UASM\bin64;
;SET LINK=/SUBSYSTEM:WINDOWS
;UASM64 -win64 RNDLINE1.ASM
;LINK RNDLINE1.OBJ
        OPTION  CASEMAP:NONE
        OPTION  WIN64:3

INCLUDE         WINDOWS.INC
INCLUDELIB      GDI32.LIB
INCLUDELIB      KERNEL32.LIB
INCLUDELIB      USER32.LIB

TIMER_ID        EQU     1000            ;­p®É¾¹ÃѧO½X
;*******************************************************************************
.CONST
szClassName     DB      "RndLine1",0     ;µøµ¡Ãþ§O¦WºÙ
szAppName       DB      "½u¬q",0
szErr           DB      "¿ù»~",0
szFmt           DB      "%d Lines",0
szErrMsg0       DB      "CPU¤£¤ä´©¡C",0
szErrMsg1       DB      "µù¥Uµøµ¡Ãþ§O¥¢±Ñ¡C",0
;*******************************************************************************
.DATA
hInstance       HINSTANCE   ?   ;¼Ò²Õ¥N½X
hWnd            HWND        ?   ;µøµ¡¥N½X
iWidth          DD          ?   ;¤u§@°Ï¼e«×
iHeight         DD          ?   ;¤u§@°Ï°ª«×
cLines          DD          ?
pnt             POINT       <0,0>
;*******************************************************************************
.CODE
;-------------------------------------------------------------------------------
;»s³y¤@¶Ã¼Æ¡A¥B¤p©órange¡AµM«á¦s©óEAX«áªð¦^
get_rnd PROC    range:DWORD
        mov     ecx,30
        bsr     eax,range       ;¥H¤G¶i¦ìªí¥Ürange¡A³Ì°ª¦ì¤¸¬°1ªº¦b²ÄEAX¦ì¤¸
        mov     r11d,7fffffffh  ;¨Ï²Å¸¹¦ì¤¸¬°0
        sub     ecx,eax         ;ECX¡×R11Dªº²Å¸¹¦ì¤¸­n¥k²¾¦h¤Ö¦¸
        sar     r11d,cl         ;°õ¦æ¥k²¾CL¦¸
retry:  rdrand  eax
        and     eax,r11d
        cmp     eax,range
        ja      retry
        ret
get_rnd ENDP
;-------------------------------------------------------------------------------
DrawLine PROC   hwnd:HWND
         LOCAL  hdc:HDC,hPen:HPEN,buffer[16]:BYTE,text_color:DWORD
     ;Àò±oµøµ¡ªº¸Ë¸m¤º®e
         invoke GetDC,hwnd
         mov    hdc,rax
     ;«Ø¥ßÀH¾÷ªºÃC¦â¡A¥H¦¹ÃC¦â«Ø¥ßµeµ§¡A¨Ã³]©w¤å¦rÃC¦â
         invoke get_rnd,0ffffffh
         mov    text_color,eax
         invoke CreatePen,PS_SOLID,3,eax
         mov    hPen,rax
         invoke SetTextColor,hdc,text_color
     ;§â·s«Ø¥ßªºµeµ§¿ï¤J¸Ë¸m¤º®e¤¤
         invoke SelectObject,hdc,hPen  
     ;§â·í«e¹³¯À³]¬°¤W¦¸½u¬q²×ÂI
         invoke MoveToEx,hdc,pnt.x,pnt.y,0
     ;¦b¤u§@°Ï¤ºÀH¾÷¿ï¾Ü¤@ÂI¡A¨Ã¦s¤Jpnt¡A¥H§@¬°¤U¦¸ªº°_ÂI
         invoke get_rnd,iWidth
         mov    edx,eax
         mov    pnt.x,eax
         invoke get_rnd,iHeight
         mov    pnt.y,eax
     ;§â·í«eªºÂI¡]¤W¦¸½u¬q²×ÂI¡^»PÀH¾÷¿ï¾ÜªºÂI³s±µ°_¨Ó¡A§Î¦¨½u¬q
         invoke LineTo,hdc,edx,eax
         inc    cLines
     ;¦b¤u§@°Ï¥k¤U¨¤Åã¥Ü¤wµe¥Xªº½u¬q­Ó¼Æ
         invoke SetTextAlign,hdc,TA_BOTTOM or TA_RIGHT
         invoke wsprintf,ADDR buffer,OFFSET szFmt,cLines
         invoke TextOut,hdc,iWidth,iHeight,ADDR buffer,eax
         invoke DeleteObject,hPen
         invoke ReleaseDC,hwnd,hdc
         ret
DrawLine ENDP
;-------------------------------------------------------------------------------
WndProc PROC    hwnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
.switch uMsg
.case WM_CREATE
        invoke  SetTimer,hwnd,TIMER_ID,330,0

.case WM_TIMER
        invoke  DrawLine,hwnd

.case WM_SIZE
        mov     r10,lParam
        mov     r11,lParam
        and     r10,0ffffh
        shr     r11,10h
        mov     iWidth,r10d
        mov     iHeight,r11d
        mov     cLines,0

.case WM_DESTROY
        invoke  KillTimer,hwnd,TIMER_ID
        invoke  PostQuitMessage,0
.default
        invoke  DefWindowProc,hwnd,uMsg,wParam,lParam
        ret
.endsw
        xor     rax,rax
        ret
WndProc ENDP
;-------------------------------------------------------------------------------
;check_rdrand¥Î¨ÓÀˬdCPU¬O§_¤ä´©RDRAND¡A¦pªG¤ä´©¦^¶Ç­È¬°1¡F¤£¤ä´©¦^¶Ç­È¬°0
check_rdrand    PROC
;¨Ã¤£¬O©Ò¦³ªºCPU³£¤ä´©RDRAND«ü¥O¡A©Ò¥H¦b¨Ï¥Î«e­n¥ý¶i¦æÀË´ú¡G§âEAX¼È¦s¾¹¸m1¨Ã°õ¦æ
;CPUID«ü¥O¡A¦pªG¤ä´©RDRANDªº¸Ü¡AECX¼È¦s¾¹ªº²Ä30¦ì¤¸¦ì·|³Q¸m¬°1¡C
                mov     rax,1
                cpuid
                test    ecx,40000000h
                jnz     support_rdrand
                xor     rax,rax
support_rdrand: ret
check_rdrand    ENDP
;-------------------------------------------------------------------------------
main    PROC
        LOCAL   wc:WNDCLASSEX,msg:MSG
        call    check_rdrand            ;ÀˬdCPU¬O§_¤ä´©RDRAND
        lea     rdx,szErrMsg0
        or      rax,rax                 ;­YRAX¡×0¡A¤£¤ä´©¡A¸õ¦Üerror:³B
        jz      error
        invoke  GetModuleHandle,0       ;¨ú±o¼Ò²Õ¥N½X
        mov     hInstance,rax
        mov     wc.cbSize,SIZEOF WNDCLASSEX
        mov     wc.style,CS_HREDRAW or CS_VREDRAW
        lea     rdx,WndProc
        mov     wc.lpfnWndProc,rdx
        mov     wc.cbClsExtra,0
        mov     wc.cbWndExtra,0
        mov     wc.hInstance,rax
        invoke  LoadIcon,NULL,IDI_APPLICATION   ;¨ú±o¹Ï¥Ü¥N½X
        mov     wc.hIcon,rax                    ;¦s¤J¹Ï¥Ü¥N½X
        mov     wc.hIconSm,rax                  ;¦s¤J¤p¹Ï¥Ü¥N½X
        invoke  LoadCursor,NULL,IDC_ARROW       ;¨ú±o´å¼Ð¥N½X
        mov     wc.hCursor,rax                  ;¦s¤J´å¼Ð¥N½X
        mov     wc.hbrBackground,COLOR_WINDOW+1
        mov     wc.lpszMenuName,0
        lea     rdx,szClassName
        mov     wc.lpszClassName,rdx
        invoke  RegisterClassEx,ADDR wc         ;µù¥Uµøµ¡Ãþ§O
        lea     edx,szErrMsg1
.if rax==0
error:  invoke  MessageBox,0,rdx,OFFSET szErr,MB_ICONERROR or MB_OK
        jmp     quit
.endif
        invoke  CreateWindowEx,0,ADDR szClassName,ADDR szAppName,WS_OVERLAPPEDWINDOW,\
                CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hInstance,0
        mov     hWnd,rax
        invoke  ShowWindow,hWnd,SW_SHOWNORMAL
        invoke  UpdateWindow,hWnd
.while TRUE
        invoke  GetMessage,ADDR msg,0,0,0
.break .if rax==0
        invoke  TranslateMessage,ADDR msg
        invoke  DispatchMessage,ADDR msg
.endw
quit:   invoke  ExitProcess,0                   ;µ{¦¡µ²§ô
main    ENDP
;*******************************************************************************
END     main

RNDLINE1.ASM ²Ä 81 ¦æ¡A¦b³B²z WM_CREATE °T®§®É¡A«Ø¥ß¤F¨C¹j 330 ²@¬íµo°e WM_TIMER °T®§µ¹µøµ¡¨ç¦¡ªº­p®É¾¹¡C¦b³B²z WM_DESTROY °T®§®É¡A©I¥s KillTimer §R°£¦¹­p®É¾¹¡A¦]¬°¦¹¨è§Y±N°h¥Xµ{¦¡¡A­p®É¾¹¤]´N¨S¥Î¤F¡C

¦b RNDLINE1 °õ¦æ¹Lµ{¤¤¡AWindows ·|¨C¹j 330 ²@¬í¡Aµo¥X WM_TIMER °T®§µ¹µøµ¡¨ç¦¡¡Cµøµ¡¨ç¦¡¦b³B²z³o«h°T®§®É¡A©I¥s DrawLine °Æµ{¦¡¡C¦b DrawLine °Æµ{¦¡¤¤¡A¥ýÀò±o¸Ë¸m¤º®e¥N½X¡]²Ä 53¡ã55 ¦æ¡^¡AµM«á¥H¶Ã¼Æ»s³y¥ô·N¤@ºØÃC¦â¡Aµ¥·|¨à·|¥Î³oºØÃC¦âµe½u¨ÃÅã¥Ü¤wµe¥X´X±ø½uªº¤å¦r¡C©Ò¥Hºò±µµÛ²Ä 56¡ã63 ¦æ´N¬O©I¥s CreatePen «Ø¥ßÅÞ¿èµeµ§»P©I¥s SetTextColor ³]©w¤å¦rÃC¦â¡AµM«á¬O©I¥s SelectObject ±N¸Ë¸m¤º®eªºµeµ§³]©w¬°­è¤~«Ø¥ßªºµeµ§¡C

¥»¨Ó±µ¤U¨Ó´N¬O­n©I¥s LineTo µe¥X½u¬q¡ALineTo ©Òµe¥Xªº½u¬q¬O§â·í«e¹³¯À»P¥Ñ¶Ã¼Æ§Î¦¨ªº X¡BY ®y¼Ð©Ò¥Nªíªº¹³¯À³s±µ°_¨Ó¡C¦ý¬O¦]¬°¨C¦¸©I¥s GetDC «á¡AWindows ³£·|§â·í«e¹³¯À³]¬°¥ª¤W¨¤¡A¦pªGª½±µ©I¥s LineTo µe½u¬qªº¸Ü¡A¨º»ò¨C¦¸ªº½u¬q°_ÂI³£¬O¥ª¤W¨¤¡A³o¨Ã¤£²Å¦X­ì¨Óªº·Qªk¡C¸Ñ¨Mªº¤èªk´N¬O§â«e¦¸ªº²×ÂI«O¦s°_¨Ó¡A¦b©I¥s LineTo ¤§«e¡A¥ý©I¥s MoveToEx ±N·í«e¹³¯À³]¬°«e¦¸²×ÂI¡A³o¼Ëµe¥X¨Óªº½u¬q´N¯à­º§À¬Û³s¡C

«e¦¸²×ÂI«O¦s¦b pnt ÅܼÆùØ¡A³o­ÓÅܼƨä¹ê¬O¤@­Ó POINT µ²ºcÅé¡Cµ{¦¡¤@¶}©l®É¡A¬°¤u§@°Ïªº¥ª¤W¨¤¡A¨ä®y¼Ð¬O ( 0, 0 )¡A¨£²Ä 33 ¦æ¡C¨C¦¸©I¥s DrawLine ®É¡A³£·|ÀH¾÷«ü©w¤@­Ó¹³¯À¡A§@¬°½u¬qªº²×ÂI¡ARNDLINE1 ¤]¥²¶·±N³o­Ó¹³¯Àªº®y¼Ð«O¦s¤U¨Ó¡A¥H§@¬°¤U¦¸½u¬qªº°_ÂI¡C


­p®É¾¹ªº¥t¤@ºØ¥Îªk

RNDLINE1.ASM ¤¤ªº­p®É¾¹¬O§â WM_TIMER °T®§µo°eµ¹µøµ¡¨ç¦¡¡A¦ý¬O¤]¥i¥HÅý Windows ±N WM_TIMER °T®§µo°eµ¹¬Y­Ó¦^©I¨ç¦¡¡C©³¤Uªº RNDLINE2.ASM ´N¬O³o¼Ëªº¨Ò¤l¡C¤£¹L¤p¤ì°¸¤£¥´ºâ§â¥¦§¹¾ãªº¦C¥X¨Ó¡A¥u¦C¥X­«­nªº³¡¤À¡C

1
2
3
4
5
6
7
8
9
10
⁝ 
50
51
52
⁝ 
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
⁝ 
164
165
;RNDLINE2.ASM¡G¦b¿Ã¹õ¤WÅã¥Üµøµ¡¡A§Q¥Î­p®É¾¹¨C¹j¤@¬q®É¶¡¡A¦bµøµ¡¤ºÀH¾÷ø»s½u¬q
;²ÕĶ¤è¦¡¡G
;SET INCLUDE=C:\UASM\Include
;SET LIB=C:\UASM\lib64
;SET PATH=C:\UASM;C:\UASM\bin64;
;SET LINK=/SUBSYSTEM:WINDOWS
;UASM64 -win64 RNDLINE2.ASM
;LINK RNDLINE2.OBJ
        OPTION  CASEMAP:NONE
        OPTION  WIN64:3
        ⁝
;-------------------------------------------------------------------------------
DrawLine PROC   hwnd:HWND, uMsg:UINT, iTimerID:WPARAM, dwTime:LPARAM
         LOCAL  hPen:HPEN,hdc:HDC,buffer[16]:BYTE,text_color:DWORD
        ⁝
DrawLine ENDP
;-------------------------------------------------------------------------------
WndProc PROC    hwnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
.switch uMsg
.case WM_CREATE
        invoke  SetTimer,hwnd,TIMER_ID,330,OFFSET DrawLine

.case WM_SIZE
        mov     r10,lParam
        mov     r11,lParam
        and     r10,0ffffh
        shr     r11,10h
        mov     iWidth,r10d
        mov     iHeight,r11d
        mov     cLines,0

.case WM_DESTROY
        invoke  KillTimer,hwnd,TIMER_ID
        invoke  PostQuitMessage,0
.default
        invoke  DefWindowProc,hwnd,uMsg,wParam,lParam
        ret
.endsw
        xor     rax,rax
        ret
WndProc ENDP
;-------------------------------------------------------------------------------
        ⁝
;*******************************************************************************
END     main

³Ì­«­n¤£¦P¬O²Ä 87 ¦æ¡ASetTimer ªº³Ì«á¤@­Ó°Ñ¼Æ§ï¦¨¡uOFFSET DrawLine¡v¡A³o¼Ë´N·|¨Ï Windows ±N WM_TIMER °T®§µo°eµ¹ DrawLine °Æµ{¦¡¡C¥¿¦]¬°¦p¦¹¡A¾ã­Óµøµ¡¨ç¦¡¡A±q²Ä 83¡ã108 ¦æ¡A³£¤£»Ý³B²z WM_TIMER¡C¦Ó DrawLine ¬O³Q Windows §@·~¨t²Î©Ò©I¥s¡A¦]¦¹¬O¦^©I¨ç¦¡¡CWindows ¶Ç¨Óªº°Ñ¼Æ¦³¥|­Ó¡AÁöµM¥u¦³²Ä¤@­Ó°Ñ¼Æ hwnd ¥Î±oµÛ¡A²Ä¤G­Ó°Ñ¼Æ uMsg ¥²©w¬O WM_TIMER¡A¨ä¥L¨â­Ó°Ñ¼Æ¦b³o­Óµ{¦¡¤¤¥Î¤£¨ì¡A¦ýÁÙ¬O±N¨ä¥þ³¡¦C¥X¨Ó¡A¨£ RNDLINE2.ASM ²Ä 51 ¦æ¡C


µùÄÀ

µù¤@¡G¦h¤u¡]multitasking¡^

¦h¤u¬O¦b¤@¬q®É¶¡¤º¦P®É°õ¦æ¦h­Óµ{¦¡¡]ºë½TªºÁ¿ªk¬O¦æµ{¡Aprocess¡^¡C¬Y­Óµ{¦¡¥i¥H¦b¥¿¦b°õ¦æªºµ{¦¡ÁÙ¨Sµ²§ô¤§«e¡A´N°õ¦æ¦Ó«áªÌ·|¤¤Â_¦ý¤£¤@©w¬Oµ²§ô¡C¦]¦¹¡A¦h¤u¥i¬Ý¦¨¹q¸£¥H¥æ¿ùªº¤è¦¡°õ¦æ¦h­Óµ{¦¡¤ù¬q¡A¦ý¦]¬°¹q¸£°õ¦æ³t«×«Ü§Ö¡A¥H¤HÃþªº¨¤«×¬Ý¨Ó¦ü¥G¬O¦P®É°õ¦æ³\¦hµ{¦¡¡A¦Ó¹ê»Ú¤W¬O³o¨Çµ{¦¡¦@¨É CPU ©M°O¾ÐÅé¡C·íµM¤¤Â_ªºµ{¦¡¥²¶·«O¦s¨äª¬ºA¡]¨Ò¦p°O¾ÐÅ餺®e©M¼È¦s¾¹¤º®e¡^¡AµM«á¤~¯à¸ü¤J¥t¤@­Óµ{¦¡¨Ã±N±±¨îÅvÂಾµ¹¥¦¡C

¦h¤u¤À¬°¨âºØ¡G①¨ó½Õ¦¡¦h¤u¡]cooperative multi-tasking¡^»P②Àu¥ýÅv¦¡¦h¤u¡]preemptive multi-tasking¡^¡C

¨ó½Õ¦¡¦h¤u¬O«ü¥¿¦b°õ¦æªºµ{¦¡¥D°Ê¥æ¥X±±¨îÅv¡AµM«á§@·~¨t²Î¦A±N¨ä¥æµ¹¨ä¥Lµ{¦¡¡C³oºØ¦h¤u¦h¥b¬O¥Ñ©ó CPU ¥¼¯à¦³®Ä¤ä´©©Î®Ä¯à¤£°÷¦Óµo®i¥X¨Óªº¦h¤u¡C¦pªG¦³¤@­Óµ{¦¡¤£¥æ¦^±±¨îÅv©ÎªÌ·í±¼¤F¡A¨º»ò¾ã­Ó§@·~¨t²Î´N·í±¼¤F¡A¥u¦³­«·s±Ò°Ê¤@³~¡C¨Ò¦p«Øºc¦b MS¡ÐDOS ¤Uªº Windows 3.1 ´NÄÝ©ó¨ó½Õ¦¡¦h¤u¡C

Àu¥ýÅv¦¡¦h¤u¤]¥s¥ý¦û¦¡¦h¤u¡A¦b³oºØ¦h¤u¤§¤U¡A¨C­Óµ{¦¡ªº°õ¦æ®É¶¡¥Ñ§@·~¨t²Î¨Ó¤À°t¡A¤@­Óµ{¦¡ªº®É¶¡¨Ï¥Î§¹¤§«á¡A¨t²Î´N·|±N CPU ¤À°tµ¹¤U¤@­Óµ{¦¡¡]³oºØ°Ê§@ºÙ¬°context switching¡^¡A¨S¦³¤@­Óµ{¦¡¯à¿W¦û CPU¡C³oºØ¦h¤u¤§¤U¡A¤@­Óµ{¦¡·í±¼¡A¤£·|¼vÅT¥t¤@­Óµ{¦¡¡COS/2¡BWindows NT/2000/XP/Vista/7/8.x/10/11¡BUNIX/Linux/Solaris/Mac-OS ³£ÄÝ©ó³oºØ¦h¤u¤è¦¡¡C