汇编语言计时器代码分析(19)

来源:互联网 发布:毒蛇队与马刺比赛数据 编辑:程序博客网 时间:2024/05/09 20:28

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

 

程序内容为,每经过100毫秒就在屏幕上显示一个点,当大于5000毫秒时退出。

 

代码如下:

 

 

TITLE Calculate Elapsed Time                   ( TimingLoop.asm )

 

; This program uses GetTickCount to calculate the number

; of milliseconds that have elapsed since the program

; started.

 

INCLUDE Irvine32.inc

TIME_LIMIT = 5000

.data

startTime DWORD ?

dot BYTE ".",0

 

.code

main PROC

      INVOKE GetTickCount       ; get milliseconds

      mov startTime , eax

L1: mov edx , OFFSET dot      ; display a dot

      call WriteString

 

      INVOKE  Sleep , 100         ; sleep for 100ms

 

      INVOKE  GetTickCount

      sub eax , startTime           ; check the elapsed time

      cmp eax , TIME_LIMIT

      jb L1

L2: exit

main ENDP

END main

 

程序里用到了两个新函数:GetTickCount 和 Sleep。

 

GetTickCount  函数原型如下:

 

GetTickCount  PROTO          ; 返回值在EAX中

 

GetTickCount  用来返回系统启动以来所经过的毫秒数,因为计数值是一个双字,所以系统连续运行49.7天之后,计数值将归0.

 

Sleep 函数原型如下:

 

Sleep PROTO,

          dwMilliseconds : DWORD

 

用来暂停参数指定的毫秒数。

 

注释一下程序:

 

 

TITLE Calculate Elapsed Time                   ( TimingLoop.asm )

 

; This program uses GetTickCount to calculate the number

; of milliseconds that have elapsed since the program

; started.

 

INCLUDE Irvine32.inc

TIME_LIMIT = 5000              ; 设定了截止时间5000毫秒

.data

startTime DWORD ?

dot BYTE ".",0

 

.code

main PROC

      INVOKE GetTickCount       ; 调用GetTickCount 得到程序开始的时间(系统启动以来的时间),保存在eax中

      mov startTime , eax          ; 将现在的时间保存在变量中

L1: mov edx , OFFSET dot      ; display a dot

      call WriteString

 

      INVOKE  Sleep , 100         ; 间隔100毫秒

 

      INVOKE  GetTickCount      ; 再次得到现在的时间

      sub eax , startTime           ; 减去程序开始时的时间

      cmp eax , TIME_LIMIT       ; 看是否大于截止时间

      jb L1                                 ; 如果小于则继续执行

L2: exit

main ENDP

END main

 

 

 代码完。