nasm汇编linux定时器或睡眠

问题描述:

我正在尝试寻找一种方法,使我的代码等待两秒钟再继续.我在保护模式下将nasm用于Linux,所以我只能使用int 80h.我找到了一个名为"alarm"的syscall(27)和另一个名为"pause"的29(29).但是,当我尝试使用它们时,程序将等待并完成而不是继续执行.我还发现了另一个syscall sigaction,它可以改变信号的行为(因此,我认为它可以使程序忽略警报生成的信号而不是退出),但是我不太了解sigaction的方式作品.谢谢你的帮助. 有用的链接: http://man7.org/linux/man-pages/man2/alarm.2.html http://man7.org/linux/man-pages/man2/sigaction .2.html

I'm trying to find a way to make my code wait for two seconds before proceeding. I'm using nasm for Linux in protected mode, so I can only use int 80h. I found a syscall called "alarm" (27) and another called "pause" (29). However, when I try to use those, the program waits and finishes instead of continuing execution. I've also found another syscall, sigaction, which changes the behavior of a signal (so I think it can be used to make the program ignore the signal generated by alarm instead of exiting) but I didn't quite understand how sigaction works. Thanks for any help. Useful links:http://man7.org/linux/man-pages/man2/alarm.2.html http://man7.org/linux/man-pages/man2/sigaction.2.html

有一个使程序进入休眠状态的系统调用, sys_nanosleep :

There is a system call for sleeping the program, sys_nanosleep:

 sys_nanosleep : eax = 162, ebx = struct timespec *, ecx = struct timespec *

struct timespec结构具有两个成员:

tv_sec   ; 32 bit seconds
tv_nsec  ; 32 bit nanoseconds

此结构可以在nasm中声明为:

this structure can be declared in nasm as:

section .data

  timeval:
    tv_sec  dd 0
    tv_usec dd 0

然后设置值并将其命名为:

and then you sets the values and call it as:

mov dword [tv_sec], 5
mov dword [tv_usec], 0
mov eax, 162
mov ebx, timeval
mov ecx, 0
int 0x80

然后程序将休眠5秒钟.一个完整的例子:

the program then will sleep for 5 seconds. A complete example:

global  _start

section .text
_start:

  ; print "Sleep"
  mov eax, 4
  mov ebx, 1
  mov ecx, bmessage
  mov edx, bmessagel
  int 0x80

  ; Sleep for 5 seconds and 0 nanoseconds
  mov dword [tv_sec], 5
  mov dword [tv_usec], 0
  mov eax, 162
  mov ebx, timeval
  mov ecx, 0
  int 0x80

  ; print "Continue"
  mov eax, 4
  mov ebx, 1
  mov ecx, emessage
  mov edx, emessagel
  int 0x80

  ; exit
  mov eax, 1
  mov ebx, 0
  int 0x80

section .data

  timeval:
    tv_sec  dd 0
    tv_usec dd 0

  bmessage  db "Sleep", 10, 0
  bmessagel equ $ - bmessage

  emessage  db "Continue", 10, 0
  emessagel equ $ - emessage