简体   繁体   English

nasm 程序集 linux 计时器或睡眠

[英]nasm assembly linux timer or sleep

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.我在保护模式下为 Linux 使用 nasm,所以我只能使用 int 80h。 I found a syscall called " alarm " (27) and another called " pause " (29).我发现了一个名为“ alarm ”(27)的syscall调用和另一个名为“ pause ”(29)的syscall调用。 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.我还发现了另一个syscall sigaction,它改变了信号的行为(所以我认为它可以用来使程序忽略警报生成的信号而不是退出),但我不太明白 sigaction 是如何工作的。 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有用的链接: http : //man7.org/linux/man-pages/man2/alarm.2.html http://man7.org/linux/man-pages/man2/sigaction.2.html

There is a system call for sleeping the program, sys_nanosleep :有一个用于休眠程序的系统调用sys_nanosleep

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

this struct timespec structure has two members:这个struct timespec结构有两个成员:

 ;; This is for 32-bit.  Note that x86-64 uses 2x 64-bit members
tv_sec   ; 32 bit seconds
tv_nsec  ; 32 bit nanoseconds

this structure can be declared in nasm as:这个结构可以在 nasm 中声明为:

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

the program then will sleep for 5 seconds.然后程序将休眠 5 秒钟。 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

With NASM, if you are targeting Linux x86-64, you can simply do something similar to the following:使用 NASM,如果您的目标是 Linux x86-64,您可以简单地执行类似以下操作:

global _start

section .data

    timespec:
        tv_sec  dq 1
        tv_nsec dq 200000000

section .text

    _start:
        mov rax, 35
        mov rdi, timespec
        xor rsi, rsi        
        syscall
        ...

35 corresponds to the 64-bit system call number for sys_nanosleep (as listed here ). 35对应于用于64位系统呼叫号码sys_nanosleep (所列出这里)。 If the call is interrupted, the remaining sleep time is written to the memory location pointed by register rsi ;如果调用中断,则将剩余的休眠时间写入寄存器rsi指向的内存位置; in this example rsi is set to 0 to ignore the value if it happens.在本例中, rsi设置为 0 以在发生时忽略该值。 This call will sleep for tv_sec seconds + tv_nsec nanoseconds , 1.2 seconds in the above code snippet.此调用将休眠tv_sec seconds + tv_nsec nanoseconds ,在上面的代码片段中为 1.2 秒。

More information about this system call can be found in the nanosleep man page .有关此系统调用的更多信息,请参见nanosleep 手册页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM