Skip to content
Advertisement

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. 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

Advertisement

Answer

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

JavaScript

this struct timespec structure has two members:

JavaScript

this structure can be declared in nasm as:

JavaScript

and then you sets the values and call it as:

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement