32位汇编语言学习笔记(42)--测试时间相关函数

来源:互联网 发布:壁虎数据恢复免费版 编辑:程序博客网 时间:2024/05/21 22:53


此程序出自《Assembly Language step by step programming with linux》第12章,用于演示时间相关的libc库函数的使用,程序中使用了如下的libc库函数:
time_t time( time_t *timer );
char *ctime( const time_t *timer );
double difftime( time_t timer1, time_t timer0 );
struct tm *localtime( const time_t *timer );
int getchar( void );

程序如下:

[SECTION .data]; Section containing initialised dataTimeMsg  db "Hey, what time is it?  It's %s",10,0YrMsg db "The year is %d.",10,0Elapsed  db "A total of %d seconds has elapsed since program began running.",10,0[SECTION .bss]; Section containing uninitialized dataOldTime resd 1; Reserve 3 integers (doubles) for time valuesNewTime  resd 1TimeDiff resd 1TimeStr  resb 40; Reserve 40 bytes for time stringTmCopy resd 9; Reserve 9 integer fields for time struct tm[SECTION .text]; Section containing codeextern ctimeextern difftimeextern getcharextern printfextern localtimeextern timeglobal main; Required so linker can find entry pointmain:    push ebp; Set up stack frame for debuggermov ebp,esppush ebx; Program must preserve EBP, EBX, ESI, & EDIpush esipush edi;;; Everything before this is boilerplate; use it for all ordinary apps!; Generate a time_t calendar time value with clib's time functionpush 0; Push a 32-bit null pointer to stack,;   since we don't need a buffer.call time; Returns calendar time in EAXadd esp,4; Clean up stack after callmov [OldTime],eax; Save time value in memory variable; Generate a string summary of local time with clib's ctime functionpush OldTime; Push address of calendar time valuecall ctime; Returns pointer to ASCII time string in EAX    add esp,4; Stack cleanup for 1 parmpush eax; Push pointer to ASCII time string on stackpush TimeMsg; Push pointer to base message text stringcall printf; Merge and display the two stringsadd esp,8; Stack cleanup: 2 parms X 4 bytes = 8; Generate local time values into clib's static tm structpush dword OldTime; Push address of calendar time valuecall localtime; Returns pointer to static time structure in EAXadd esp,4; Stack cleanup for 1 parm; Make a local copy of clib's static tm structmov esi,eax; Copy address of static tm from eax to ESImov edi,TmCopy; Put the address of the local tm copy in EDImov ecx,9; A tm struct is 9 dwords in size under Linuxcld; Clear DF so we move up-memoryrep movsd; Copy static tm struct to local copy; Display one of the fields in the tm structuremov edx,dword [TmCopy+20] ; Year field is 20 bytes offset into tmadd edx,1900; Year field is # of years since 1900push edx; Push value onto the stackpush YrMsg; Push address of the base stringcall printf; Display string and year value with printfadd esp,8; Stack cleanup: 2 parms X 4 bytes = 8; Wait a few seconds for user to press Enter so we have a time difference:call getchar; Wait for user to press Enter; Calculating seconds passed since program began running:push dword 0; Push null ptr; we'll take value in EAXcall time; Get current time value; return in EAXadd esp,4; Clean up the stackmov [NewTime],eax; Save new time valuesub eax,[OldTime]; Calculate time difference valuemov [TimeDiff],eax; Save time difference valuepush dword [TimeDiff]; Push difference in seconds onto the stackpush Elapsed; Push addr. of elapsed time message stringcall printf; Display elapsed timeadd esp,8; Stack cleanup for 1 parm;;; Everything after this is boilerplate; use it for all ordinary apps!pop edi; Restore saved registerspop esipop ebxmov esp,ebp; Destroy stack frame before returningpop ebpret; Return control to Linux


程序分析:
push 0  //函数参数timer=NULL
 call time  //调用time函数,返回当前时间,用与1970年1月1日0点0分0秒的相差的秒数表示。
 add esp,4  //清理栈
 mov [OldTime],eax //保存返回值到OldTime变量

 push OldTime  // 函数参数timer= OldTime
 call ctime  //调用ctime函数,返回时间信息字符串
add esp,4  //清理栈

push eax  //时间字符串压入堆栈
 push TimeMsg  //时间显示格式
 call printf  //调用printf函数,打印时间信息
 add esp,8  //清理栈,因为两个参数,所以加8

push dword OldTime //函数参数 timer= OldTime
 call localtime  //调用localtime函数,对时间值进行转换,返回tm结构体的指针。
 add esp,4  //清理栈

 mov esi,eax  //esi是源,保存返回的tm结构体的地址
 mov edi,TmCopy  //edi=TmCopy,指定拷贝的目的地址
 mov ecx,9  //ecx用于循环计数,tm结构体是9个dword,因此ecx=9
 cld   //清除DF标志,控制地址变化方向从低到高
 rep movsd  //进行拷贝,把返回的tm结构体拷贝到TmCopy

    mov edx,dword [TmCopy+20]  //edx=TmCopy的year字段
 add edx,1900  //因为tm保存的是与1900年的差值,因此要加1900
 push edx  //把年份参数压入堆栈
 push YrMsg  //格式化字符串
 call printf  //调用printf函数打印年份信息
 add esp,8     //清理栈

call getchar  //等待用户键入回车键
 
 push dword 0  //函数参数timer=NULL
 call time  //调用time函数,返回当前时间。
 add esp,4  //清理栈
 mov [NewTime],eax //把当前时间值保存到NewTime中。
 
 sub eax,[OldTime] //用NewTime- OldTime的值放入eax
 mov [TimeDiff],eax //差值保存到TimeDiff中
 
 push dword [TimeDiff] //把差值作为函数参数,压入堆栈
 push Elapsed  //格式化字符串压入堆栈
 call printf  //调用printf函数,显示消耗的秒数信息。
 add esp,8  //清理栈

makefile文件内容:

timetest: timetest.ogcc timetest.o -o timetesttimetest.o: timetest.asmnasm -f elf -g -F stabs timetest.asm


测试:

[root@bogon timetest]# makenasm -f elf -g -F stabs timetest.asmgcc timetest.o -o timetest[root@bogon timetest]# ./timetestHey, what time is it?  It's Wed Jan 14 15:12:39 2015The year is 2015.A total of 2 seconds has elapsed since program began running.


0 0
原创粉丝点击