`
qdujunjie
  • 浏览: 108720 次
  • 性别: Icon_minigender_1
  • 来自: Mars
社区版块
存档分类
最新评论

汇编语言秒表程序代码分析(21)

阅读更多

 

本文代码来自于《Intel汇编语言程序设计》 (第四版)第11章-----------32位windows编程。

 

 

秒表程序使用了一个TimeStart来启动秒表,还有一个TimeStop返回自TimeStart启动以来的毫秒数。

 

 

程序本身其实很简单,以下为代码:

 

TITLE Calculate Elapsed Time

 

; Demonstrate a simple  stopwatch timer, using

; the Win32 GetTickCount function.

 

INCLUDE Irvine32.inc

 

TimerStart PROTO,

     pSaveTime : PTR DWORD

 

TimeStop PROTO,

     pSaveTime : PTR DWORD

 

.data

msg BYTE "milliseconds have elapsed" , 0dh , 0ah , 0

timer1 DWORD ?

 

.code

main PROC

      INVOKE TimerStart ,      ; 开始计时      

       ADDR timer1                 ; 传入一个指向DWORD类型的指针

 

      INVOKE Sleep , 5000     ; 暂停5秒

 

      INVOKE TimerStop,        ; 结束计时   

      ADDR timer1                  ; 传入一个指向DWORD类型的指针

 

      call WriteDec                 ; 打印一共花费的毫秒数

      mov edx,OFFSET msg

      call WriteString

 

      exit

main ENDP

 

 

;----------------------------------------------------------------------------

TimerStart PROC uses eax esi,

     pSavedTime : PTR DWORD

; starts a stopwatch timer.

; Receives : pointer to a variable that will hold

; the current time.

; Returns : nothing

;----------------------------------------------------------------------------

 

       INVOKE  GetTickCount         ; 得到了时间值,保存在eax中

       mov esi,pSavedTime            ; 得到传入的参数地址

       mov [esi],eax                       ; 将得到的时间值保存在传入的DWORD类型指针所指向的地址中

       ret

TimerStart ENDP

 

 

;----------------------------------------------------------------------------

TimerStop PROC uses esi,

     pSavedTime : PTR DWORD                    ; 接收一个指向DWORD类型的指针作为参数

; Stops the current stopwatch timer.

; Receives : pointer to a variable holding the saved time

; Returns : EAX = number of elapsed milliseconds

; Remarks : Accurate to about 10ms              ; 系统的精确度在XP是10ms

;----------------------------------------------------------------------------

 

       INVOKE  GetTickCount             ; 又得到了时间,将得到的时间值保存到了eax中

       mov esi,pSavedTime                ; 将接收到指针赋值到esi中

       sub eax,[esi]                            ; 使用eax的值减去目前[esi]中的值记得到自TimerStart以来的时间差

       ret

TimerStop ENDP

 

END main

 

 

注意在main之前,有TimerStart 和TimerStop 两个函数原型的定义,汇编语言和其他语言不同,必须在使用之前定义,这样main中使用到这两个函数时才会知道,否则程序会认为找不到这两个函数。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics