汇编语言秒表程序代码分析(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中使用到这两个函数时才会知道,否则程序会认为找不到这两个函数。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值